我将“.mov”文件保存到带有数据的图像选取器控制器的托管Parse数据库中:
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *data = [NSData dataWithContentsOfURL:videoURL];
我稍后检索该文件的NSData,并希望让用户使用MPMoviePlayerController播放视频。有没有办法在不编写文件的情况下执行此操作?我宁愿只在一个完成后释放的临时位置播放它......
这是我的代码,但是当视图显示时没有视频播放......
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"temp.mov"];
[self.videoData writeToFile:outputPath atomically:YES];
NSURL *moveUrl = [NSURL fileURLWithPath:outputPath];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:moveUrl];
[player prepareToPlay];
[player.view setFrame:CGRectMake(40, 197, 240, 160)];
[self.view addSubview:player.view];
[player play];
答案 0 :(得分:0)
首先,MPMoviePlayerController is deprecated in iOS9
如果您确实想要使用它(不推荐),则必须使用initWithContentURL:
进行初始化
在您的情况下,最佳目录可能是tmp目录,请参阅here。
答案 1 :(得分:0)
在您的问题中,您首先获取数据,然后播放视频。您可以根据主机的带宽成本对其进行流式传输。
这是一个将视频播放器添加到UIView层的Swift技术,它在通知选择器中完成后循环。 (如果你不想转换它,请检查如何将Swift与Objective-C一起使用)
导入AVFoundation
var videoPlayer: AVPlayer!
var isMoviePlaying: Bool = false
func playVideo(video: Video, autoplay: Bool)
{
if !isMoviePlaying
{
isMoviePlaying = true
var url:NSURL = NSURL(string: videoUrl)!
startActivityIndicator()
let playerItem = AVPlayerItem(URL: url)
self.videoPlayer = AVPlayer.playerWithPlayerItem(playerItem) as! AVPlayer
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name:"AVPlayerItemDidPlayToEndTimeNotification", object: videoPlayer.currentItem)
let layer = AVPlayerLayer(player: self.videoPlayer)
self.videoPlayer.actionAtItemEnd = .None
layer.frame = CGRectMake(0, 0, self.frame.size.width * 0.95, self.frame.size.height * 0.85)
layer.videoGravity = AVLayerVideoGravityResizeAspect
self.videoView.layer.addSublayer(layer)
stopActivityIndicator()
self.videoPlayer.play()
}
}
func playerItemDidReachEnd(notification: NSNotification)
{
// loop the video
if let playerItem: AVPlayerItem = notification.object as? AVPlayerItem
{
playerItem.seekToTime(kCMTimeZero)
}
}