我正在使用AVPlayer
从远程网址播放mp3文件。我对mp3的初始加载时间有一些问题,它非常慢(大约5-8秒)。
我将它与其他第三方玩家进行了比较,速度要慢得多,我也将它与Android玩家进行了比较,而且速度也慢得多。
所以问题不在于网址本身,也不在于网络连接。
另一个有趣的一点是,在AVPlayer
开始播放mp3后,寻求非常快(几乎立即),这是否意味着播放器在开始播放前下载整个mp3文件,这就是它如此慢的原因?
我可以控制这种行为吗?如果没有,任何其他想法可能是什么原因?
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);
对于iOS 10.x或更高版本来减少缓慢的加载时间,我设置:avplayer.automaticallyWaitsToMinimizeStalling = false;
,这似乎为我修复了它。这可能会产生其他后果,但我还没有打到那些。
我从它那里得到了它的想法:AVPlayer stops playing video after buffering
在这里,我对它的看法。
那个不同的文件有不同的移动原子,所以如果在这样的文件中,如果移动原子首先出现,那么这个文件将使用缓冲。因此,这个文件与部件一起下载并且正在播放继续缓冲。
在其他情况下,在文件移动原子是最后然后第一个整个文件下载然后它播放。所以我认为这是你的情况,这就是为什么mp3延迟一段时间下载整个mp3。
您可以使用编码将move atom更改为此类文件。看看这个答案Move and fix moov atom of video recorded on phone iOS