繁体   English   中英

AVFoundation无法播放声音

[英]AVFoundation not playing sound

我一直使用音频工具箱播放声音,但是用户说没有声音,这是因为当您处于静音状态时不会播放声音。 我已经尝试过很多次,而不是换成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];}

原因可能是在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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM