在“返回菜单”按钮上按下有一个应该播放的声音,但由于导航到主视图控制器,声音没有播放。我尝试这样做,所以它在返回之前等待声音,但它不起作用,我该怎么办?
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];
});
答案 0 :(得分:0)
正如@Surjeet所说"而不是在课堂上分配AVAudioPlayer。在AppDelegate类中分配它,然后在appdelegate中创建一个方法,并从任何类按钮单击调用该方法。这样你的声音就会一直播放“。