我有一个AVPlayer流媒体广播电台,并有一个我建立的倒带按钮。当你回放时,该方法被调用并且大约80%的时间都工作,成功地将AVPLayer的当前时间设置为X-许多秒。但是,另外20%的时间,AVPlayer停止播放音频,并且在应用程序被杀死并重新打开之前不会再播放。这是方法:
- (IBAction)rewind:(id)sender
{
CMTime cmTime = CMTimeMake(CMTimeGetSeconds(self.player.currentTime) - 1.0, 1);
CMTime almostZero = CMTimeMake(1, 2);
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(almostZero)) {
[self.player.currentItem seekToTime:cmTime];
}
}
对于上下文,它是一个实时流。
这是我的15秒倒带方法:
if((self.currentPlaybackTime - 15.0f) > CMTimeGetSeconds(kCMTimeZero)) {
[self.player pause];
[self.player seekToTime:CMTimeMakeWithSeconds((self.currentPlaybackTime - 15.0f), self.player.currentTime.timescale)];
[self.player play];
} else if((self.currentPlaybackTime - 10.0f) > CMTimeGetSeconds(kCMTimeZero)) {
[self.player pause];
[self.player seekToTime:CMTimeMakeWithSeconds((self.currentPlaybackTime - 10.0f), self.player.currentTime.timescale)];
[self.player play];
} else if((self.currentPlaybackTime - 5.0f) > CMTimeGetSeconds(kCMTimeZero)) {
[self.player pause];
[self.player seekToTime:CMTimeMakeWithSeconds((self.currentPlaybackTime - 5.0f), self.player.currentTime.timescale)];
[self.player play];
} else if((self.currentPlaybackTime - 3.0f) > CMTimeGetSeconds(kCMTimeZero)) {
[self.player pause];
[self.player seekToTime:CMTimeMakeWithSeconds((self.currentPlaybackTime - 3.0f), self.player.currentTime.timescale)];
[self.player play];
} else if((self.currentPlaybackTime - 2.0f) > CMTimeGetSeconds(kCMTimeZero)) {
[self.player pause];
[self.player seekToTime:CMTimeMakeWithSeconds((self.currentPlaybackTime - 2.0f), self.player.currentTime.timescale)];
[self.player play];
} else if((self.currentPlaybackTime - 1.0f) > CMTimeGetSeconds(kCMTimeZero)) {
[self.player pause];
[self.player seekToTime:CMTimeMakeWithSeconds((self.currentPlaybackTime - 1.0f), self.player.currentTime.timescale)];
[self.player play];
}
- (NSTimeInterval)currentPlaybackTime
{
return CMTimeGetSeconds(self.player.currentTime);
}
当我记录方法和秒时,你可以看到在第三次倒带时(音频总是停止并且不会重启),玩家实际上并没有回到15秒,虽然看起来似乎是seekToTime当前时间减去15秒。
2015-05-28 09:34:57.383 Lancers[78554:11732524] sec: 45
2015-05-28 09:34:58.382 Lancers[78554:11732524] sec: 46
2015-05-28 09:34:59.383 Lancers[78554:11732524] sec: 47
2015-05-28 09:35:00.383 Lancers[78554:11732524] sec: 48
2015-05-28 09:35:01.382 Lancers[78554:11732524] sec: 49
2015-05-28 09:35:02.237 Lancers[78554:11732524] CURRENT PLAYING TIME: 49.855237
2015-05-28 09:35:02.237 Lancers[78554:11732524] REWIND 15 SECONDS!
2015-05-28 09:35:02.293 Lancers[78554:11732524] sec: 35
2015-05-28 09:35:02.293 Lancers[78554:11732524] sec: 35
2015-05-28 09:35:02.305 Lancers[78554:11732524] sec: 35
2015-05-28 09:35:02.305 Lancers[78554:11732524] sec: 35
2015-05-28 09:35:02.305 Lancers[78554:11732524] sec: 35
2015-05-28 09:35:02.472 Lancers[78554:11732524] sec: 35
2015-05-28 09:35:03.472 Lancers[78554:11732524] sec: 36
2015-05-28 09:35:04.473 Lancers[78554:11732524] sec: 37
2015-05-28 09:35:05.473 Lancers[78554:11732524] sec: 38
2015-05-28 09:35:06.473 Lancers[78554:11732524] sec: 39
2015-05-28 09:35:07.472 Lancers[78554:11732524] sec: 40
2015-05-28 09:35:08.473 Lancers[78554:11732524] sec: 41
2015-05-28 09:35:09.473 Lancers[78554:11732524] sec: 42
2015-05-28 09:35:10.473 Lancers[78554:11732524] sec: 43
2015-05-28 09:35:11.472 Lancers[78554:11732524] sec: 44
2015-05-28 09:35:12.472 Lancers[78554:11732524] sec: 45
2015-05-28 09:35:13.472 Lancers[78554:11732524] sec: 46
2015-05-28 09:35:13.771 Lancers[78554:11732524] CURRENT PLAYING TIME: 46.299634
2015-05-28 09:35:13.771 Lancers[78554:11732524] REWIND 15 SECONDS!
2015-05-28 09:35:13.821 Lancers[78554:11732524] sec: 31
2015-05-28 09:35:13.821 Lancers[78554:11732524] sec: 31
2015-05-28 09:35:13.833 Lancers[78554:11732524] sec: 31
2015-05-28 09:35:13.833 Lancers[78554:11732524] sec: 31
2015-05-28 09:35:13.833 Lancers[78554:11732524] sec: 31
2015-05-28 09:35:14.557 Lancers[78554:11732524] sec: 32
2015-05-28 09:35:15.557 Lancers[78554:11732524] sec: 33
2015-05-28 09:35:16.556 Lancers[78554:11732524] sec: 34
2015-05-28 09:35:16.870 Lancers[78554:11732524] CURRENT PLAYING TIME: 34.314164
2015-05-28 09:35:16.870 Lancers[78554:11732524] REWIND 15 SECONDS!
2015-05-28 09:35:16.898 Lancers[78554:11732524] sec: 34
2015-05-28 09:35:16.899 Lancers[78554:11732524] sec: 34
答案 0 :(得分:2)
我猜测你的应用程序,但我认为问题可能在于缓冲流媒体音频。我曾经多次使用AVPlayer与本地媒体,并且从未遇到过seekToTime的任何问题。
跳到无效的时间会让你感到困惑,但是从我在你的代码中看到的你应该没问题。您的倒带代码试图向后移动1秒(而不是15?),除非您在开始的1/2秒内,在这种情况下您不会寻找并继续播放。
我会在回放方法中检查AVPlayer对象(player.status,player.error)的状态,并查看调用seekToTime时发生的情况。还要使用调试器或NSLog检查cmTime变量的值和状态标志,以确保您也很好。