【问题标题】:AVAudioPlayer pausing issueAVAudioPlayer 暂停问题
【发布时间】:2012-03-13 22:04:35
【问题描述】:

在我的项目中,我有一个播放歌曲的 AVAudioPlayer。在我暂停它并再次点击播放后,它会重新开始,而不是在我暂停的地方播放。为什么会这样? 此外,当我第一次按播放时,它会加载大约 3-4 秒的歌曲。我以为我通过在另一个线程上加载它来解决它,但它仍然加载得太慢(至少视图不再冻结)。我该如何解决?代码如下:

- (IBAction)play:(id)sender {
  songIsCurrentlyPaused = NO;
  if(songIsCurrentlyPaused==YES){
    [self.background play];
  } else {
    playQueue = dispatch_queue_create("volume_change", NULL);
    dispatch_async(playQueue, ^{ NSString *filePath = 
      [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
      NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
      self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
      self.background.delegate = self;
      [self.background setNumberOfLoops:1];
      [self.background setVolume:0.5];
      [self.background play]; });

      [trackNameLabel setText:@"Currently playing :\n some_song"];
      self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
    }
}

- (IBAction)pause:(id)sender {
  songIsCurrentlyPaused = YES;
  [self.background pause];
  [trackNameLabel setText:@"Currently playing : some_song (paused)"];
  [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
}

谢谢!

【问题讨论】:

    标签: ios avaudioplayer resume performance


    【解决方案1】:

    您在play: 的开头将songIsCurrentlyPaused 设置为NO

    尝试将其注释掉:

    - (IBAction)play:(id)sender {
      //songIsCurrentlyPaused = NO;
      if(songIsCurrentlyPaused==YES){
        [self.background play];
      } else {
        playQueue = dispatch_queue_create("volume_change", NULL);
        dispatch_async(playQueue, ^{ NSString *filePath = 
          [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
          NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
          self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
          self.background.delegate = self;
          [self.background setNumberOfLoops:1];
          [self.background setVolume:0.5];
          [self.background play]; });
    
          [trackNameLabel setText:@"Currently playing :\n some_song"];
          self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
        }
    }
    
    - (IBAction)pause:(id)sender {
      songIsCurrentlyPaused = YES;
      [self.background pause];
      [trackNameLabel setText:@"Currently playing : some_song (paused)"];
      [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
    }
    

    如果您想摆脱最初的停顿,您将不得不完全重新组织您的播放器设置。在用户点击播放按钮并调用之前对其进行初始化:

    [self.background prepareToPlay];
    

    这将预加载歌曲。还将songIsCurrentlyPaused = NO; 移动到代码中的某个较早位置。

    编辑:

    要消除初始延迟,您应该将初始化代码移至 loadViewviweDidLoad 之类的地方。

       //initialization code
       NSString *filePath = [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
       NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
       self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
       self.background.delegate = self;
       [self.background setNumberOfLoops:1];
       [self.background setVolume:0.5];
       [self.background prepareToPlay];
    

    现在这可能会导致 UI 显示滞后,因此您可能不想考虑预加载数据 在后台线程中。

    你的IBAction 方法应该被修改:

    - (IBAction)play:(id)sender
    {
    
       if (songIsCurrentlyPaused==YES) 
       { 
          [self.background play];
       }
       else
       {
          playQueue = dispatch_queue_create("volume_change", NULL);
          dispatch_async(playQueue, ^{
             [self.background setCurrentTime: 0.0];
             [self.background play];
          });
    
          [self.progressBar setProgress:0.0 animated:YES];
          [trackNameLabel setText:@"Currently playing :\n some_song"];
          self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
    
      }
      songIsCurrentlyPaused = NO;
    }
    
    - (IBAction)pause:(id)sender
    {
      songIsCurrentlyPaused = YES;
      [self.background pause];
      [trackNameLabel setText:@"Currently playing : some_song (paused)"];
      [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
    }
    

    【讨论】:

    • 暂停问题已解决!谢谢,非常感谢!但是加载仍然很慢:(
    • @nemesis:我编辑了我的答案。因为我现在不在我的 Mac 上,所以无法获取代码。它应该可以工作,但可能需要一些调整。这个想法是预加载音乐。如果您可以在某个后台线程上移动预加载,那么应该(几乎)没有延迟。
    • 好吧,如果您在视图加载一段时间后点击播放按钮,它就会起作用。没有办法完全消除延迟吗?谢谢!!
    • @nemesis:如果您将AVAudioPlayer 作为AppDelegate 的属性并开始在@ 启动的后台线程中预加载它,您可能 会加快速度987654334@。仍然会有一些延迟 - 预加载需要时间。尝试想办法在此期间用一些动画或其他东西分散用户的注意力。
    • 我只是通过显示一个提示他查看设置视图的警报来做到这一点 :)) 先生,你太高兴了!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    相关资源
    最近更新 更多