When I used the player I found that the AVplayer buffer reached a certain time but the player did not enter the prepared state
Related
I'm using AVPlayer to play a HLS live stream and I'm trying to force a delay by simulating a pause setting the player rate to 0.05, when the delay reaches the desired delay I set the rate to 1 and sometimes the player resets the stream. This can be observed more often on a simulator. Does anyone has an idea why is this happening?
To address the question asked in the headline/title of your question:
I use player.pause() and player.play() to pause and resume HLS feeds, and it works perfectly. If you are programmatically determining the timing of your delay, you could try timing the pause() and play() methods.
Now, to address the question in the body:
I've occasionally observed this 'reset' behavior when trying to sync two devices manually (pause/play/pause/play).
My guess is that the player does its best to sync up to keyframes, depending on what it has buffered. Factors like the network connection's jitter or the stream's bit rate, keyframe rate, and frame rate will all play into how well the player recovers from state changes, and where it picks up in the stream.
When you're using the simulator, you're not benefitting from the performance gains of the actual hardware (since everything is emulated), so it's even more taxing on the CPU and more likely to be jumpy.
Playing video with avplayer, listening to the loadedTimeRanges property, playing about 10 minutes of video, avplayer always preload the video, and feel very costly, is there a way to limit the size of the preloaded area? Such as pre-loading video half of the time?
I think you're looking for AVPlayerItem's preferredForwardBufferedDuration property.
Per Apple:
This property defines the preferred forward buffer duration in seconds. If set to 0, the player will choose an appropriate level of buffering for most use cases. Setting this property to a low value will increase the chance that playback will stall and re-buffer, while setting it to a high value will increase demand on system resources.
See https://developer.apple.com/reference/avfoundation/avplayeritem/1643630-preferredforwardbufferduration?language=objc
If you use resourceLoaderdelegate you can control the exact amount of content that gets preloaded/downloaded prior to playback.
The code example here is a good start - AVPlayer stalling on large video files using resource loader delegate
Basically, you would have to maintain an array of pendingRequests and process them once by one by firing up URLSession dataTask until you have downloaded enough content that you would like to preload.
Cheers.
I'm using AVPlayer to play online video content. Server supports byte ranges as according to RFC 2616.
Thing is that it takes too long for AVPlayer to have playable status.
For a 1GB video (duration = 1h 45min) it takes 1min-2min to start playback. I notice that seek works rather good after that.
Playing same video on default Android media player takes 15sec to start.
It seems as if AVPlayer downloads chunks in different parts of media space for better seek support. But I want video to start as soon as possible. Is such a thing possible - to configure AVPlayer for quicker play?
I need to get information about total time a video is played using MPMoviePlayer.How to handle the case when user watches a 3 min video upto 2 min and moves backward to 1.30 and closes the video.The requirement is to know the fraction of video viewed by user accurately.
From the Apple docs on MPMoviePlayerController:
Movie Player Notifications
A movie player generates notifications to keep your app informed about the state of movie playback. In addition to being notified when playback finishes, your app can be notified in the following situations:
When the movie player begins playing, is paused, or begins seeking forward or backward
Using these notifications, you could set your own timers to know the total amount of time that a video has been playing. Specifically, you probably want the MPMoviePlayerPlaybackStateDidChangeNotification.
Knowing the total percentage of the video watched could be a little trickier but still possible I think. You would need to register for the MPMediaPlayback protocol and use it in conjunction with the PlaybackStateDidChangeNotification mentioned above.
One idea I had (though probably not the best or most efficient approach) would be to create an array of BOOL values, 1 for each second of the video. When a video plays, grab the currentPlaybackTime on the player and mark off each second as it is played. If the video state changes (pause, skip forward, etc), stop marking them off until it is resumed, then start at that new index based on the new currentPlaybackTime and continue marking. When they're finished, calculate the % of indexes that have been marked.
MPMoviePlayerController
MPMediaPlayback Protocol
Let me know if this works for you!!
In Avplayer, we apply the rate for video slow motion and negative frame also. if video ends and apply the rate to avplayer the video plays very fastly instead of given rate. i was struggled more than 2 days. Thanks
Try to call [avPlayer setRate:0.3] and [avPlayer play] at the same time, so the second call override the first call, which sets the rate to be 0 (normal).
The method setRate: already set the rate for the player and play the video already