【问题标题】:How to make audioplayer play again?如何让音频播放器再次播放?
【发布时间】:2012-03-01 06:53:27
【问题描述】:

我有 2 个视图控制器,我在第一个视图控制器中使用 AVAudioPlayer 播放音乐。 我转到第二个视图控制器并停止音乐:

[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];

当我再次回到第一个视图控制器时,我想再次播放我的音乐,但代码没有再次触及 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];

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

【问题讨论】:

    标签: iphone ios uiview uiviewcontroller avaudioplayer


    【解决方案1】:

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

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

    I suggest you should read UIViewControler Documentation.

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

    • ViewDidLoad - 在视图控制器将其视图层次结构加载到内存后调用此方法。无论视图层次结构是从 nib 文件加载还是在 loadView 方法中以编程方式创建,都会调用此方法。您通常会覆盖此方法以对从 nib 文件加载的视图执行额外的初始化。

    • ViewWillAppear:当被调用时,这意味着 iPhone 已经准备好向用户显示 UIView,你在这里做的任何繁重的事情都会以非常明显的方式影响性能(比如动画被延迟,或播放音乐等)。

    • ViewDidAppear:最后,这会通知视图控制器其视图已添加到视图层次结构中。因此此方法足以完成此类任务,例如播放音乐文件。注意- ->你必须在你的实现中调用super

    【讨论】:

    • 我添加了 void void viewDidAppear,当我从 secondController 回来时,编译器不执行 viewDidAppear。我也从控制台查看它,编译器没有触及它。能不能详细点?
    • @Alex Moskalev 你在那个(ViewDidAppear) 方法中调用了super。你必须在你的实现中调用super
    • 我在 secondController 中的 [UIView commitAnimations] 之前添加了 [super viewDidAppear:YES] 但它仍然不起作用。我做错了什么?
    【解决方案2】:

    viewWillAppear: 中编写你的播放代码,当你的视图出现时它会被调用。 viewDidLoad: 只为视图控制器调用一次

    更新 使用这些行删除您的第二个视图

    [super viewWillAppear:animated];
    UIView beginAnimations:@"Flipping View" context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationEnded)];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
    [self.view removeFromSuperview];
    [UIView commitAnimations];
    

    并且这个方法会在视图被移除的时候被调用

    - (void)animationEnded{ 
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/BGMCover2.mp3", [[NSBundle mainBundle] resourcePath]]]; 
        NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = -1; 
        [audioPlayer play]; 
    }
    

    【讨论】:

    • 我添加了 void void viewWillAppear,当我从 secondController 回来时,编译器不会执行 viewWillAppear。我也从控制台查看它,编译器没有触及它。能不能详细一点?
    • NSLog(@"View Did Appear"); NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/BGMCover2.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *错误; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = -1; [音频播放器播放];
    • 请写下整个方法,包括方法名称,还有一个你是推送还是展示第二个视图控制器???
    • 这是我的 viewDidAppear 方法: - (void)viewDidAppear:(BOOL)animated { NSLog(@"View Did Appear"); NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/BGMCover2.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *错误; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = -1; [音频播放器播放]; }
    • @AlexMoskalev 调用如下所示 -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:YES]; NSLog(@"视图出现了"); NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/BGMCover2.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *错误; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = -1; [音频播放器播放]; }
    【解决方案3】:

    在做你的应用程序之前,请深入了解 UIViewController 的生命周期。您可以点击以下链接: UIViewController Life Cycle

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多