ios - AVPlayer seekToTime : backward not working

标签 ios objective-c avplayer uiprogressbar

我有以下代码:

AVPlayerItem *currentItem = [AVPlayerItem playerItemWithURL:soundURL];
[self.audioPlayer replaceCurrentItemWithPlayerItem:currentItem];
[self.audioPlayer play];

其中 soundURL 是一个 remoteURL。它工作正常。 AVPlayer 完美播放音乐。我有一个进度条,我正在根据玩家的当前时间更新它。

一切正常。我的问题是,当我向前拖动进度条时,audioplayer 从新位置开始,但如果我拖动 progressbar 它不会从新位置开始,实际上它会恢复从以前的位置。这是我的进度条拖动开始和停止代码:

- (IBAction)progressBarDraggingStart:(id)sender
{
     if (self.audioPlayer.rate != 0.0)
     {
          [self.audioPlayer pause];
     }
}

- (IBAction)progressBarDraggindStop:(id)sender
{
     CMTime newTime = CMTimeMakeWithSeconds(self.progressBar.value, 1);
     [self.audioPlayer seekToTime:newTime];
     [self.audioPlayer play];
}

谁能帮我解决这个问题?

最佳答案

我建议做几件事。首先,获取 timescale 值并将其传递给 CMTime 结构。其次,使用 seekToTime:toleranceBefore:toleranceAfter:completionHandler: 方法进行更准确的查找。例如,您的代码如下所示:

- (IBAction)progressBarDraggindStop:(id)sender {
    int32_t timeScale = self.audioPlayer.currentItem.asset.duration.timescale;

    [self.audioPlayer seekToTime: CMTimeMakeWithSeconds(self.progressBar.value, timeScale)
                 toleranceBefore: kCMTimeZero
                  toleranceAfter: kCMTimeZero
               completionHandler: ^(BOOL finished) {
                   [self.audioPlayer play];
               }];
}

关于ios - AVPlayer seekToTime : backward not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21992538/

相关文章:

ios - Swift 1.2 (Xcode 6.3) 删除了 Bool 值的 xor '^' 运算符?

ios - 在 Objective-C 中向不同对象发送消息的最佳方式

ios - 如何让NSTimer和一些操作在后台继续运行?

ios - 如何在应用程序设置中全局访问?

ios - 如何预加载 AVPlayerItem

iphone - 更改 UITableView 以清除颜色

ios - 存折无法识别 iBeacon

ios - 如何模拟后台附件 iPad

ios - 如何使用 UISlider 更改视频的 CIFilter 值?

ios - 延迟后在后台播放一系列音频片段