Oskar Pålsson
Oskar Pålsson

Reputation: 129

Play video stored in NSData

I am trying to play a video that is stored in an NSData object. I save the file to my applications temp folder and then try to play it, but all I get is a black screen. I can later browse to that folder and play the file, so I know that the file gets written and is supported.

This is my code:

NSString *urlString = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.m4v"];
NSURL *url = [[NSURL alloc] initWithString:urlString];

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];
[moviePlayer prepareToPlay];
moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer play];

The only thing I can think of is that it takes some time for the file to get written, and the player tries to access it before it is done. Is that possible, and if so, how do I fix it?

Upvotes: 1

Views: 2857

Answers (1)

rckoenes
rckoenes

Reputation: 69469

When working with files you should tell the NSURL that the URL should point to a file:

NSString *urlString = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.m4v"];
NSURL *url = [NSURL fileURLWithPath:urlString];

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];
[moviePlayer prepareToPlay];
moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer play];

Upvotes: 3

Related Questions