AVPlayer - switching stream quality while playing - ios

I'm using AVPlayer to play youtube videos, for each youtube video id I retrieve a couple of stream urls in different qualities.
I want to play a particular stream quality according to the network state. For example if user is on 3G I want to play the lowest quality URL but if user moves to wifi I want to seamlessly switch to the better quality stream.
This is nothing new, youtube is doing that in their app and many others.
So I wonder what is the best way to do this kind of switching with AVPlayer, I don't want the user to notice the switching as possible, without pausing the video playback or buffering.
Any advices?
I'm not sure if this kind of functionality is supported on the youtube servers or if I need to do it on client side.

You should have a look at the Apple documentation on HTTP live streaming.
The only way to achieve the type of switching that you want and is talked about in the documentation is the use of m3u index files and TS files containing the video data.
You connect to the index file and store its contents, which will be multiple URL's along with bandwidth requirements. See the examples here. Then use the
Reachability class to check network status and connect to the appropriate stream. Start the Reachability notifier and react to events by changing the stream you're connected to. This will cause the TS file that belongs to the stream to be downloaded and buffered for playback, achieving the type of switching you want.
As I previously said, the drawback is the requirement to use TS files. This would mean you video files would have to be downloaded from Youtube, prepared using the Apple provided mediafilesegmenter command line tool and the stored on an FTP server! Not ideal at all but as far as I'm aware the only way to do this.

Check out the AVPlayer replaceCurrentItemWithPlayerItem: method. If I were you, I would use Reachbility to observe the user's network status. When the network reachability degrades, you can do something like this:
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:urlOfLowerQuality];
[item seekToTime:player.currentTime];
[player replaceCurrentItemWithPlayerItem:item];

Use ReachabilityManager to check current status of data type either wifi or 3G. According to data mode switch url type.While Switching url take current time of video and need to set seek time of video.

Related

ios Can I use MPMoviePlayerController to play .mp3 url

My app needs to play some music files, like .mp3. I would like to use MPMoviePlayerController because it has implemented all the UI stuff for me, i.e. I do not want to bother implementing progress slide bar and things like this.
I tested to use it to play a .mp3 file and it worked fine but I do not know if it is fine to use it to do this because its name says "movie player" and it seems it is supposed to play a movie. Would apple reject this? Thank you.
For playing audio from a file or memory, AVAudioPlayer is your best option but unfortunately it doesn't support a network stream while MPMoviePlayerController can
From documentation :
An instance of the AVAudioPlayer class, called an audio player,
provides playback of audio data from a file or memory.
Apple recommends that you use this class for audio playback unless you
are playing audio captured from a network stream or require very low
I/O latency.
For the Apple validation I don't think that your application can be rejected because you're using the Media Player Framework to play an audio file. In fact here they explicitly say that you can do just that:
Choose the right technology for your needs:
To play the audio items in a user’s iPod library, or to play local or
streamed movies, use the Media Player framework. Classes in this
framework automatically support sending audio and video to AirPlay
devices such as Apple TV.
Not sure about performance and memory issues though!
Best of luck.

upload video stream and audio stream on iOS

Here's the needs: People record their video via my App and upload stream to our website, you know, like a live show.
When they are recording, the network goes bad, then record audio instead of video.
And when audience watch, first comes the video, in a certain time comes the audio with a static picture.
How am I supposed to do that? Thanks.
I got this after thinking. So I'll answer by my self.
If you wanna record video and audio separately. Use lower level Library to handle this. Then upload it. And you can get the upload speed, When the speed goes low, you can shut down the video upload process.

Play youtube video in offline mode?

I want to play YouTube video when we redirect from internet area to non internet area.
Like I am having internet access at some point and I played Youtube video, and after some
times my internet connection is loosed, but now I want to play all those video which I played
previously.
I am not going to write any code for this but only tell you the route you probably need to take. As with any online media, If you don't have access to the internet, well you can't really get the data. What you need to do is capture the data which is coming in from any media and save it on the device. When the user then clicks on a video which has been previously saved while not connected to the internet, you can play the saved video file. There isn't really a simple quick way to do this, just do some research on saving streaming video or downloading video and saving to device.
I am not aware of any caching mechanism for a browser that could do that 'off the box'. This wouldn't make much sense anyway because the cache would get full with only a few videos.
The only way that comes to mind is to download the video of interest while connected and play it with your favorite media player. There's bound to be a few addons depending on the browser you use, I use VideoDownloadHelper for Firefox but for iPhone this blog seems to have some instructions.

Youtube API Java client library - How check if a video entry is available for mobile?

I have a content management server application written in Java. A background process goes through a list of video ids and fetches the details for those video ids using Youtube API.
I would like to check if a particular video entry is available for mobile or not.
I checked syndicate allowed like
String videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/"+videoID;
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class);
if(!videoEntry.getXmlBlob().getBlob().contains("yt:accessControl permission='denied' action='syndicate'")){
System.out.println("The video is syndicatable");
}
Checking for syndicate still not solved the problem and the server still lets in videos that cannot play on Android phone.
What is the right way to filter only the videos that can be played on mobile?
There's no single check to see whether a video is playable "on mobile".
There are a variety of different reasons why a particular video might not be playable on a particular platform, and unfortunately the only way to be absolutely sure whether a particular video will play in a particular player is to attempt to play it.
That being said, this blog post goes into more details about the types of common playback restrictions that crop up: http://apiblog.youtube.com/2011/12/understanding-playback-restrictions.html

Play mp3 from web with MPMoviePlayerController over 3G

In my app I use MPMoviePlayerController to play an mp3 file from a web server. This plays while downloading the whole file, which is fine over WiFi. But now I want it to work over 3G (and get it into the app store). How do I get it to just buffer the next 10 seconds or so (as per apple rules)? I'm digging through the documentation on AVPlayer, HTTP Live streaming, etc, but I'm still confused about the best way to do this. With so many podcast apps out there, I'm suprised there aren't more tutorials/libraries about it.
Thanks for your time.
I investigated this as well, and I was not able find a way to limit the look-ahead buffer using MPMoviePlayerController. I believe you would have to load chunks at the network layer and feed them in at the AVFoundation layer, but I have not attempted this myself.
That said, I can confirm that you can get an app approved that plays mp3 files using MPMoviePlayerController over both WiFi and 3G connections. In my app I added a setting so the user can decide whether to enable mp3 downloads over 3G or not, although I don't know if that was needed to get approved. I provided it so users didn't inadvertently incur bandwidth costs.

Resources