How to play video from NSData

13,026

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];
Share:
13,026

Related videos on Youtube

Sergio Andreotti
Author by

Sergio Andreotti

Updated on May 20, 2022

Comments

  • Sergio Andreotti
    Sergio Andreotti about 2 years

    I would like to know if it's possible to play a video from an NSData object... with the MPMoviePlayerController.

  • Sergio Andreotti
    Sergio Andreotti over 13 years
    yes but I would like to avoid this... ;) I'm trying to do this with QTKit framework now...
  • drexsien
    drexsien over 12 years
    this won't work on the device, you cant write files anywhere on the device. Check out my answer bellow..
  • drexsien
    drexsien over 12 years
    ops! i cant believe i just answered a one year old question...:)
  • Hope4You
    Hope4You over 11 years
    I'm wondering if there's any way to do this without writing to a file?
  • Zeeshan
    Zeeshan almost 11 years
    [[NSFileManager defaultManager] createFileAtPath:videoPath contents:videoData attributes:nil];
  • user523234
    user523234 about 9 years
    What are the advantages using createFileAtPath vs writeToFile?