[英]Sound does not play when going to another view controller (iOS)
在“返回菜單”按鈕上,按一下應該播放的聲音,但是由於導航到主視圖控制器而沒有播放聲音。 我嘗試這樣做,因此它在返回之前等待聲音,但是它不起作用,我該怎么辦?
if(appDelegate.disabledSoundEffects==0)
[self performSelectorOnMainThread:@selector(playButtonSound)
withObject:NULL waitUntilDone:true];
playButtonSound是播放聲音的方法,很明顯,如果我在這里做錯了,那就是它的代碼
dispatch_queue_t dispatchQueue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(dispatchQueue, ^(void){
NSBundle * mainBundle = [NSBundle mainBundle];
NSString * filePath = [mainBundle pathForResource:@"clickSound"
ofType:@"mp3"];
NSData * fileData=[NSData dataWithContentsOfFile:filePath];
NSError * error= nil;
self.audioPlayer=[[AVAudioPlayer alloc] initWithData:fileData error:&error];
if(self.audioPlayer!=nil)
self.audioPlayer.delegate=self;
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
});
正如@Surjeet所說:“與其在類內分配AVAudioPlayer,不如在AppDelegate類中分配它,然后在appdelegate中創建一個方法,然后在任何類按鈕單擊時都調用該方法。這樣您的聲音將始終播放。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.