satya
satya

Reputation: 241

how to play video using video data in iOS

I am getting video data from server. i am using signalR Framework. first of all i am capturing video from iPhone photo library. Then next i am converting encoding format (base64EncodedStringwithOptions) method. next i am sending video encrypted data to signalR server. and next i am receiving video data and decrypted and play the video data using AVPlayer. but my problem video does not played. but i received video data from signalR. For decrypting i am using this method. (NSDataBase64DecodingIgnoreUnknownCharacters)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *videoFile=[documentDirectory stringByAppendingPathComponent:post.mediaName];

NSURL *url;

if ([[NSFileManager defaultManager] fileExistsAtPath:videoFile]) {

url=[NSURL URLWithString:videoFile];

}

NSLog(@"Video URL is:%@",url);

AVPlayer *player = [AVPlayer playerWithURL:url];

cell.videoPlayerController.player = player;

[player play];

i got Video URL like this:

Video URL is:/Users/Library/Developer/CoreSimulator/Devices/EEC96DE7-6D0F-4D80-82B8-3C24E4F6B3EF/data/Containers/Data/Applicatication video0006.mp4 

Upvotes: 0

Views: 856

Answers (1)

Ketan Parmar
Ketan Parmar

Reputation: 27428

You can use MPMoviePlayerController to play video from url' but make sure that you have to declare property for that like,

 @property MPMoviePlayerController *videoController;

don't create local object of MPMoviePlayerController.

you can use it something like,

 self.videoController = [[MPMoviePlayerController alloc] init];

    [self.videoController setContentURL:tempView.videoURL];  //here your url
    [self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.videoController.view];

    // close or cancel button if you want to put 

    UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 20, 60, 30)];

    [closeButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];

    [closeButton setTitle:@"Cancel" forState:UIControlStateNormal];

    [self.videoController.view addSubview:closeButton];

   // add observer to notify when playing will be completed if you want to put

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoPlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.videoController];

    [self.videoController play];

Update :

You must use file url like,

   NSURL *url = [NSURL fileURLWithPath:videoFile]; 

instead of

  [NSURL URLWithString:videoFile];

Upvotes: 1

Related Questions