Playing a video from live stream while also saving the stream to disk on iOS

bompf picture bompf · Aug 13, 2012 · Viewed 8.3k times · Source

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?

Answer

bitmapdata.com picture bitmapdata.com · Aug 13, 2012

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];