声音没有播放AVAudioPlayer
我搜索过,我相信我的问题非常独特。 我知道使用AVAudioPlayer时的模拟器5.1错误,这不是我的问题。 我在iOS 5.1设备上运行。
这是我的头文件:
#import #import #import -(IBAction)pushBell; @end
和我的实施文件:
#import "BellViewController.h" @interface BellViewController () @end @implementation BellViewController -(IBAction)pushBell { NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; NSError *error; AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; [theAudio setDelegate:self]; [theAudio setNumberOfLoops:0]; [theAudio play]; } @end
我的.xib中有一个按钮来播放该文件。
为了确保我正确引用了我的音频文件,我故意键入一个错误的文件名,实际上我得到了一个例外。 为了确保文件可播放,我将其邮寄给自己并在设备上播放。
当我点击按钮时,我什么都没听到。 没有错误。 我不知道出了什么问题。
更多信息 iPhone 4与iOS 5.1 Xcode 4.3.2音频大约700kbps转换为.caf格式使用afconvert从一个大的.wav文件。
解决了。
1.-检查您的文件是否已添加到Build Phases中的“Copy Bundle Resources”。
2.- ViewController.m
#import @interface ViewController () @property (nonatomic, strong) AVAudioPlayer *theAudio; - (IBAction)pushBell; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"beep-beep" ofType:@"caf"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; NSError *error = nil; self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; } - (IBAction)pushBell { [self.theAudio play]; }
3.-将IBAction连接到Interface Builder中的按钮。
完成。
这是我的问题,所以我只是在这里张贴以防万一它可以帮助任何人!
信不信由你,似乎有一个错误(至少在iPad 2上的iOS 5.1中),如果你将侧开关设置为静音并且它是静音的,那么将侧开关设置为锁定旋转,AVAudioPlayer听起来不会通过耳机播放!
所以我不得不去设置应用程序,将开关改回静音,然后取消iPad静音,将其切换回锁定旋转,我的应用程序开始正常工作。
你的theAudio
AVAudioPlayer
在它甚至可以开始播放之前被释放(因为它被分配为pushBell方法中的局部变量。
将您的代码更改为:
头文件:
#import #import #import @interface BellViewController:UIViewController @property (nonatomic, strong) AVAudioPlayer *theAudio; -(IBAction)pushBell; @end
实施文件:
#import "BellViewController.h" @implementation BellViewController @synthesize theAudio = _theAudio; - (void)viewDidLoad { if (!theAudio) { NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; NSError *error; _theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; [self.theAudio setDelegate:self]; [self.theAudio setNumberOfLoops:0]; [self.theAudio prepareToPlay]; } } -(IBAction)pushBell { [self.theAudio play]; } @end
我假设BellController
是一个UIViewController
,如果它是一个不同类的类,只需相应地修改代码。
我认为这是一个内存问题,你应该将AVAudioPlayer *theAudio
实例AVAudioPlayer *theAudio
全局类对象并在init
方法中初始化它。 然后运行[theAudio play];
-(IBAction)pushBell
方法中的方法。
我回答了一个与播放系统声音有关的类似问题(事实上这正是你想要做的),检查一下我的回答是在底部:
切换到iOS 5后无法播放系统声音
顺便说一下,考虑播放铃声作为系统声音, AVAudioPlayer
更适合播放音轨,而不是铃声和效果。
我希望它有所帮助。
我同意hooleyhoop。 尝试
NSLog(@"%@", error.userInfo)
看看会发生什么。
另一种方法是在viewDidLoad
或initWithNib
设置播放器对象,并将其存储为对象的属性。 这样你可以使用类似[self.theAudio prepareToPlay]的东西;
每次按下按钮时都要避免加载音频,这样可以节省一些时间,如果它是高质量的音频。 然后打电话给[self.theAudio play];
在你的IBAction
方法。
对不起,如果这个答案有点草率 – 在iPad上打字,所以无法正确检查。
你应该给它一些时间准备发音,因为它可能是从URL缓冲。
代码:
[theAudio prepareToPlay]; [self performSelector:@selector(playMusic) withObject:nil afterDelay:3.0];