I would like to know if it's possible to play a video from an NSData object... with the MPMoviePlayerController.
Asked
Active
Viewed 1.3k times
11

ROMANIA_engineer
- 54,432
- 29
- 203
- 199

Sergio Andreotti
- 913
- 2
- 8
- 26
-
Where do you get the data from? – zoul Feb 17 '11 at 08:48
-
from my db... not from bundle... – Sergio Andreotti Feb 17 '11 at 09:04
-
Just a heads up for the future reader, the MPMoviePlayerController has been deprecated ... https://developer.apple.com/documentation/mediaplayer/mpmovieplayercontroller – multitudes Nov 04 '21 at 11:17
4 Answers
14
Ben's answer works perfectly on simulator but wont work on the device, You cannot write anywhere on the device. Check code below
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myMove.mp4"];
[videoData writeToFile:path atomically:YES];
NSURL *moveUrl = [NSURL fileURLWithPath:path];
player = [[MPMoviePlayerController alloc]init];
[player setContentURL:moveUrl];
player.view.frame = viewPlayer.bounds;
[viewPlayer addSubview:player.view];
[player play];

drexsien
- 882
- 3
- 12
- 31
-
1
-
7I'm wondering if there's any way to do this without writing to a file? – Hope4You Dec 31 '12 at 14:50
-
[[NSFileManager defaultManager] createFileAtPath:videoPath contents:videoData attributes:nil]; – iC7Zi Sep 09 '13 at 03:47
0
It is better to use NSFileManager in this case instead writeToFile
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myMove.mp4"];
[[NSFileManager defaultManager] createFileAtPath:path contents:videoData attributes:nil];
NSURL *moveUrl = [NSURL fileURLWithPath:path];
player = [[MPMoviePlayerController alloc]init];
[player setContentURL:moveUrl];
player.view.frame = viewPlayer.bounds;
[viewPlayer addSubview:player.view];
[player play];

iC7Zi
- 1,528
- 2
- 15
- 21
0
As far as I know this is not possible. If the data comes from your DB, can you save it into a temporary file and play that?

zoul
- 102,279
- 44
- 260
- 354
-
yes but I would like to avoid this... ;) I'm trying to do this with QTKit framework now... – Sergio Andreotti Feb 17 '11 at 09:34
-4
create a file with the type of your NSData for example if your NSData is type of mp4 create a file with that type - for example - "myMove.mp4"
copy past the file to your app resurces
add this code
NSData *mediaData; //your data NSString *movePath=[[NSBundle mainBundle] pathForResource:@"myMove" ofType:@"mp4"]; [mediaData writeToFile:movePath atomically:YES]; NSURL *moveUrl= [NSURL fileURLWithPath:movePath]; MPMoviePlayerController *movePlayer=[[MPMoviePlayerController alloc]init]; [movePlayer setContentURL:moveUrl]; [movePlayer play];
-
this won't work on the device, you cant write files anywhere on the device. Check out my answer bellow.. – drexsien Feb 17 '12 at 07:32