首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVFoundation不播放声音

AVFoundation不播放声音
EN

Stack Overflow用户
提问于 2013-03-01 03:28:31
回答 1查看 945关注 0票数 0

我一直使用音频工具箱来播放我的声音,但用户说没有声音,这是因为当你处于静音状态时,它不能播放。我试过很多次换成av foundation,但对我来说从来都不起作用。这是我现在尝试使用的代码:

代码语言:javascript
复制
- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"EASPORTS" ofType:@"mp3"];
AVAudioPlayer * theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

[theAudio play];}
EN

回答 1

Stack Overflow用户

发布于 2013-03-01 03:34:03

原因可能是在ARC中,您的音频播放器是由ARC发布的,因此您需要对AVAudioPlayer进行强引用,您可以通过将AVAudioPlayer设置为类级属性来实现此目的。在您的.h文件中,如下所示

代码语言:javascript
复制
@property (strong, nonatomic) AVAudioPlayer *theAudio;

并在.m文件中像这样合成它

代码语言:javascript
复制
@synthesize theAudio;

最后,您的代码将如下所示

代码语言:javascript
复制
- (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];
}

还要检查您的委托方法是否正在响应某些内容

代码语言:javascript
复制
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15144014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档