如何在iOS中使用视频数据播放视频

时间:2016-07-26 09:22:18

标签: ios objective-c iphone avplayer

我从服务器获取视频数据。我正在使用signalR Framework。首先,我正在从iPhone照片库中捕获视频。接下来我正在转换编码格式(base64EncodedStringwithOptions)方法。接下来我将视频加密数据发送到signalR服务器。然后我接收视频数据并解密并使用AVPlayer播放视频数据。但我的问题视频没有播放。但是我收到了来自signalR的视频数据。对于解密我正在使用这种方法。 (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];

我有这样的视频网址:

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

1 个答案:

答案 0 :(得分:1)

您可以使用MPMoviePlayerController播放来自网址的视频,但请确保您必须为此声明属性,

 @property MPMoviePlayerController *videoController;

不要创建MPMoviePlayerController的本地对象。

你可以使用类似的东西,

 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];

更新:

您必须使用文件网址,

   NSURL *url = [NSURL fileURLWithPath:videoFile]; 

而不是

  [NSURL URLWithString:videoFile];