AVPlayer - switching quality during playback

I use AVPlayer to play videos on YouTube, for each YouTube video ID I get a couple of stream URLs in different qualities.

I want to reproduce a specific stream quality according to the state of the network. For example, if the user is connected to 3G, I want to play the URL of the lowest quality, but if the user goes to Wi-Fi, I want to seamlessly switch to the stream of the best quality.

This is nothing new, youtube does it in its application and many others.

So I wonder what is the best way to do this type of switching using AVPlayer , I don’t want the user to notice the possibility of switching as much as possible without pausing playback or buffering the video.
Any tips?

I'm not sure if this feature is supported on youtube servers or if I need to do this on the client side.

+6
source share
3 answers

You should take a look at the Apple documentation on HTTP streaming.

The only way to achieve the type of switch you are talking about and referred to in the documentation is to use the m3u index files and TS files containing video data.

You connect to the index file and save its contents, which will contain multiple URLs along with bandwidth requirements. See examples here . Then use the Reachability class to check the status of the network and connect to the appropriate stream. Launch the reachability notification tool and respond to events by changing the flow to which you are connected. This will cause the TS file that belongs to the stream loaded and buffered for playback to achieve the desired switch type.

As I said, the downside is the requirement to use TS files. This would mean that you would need to upload video files from Youtube prepared using the mediafilesegmenter command line tool provided by Apple and stored on an FTP server! Not perfect, but as far as I know, the only way to do this.

+8
source

Check the AVPlayer replaceCurrentItemWithPlayerItem: method. If I were you, I would use Reachbility to monitor the state of the user's network. When network reachability degrades, you can do something like this:

 AVPlayerItem *item = [AVPlayerItem playerItemWithURL:urlOfLowerQuality]; [item seekToTime:player.currentTime]; [player replaceCurrentItemWithPlayerItem:item]; 
+2
source

Use the ReachabilityManager to check the current status of a data type of either Wi-Fi or 3G. According to the type of data transfer URL. If the switch URL takes the current video time and needs to set the video search time.

0
source

Source: https://habr.com/ru/post/972346/


All Articles