【问题标题】:how to stop audio playing if user leaves the view如果用户离开视图,如何停止音频播放
【发布时间】:2012-01-19 17:50:11
【问题描述】:

我有一个音频文件,当用户单击一个视图时播放。我的问题是,如果他们移动到另一个视图,音频会继续播放。如果用户不再在该视图中,我希望音频停止。

这是在 viewDidUnload 方法中完成的吗?

这是延迟 15 秒后播放音频的代码:

-(void)startPlaying {

    [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(startPlaying) userInfo:nil repeats:NO];


    NSString *audioSoundPath = [[ NSBundle mainBundle] pathForResource:@"audio_file" ofType:@"caf"];

    CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath:audioSoundPath];
    AudioServicesCreateSystemSoundID(audioURL, &audioID);
    AudioServicesPlaySystemSound(audioID);
}

感谢您的帮助。

【问题讨论】:

    标签: iphone objective-c core-audio


    【解决方案1】:
    -(void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        // Stop Sound
    }
    

    【讨论】:

      【解决方案2】:

      -(void)viewWillDisappear:(BOOL)animated { [超级viewWillDisappear:动画];

      AudioServicesDisposeSystemSoundID(audioID);
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 2012-10-18
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 2018-08-23
        • 2021-10-11
        相关资源
        最近更新 更多