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

2

6

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?

Ambitious answered 12/8, 2012 at 23:26 Comment(1)
I know this is an old question. However, you can find the answer here: #37611988Leboeuf
S
8

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];
Sara answered 13/8, 2012 at 0:48 Comment(1)
Hi, good answer , but if i want to play after downloaded via output stream. is it possible ?Veneer
R
1

You should check out this excellent answer:

https://mcmap.net/q/746375/-saving-buffer-data-of-avplayer

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.

Rosalindarosalinde answered 29/8, 2014 at 10:16 Comment(1)
the answer above works great for audio, but does not work for video.Xiphoid

© 2022 - 2024 — McMap. All rights reserved.