OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何让音频播放器再次播放? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:56
标题: iphone - 如何让音频播放器再次播放?

我有 2 个 View Controller ,我在第一个 View Controller 中使用 AVAudioPlayer 播放音乐。 我转到第二个 View Controller 并停止音乐:

[audioPlayer stop];    
[UIView beginAnimations"Curl View" context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
        [self.view addSubview:secondController.view];
        [UIView commitAnimations];

当我再次回到第一个 View Controller 时,我想再次播放我的音乐,但代码没有再次触及 viewDidLoad。 这是我返回的代码:

[UIView beginAnimations"Flipping View" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
    [self.view removeFromSuperview];
    [UIView commitAnimations];

请分享一些解决方案或之后代码执行的内容..提前谢谢



Best Answer-推荐答案


这只是因为您从 viewDidLoad 调用音乐播放代码 并且 viewDidLoad 方法只为 ViewController 调用一次。

您应该从 viewWillAppear 而不是 viewDidLoad 方法调用您的代码(播放音乐)。

I suggest you should read UIViewControler Documentation.

更新答案:因为您想在加载和返回查看时播放音乐文件。 性能方面可能存在一些问题。 因为你调用了 Music 文件立即加载的时候查看。 所以查看下面的解释行。

关于iphone - 如何让音频播放器再次播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9511877/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4