Ios AVPlayer seekToTime导致播放器停止播放

Ios AVPlayer seekToTime导致播放器停止播放,ios,objective-c,audio,avplayer,Ios,Objective C,Audio,Avplayer,我有一个AVPlayer,可以播放广播电台的节目,还有一个我自己制作的回放按钮。当您倒带时,将调用该方法并在大约80%的时间内工作,从而成功地将AVPLayer的当前时间设置回X多秒。然而,在另外20%的时间里,AVPlayer会停止播放音频,并且在应用程序关闭并重新打开之前不会再次播放音频。方法如下: - (IBAction)rewind:(id)sender { CMTime cmTime = CMTimeMake(CMTimeGetSeconds(self.player.curre

我有一个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秒,尽管看起来搜索时间到当前时间减去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

我在猜测你的应用程序,但我认为问题可能在于你的流媒体音频的缓冲。我曾多次在当地媒体上使用AVPlayer,但从未遇到过seekToTime的任何问题

跳到一个无效的时间会把你搞得一团糟,但从我在你的代码中看到的,你应该没事。你的倒带代码试图向后移动1秒(不是15?),除非你在开始的1/2秒内,在这种情况下,你不寻找并继续玩


我会检查回放方法中AVPlayer对象(player.status,player.error)的状态,看看调用seekToTime时发生了什么。还可以使用调试器或NSLog检查cmTime变量的值和状态标志,以确保您在这方面也做得很好。

如果您有一种可行的方法,问题是什么?它没有。“回放”按钮工作两次,然后冻结音频。你只能倒带30秒,然后播放器冻结。如果我让倒带2方法简单地调用倒带1方法15次,播放器在第三次调用时仍然会冻结。我不清楚“冻结”是什么意思。如果您想在查找后继续播放,那么调用
seekToTime:preference:preference:preference:preference:completionHandler:
是否有帮助?
completionHandler:
的全部要点是,您可以在查找后执行类似于play的操作。谢谢。我应该将公差设置为什么?当我用倒带1方法搜索时,它会继续播放,所以我想它总是在你搜索后继续播放。我不知道,但医生说这是最准确的。我不是说这会管用,但如果真的管用,那就太好了!给它一个镜头,让我们看看会发生什么…我有两种不同的倒带方法,一种是15秒,另一种是1秒。我以前没有使用过AVPlayer,所以我不完全确定如何实施您的建议,但我非常感谢您的建议,并将尽我最大的努力。像这样简单的事情应该行得通,不是吗<代码>NSLog(@“ERROR:%@”,self.player.ERROR);NSLog(@“状态:%d”,self.player.STATUS)确定。消除可能的问题。是的,您的日志记录正确。那么@matt关于在方法中查看cmTime值的建议呢。也许在第二次计算cmTime之后,它变得很古怪。你可以发布-15秒调整的代码,这样我们就可以看到给你带来问题的执行代码了吗?thx-dclOK。以下是我认为正在发生的事情。不幸的是,如果我是对的,你的代码永远不会工作。它有时可能只是AVPlayer内部工作的时机。AVPlayer.currentTime仅在播放时返回当前时间。如果停止,则currentTime返回介质的开始时间。因此,在调用[self.player pause]后,从currentPlaybackTime返回的值与您的想法不同。在暂停之前保存currentTime,并使用该值进行seekToTime计算。我相信您能够做到。只要你将流媒体数据保存在AVPlayer的某个缓冲区中,它就不是真正的流媒体。听起来你已经有数据进入你的应用程序并播放了——这是一个很大的进步。考虑一下数据是什么,它在你的应用程序中流动的地方。有了它,你就可以找到最好的缓存位置,你想保存多少,保存多长时间。然后,这将决定是否将其保存到本地文件、核心数据等。