我一直使用音频工具箱来播放我的声音,但是用户说没有声音,这是因为当你处于静音状态时它不起作用。我已多次尝试交换为av基础,但它永远不适合我。这是我现在尝试使用的代码:
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"EASPORTS" ofType:@"mp3"];
AVAudioPlayer * theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];}
答案 0 :(得分:3)
原因可能是在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;