簡體   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