Ana*_*d Y 7 objective-c uiprogressbar ios avplayer
我有以下代码:
AVPlayerItem *currentItem = [AVPlayerItem playerItemWithURL:soundURL];
[self.audioPlayer replaceCurrentItemWithPlayerItem:currentItem];
[self.audioPlayer play];
Run Code Online (Sandbox Code Playgroud)
这里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];
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题吗?
我建议你做几件事.首先,获取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];
}];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4779 次 |
| 最近记录: |