[英]Sound does not play when going to another view controller (iOS)
On “back to menu” button press there is a sound that should be played, but the sound is not playing because of the navigation to the main view controller. 在“返回菜单”按钮上,按一下应该播放的声音,但是由于导航到主视图控制器而没有播放声音。 I tried doing this so it waits for the sound before going back, but it doesn't work, what should I do? 我尝试这样做,因此它在返回之前等待声音,但是它不起作用,我该怎么办?
if(appDelegate.disabledSoundEffects==0)
[self performSelectorOnMainThread:@selector(playButtonSound)
withObject:NULL waitUntilDone:true];
playButtonSound is the method that plays the sound obviously that's the code for it in case I'm doing something wrong there 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.