ios - AVAudioPlayer 暂停问题

标签 ios avaudioplayer resume performance

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

- (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];
}

谢谢!

最佳答案

您在 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];
}

关于ios - AVAudioPlayer 暂停问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9692929/

相关文章:

ios - AVAudioPlayer 与 MPMusicPlayerController 之间有什么区别

ios - AVAudioPlayer 不会播放 mp3 文件

swift - 在显示暂停计时器 Int 的标签上显示暂停和恢复的计时器

javascript - 如何从不透明度暂停和恢复 fadeIn()?

iphone - 对 NSString 的 NSArray 进行排序

ios - 可以在 UICollectionView 中自定义单元格吗?

swift - AVPlayer 音乐在转到其他 View 时停止

android - 如何使通知 Intent 恢复而不是新的 Intent ?

ios - 将 View Controller 关闭到上一个 View Controller

ios - 尝试向 tabBar 添加指示器...但它显示在错误的位置