I've been looking into this topic for a while now and I can't really figure out how I could achieve this. Let's say we have a video from an HTTP Stream and I want to be able to play that video in my app, and at the same time the video is also persisted on the disk. I know I can play videos with the AVFoundation
or MediaPlayer frameworks and I can load the file/stream via NSURLRequest
.
These are two different processes though, which would require loading the video two times. I can't figure out if and how I can combine these two. Is this possible/feasible?
It's Possible. However, for the purpose of your YouTube video if you like I do not want to recommend. becauseof following Youtbe TOS.
For the following reasons:
22.4
We found that your app contains information that may allow its users to download YouTube content, which is not in compliance with the YouTube Terms of Service.
"...You shall not download any Content unless you see a "download" or similar link displayed by YouTube on the Service for that Content. You shall not copy, reproduce, distribute, transmit, broadcast, display, sell, license, or otherwise exploit any Content for any other purposes without the prior written consent of YouTube or the respective licensors of the Content. YouTube and its licensors reserve all rights not expressly granted in and to the Service and the Content."
also refer following site question. that's very important. you will definitely need to read.
http://www.iphonedevsdk.com/forum/business-legal-app-store/88260-youtube-downloader.html
Anyway, now I'll tell you about how to implement them.
a. Streaming Service is not difficult to implement. According to Apple's sample code can be easily implemented. Please note the following. this using a AVPlayer. at 3G, Wifi i tested. it' fine. how to test? upload to video your server or get video link. and copy and paste in source code xib textField.
StitchedStreamPlayer
b. Video downloads are available at the same time. I will recommend AFNetworking. ARC support for it. so if apps state is the background, download continue.
AFNetworking
How to Download?
NSURLRequest *request = [NSURLRequest requestWithURL:"your_http_video_url" cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20];
AFURLConnection *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:"your_save_video_name.mp4"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long
totalBytesRead, long long totalBytesExpectedToRead)
{
//do stuff. about upadte progressbar...
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"downloadComplete"); // show alertView to user.
}];
[operation start];
You should check out this excellent answer:
https://stackoverflow.com/a/23754994/1509392
which explains how this can be done with AVURLAsset and providing data to the resourceLoader object. The idea is that the resourceLoader will ask your delegate for the media data, which you can then download from the source and save to disk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With