AVPlayer slow loading

前端 未结 3 1463
南方客
南方客 2021-01-12 18:08

I\'m using AVPlayer to play mp3 files from a remote url. i\'m having some issues with the initial loading time of the mp3, it is very slow (around 5-8 sec).

相关标签:
3条回答
  • 2021-01-12 18:39

    Here, What I think about it.

    That different file has different move atoms, So if in such file if move atom came first then this file plays with buffering. So this file downloaded with parts and it's playing continues with buffering.

    In other case, in file move atom is at last then first whole file downloaded then it plays. So I think that is your case that's why mp3 delays with some time for downloading whole mp3.

    You can change move atom into such file with coding. Check out this answer Move and fix moov atom of video recorded on phone iOS

    0 讨论(0)
  • 2021-01-12 18:41

    For iOS 10.x or greater to reduce slow load time I set: avplayer.automaticallyWaitsToMinimizeStalling = false; and that seemed to fix it for me. This could have other consequences, but I haven't hit those yet.

    I got the idea for it from: AVPlayer stops playing video after buffering

    0 讨论(0)
  • 2021-01-12 18:54

    AVPlayer has some new functionality (for iOS 10+), that You can try out. I used it myself and everything was working properly.

    /*!
     @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);
    

    Additionally You can check out this variable (You can use KVO for it too):

       /*!
         @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);
    
    0 讨论(0)
提交回复
热议问题