我一直使用音频工具箱来播放我的声音,但用户说没有声音,这是因为当你处于静音状态时,它不能播放。我试过很多次换成av foundation,但对我来说从来都不起作用。这是我现在尝试使用的代码:
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"EASPORTS" ofType:@"mp3"];
AVAudioPlayer * theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];}发布于 2013-03-01 03:34:03
原因可能是在ARC中,您的音频播放器是由ARC发布的,因此您需要对AVAudioPlayer进行强引用,您可以通过将AVAudioPlayer设置为类级属性来实现此目的。在您的.h文件中,如下所示
@property (strong, nonatomic) AVAudioPlayer *theAudio;并在.m文件中像这样合成它
@synthesize theAudio;最后,您的代码将如下所示
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"EASPORTS" ofType:@"mp3"];
self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[self.theAudio play];
}还要检查您的委托方法是否正在响应某些内容
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;https://stackoverflow.com/questions/15144014
复制相似问题