AVPlayer慢加载

8
我正在使用AVPlayer播放远程URL的mp3文件。 我遇到了mp3初始加载时间非常慢的问题(约为5-8秒)。 与其他第三方播放器相比,它慢得多。我还将其与Android播放器进行了比较,它也慢得多。 因此,问题不在于URL本身或网络连接。
另一个有趣的点是,在AVPlayer开始播放mp3之后,寻址非常快(几乎立即完成)。这是否意味着播放器在开始播放之前下载整个mp3文件,这就是为什么速度如此缓慢的原因?我能控制这种行为吗?如果不能,还有什么别的想法可以解决这个问题吗?

或许相关:https://dev59.com/S-o6XIcBkEYKwwoYPSL7 - lukas
3个回答

2

AVPlayer 有一些新功能(适用于 iOS 10+),您可以尝试使用。我自己使用过,一切都正常工作。

/*!
 @method        playImmediatelyAtRate:
 @abstract      Immediately plays the available media data at the specified rate.
 @discussion
 When the player's currentItem has a value of NO for playbackBufferEmpty, this method causes the value of rate to change to the specified rate, the value of timeControlStatus to change to AVPlayerTimeControlStatusPlaying, and the receiver to play the available media immediately, whether or not prior buffering of media data is sufficient to ensure smooth playback.
 If insufficient media data is buffered for playback to start (e.g. if the current item has a value of YES for playbackBufferEmpty), the receiver will act as if the buffer became empty during playback, except that no AVPlayerItemPlaybackStalledNotification will be posted.
 */
- (void)playImmediatelyAtRate:(float)rate NS_AVAILABLE(10_12, 10_0);

此外,您还可以查看此变量(也可以使用KVO):
   /*!
     @property      reasonForWaitingToPlay
     @abstract      Indicates the reason for waiting when the value of timeControlStatus is AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate
     @discussion
        When the value of timeControlStatus is AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate, this property describes why the player is currently waiting. It is nil otherwise.
        You can use the value of reasonForWaitingToPlay to show UI indicating the player's waiting state conditionally.
        This property is key value observable.
        Possible values are AVPlayerWaitingWithNoItemToPlayReason, AVPlayerWaitingWhileEvaluatingBufferingRateReason, and AVPlayerWaitingToMinimizeStallsReason.
    */

    @property (nonatomic, readonly, nullable) NSString *reasonForWaitingToPlay NS_AVAILABLE(10_12, 10_0);

1

为了减少iOS 10.x或更高版本的加载时间,我设置了:avplayer.automaticallyWaitsToMinimizeStalling = false; 这对我来说似乎解决了问题。这可能会产生其他后果,但我还没有遇到过。

我从以下文章中得到了这个想法: AVPlayer stops playing video after buffering


-2

这是我对此的看法。

不同的文件有不同的移动原子,所以如果在这样的文件中移动原子首先出现,那么该文件将使用缓冲播放。因此,该文件被分段下载,并且它会继续通过缓冲播放。

在另一种情况下,如果文件中的移动原子位于最后,则首先整个文件被下载,然后再进行播放。因此,我认为这就是您的情况,这就是为什么mp3需要一些时间来下载整个mp3的原因。

您可以通过编码将移动原子更改为这样的文件。请查看此答案Move and fix moov atom of video recorded on phone iOS


据我所知,MP3文件没有MOOV原子,因为这些原子是特定于MPEG4视频流的。 - Jules

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接