简体   繁体   English

转到另一个视图控制器(iOS)时声音不播放

[英]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.

相关问题 iOS 模拟器不播放声音 - iOS Simulator does not play sound 在集合视图控制器中播放键盘点击声音 - Play keyboard click sound in a collection view controller 转到分配了另一个类的另一个视图控制器 - Going to Another View Controller That Is Assigned Another Class 使用Segue从先前的View Controller传递到另一个View Controller时,数据变为Nil - Data turns Nil when passing from previous View Controller going to another View Controller using Segue iOS-在后台“通话”时播放铃声 - iOS - Play ringing sound when “called” in background 当应用程序未运行 IOS 时播放闹钟声音 - Play Alarm Sound when app is not running IOS iOS-返回时如何推送至视图控制器并维护正确的“ View Stack” - iOS - How to push to a view controller and maintain the correct “View Stack” when going back 回到另一个视图控制器IOS7后如何暂停一次音频 - How to pause the audio once after going back to another view controller IOS7 Android媒体控制器无法播放iOS录制的视频的声音 - Android media controller doesn't play sound for iOS recorded videos 导航回父级View Controller时,UIButton在标签栏后面-iOS - UIButton going behind Tab Bar when navigating back to parent View Controller - iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM