ios - MPMoviePlayerViewController 从 NSData (MP4) 播放

标签 ios mpmovieplayercontroller nsdata mp4

NSString *p = [app getFilePathFor:name];
// Printing description of p:
// /Users/Jackson/Library/Application Support/iPhone Simulator/5.0/Applications/7FE97EDD-D080-4E5E-8EDD-3D9FFA8F6CF5/Documents/6681a4d7-8630-4a12-90c6-f95d72650a42

NSData *data = [NSData dataWithContentsOfFile:p];
// Printing description of data:
// <01401dc2 a15ebc27 e473ee79 316568ff 19dc7000 df3f7c5c dc206870 01a1c00e 00010000 00000000 00e10288 00000003 01060020 08c80038 00080000 88edffbf 02000119 0000050e 00000000 00000000 02000007 0000050f 00000000 00000000>

NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
// Printing description of str:
// @¡^¼'äsîy1ehÿÜp

path = [NSURL fileURLWithPath:str];
// Printing description of path:
// %01@%1D%C3%82%C2%A1%5E%C2%BC'%C3%A4s%C3%AEy1eh%C3%BF%19%C3%9Cp%00%C3%9F%3F%7C%5C%C3%9C%20hp%01%C2%A1%C3%80%0E%00%01%00%00%00%00%00%00%00%C3%A1%02%C2%88%00%00%00%03%01%06%00%20%08%C3%88%008%00%08%00%00%C2%88%C3%AD%C3%BF%C2%BF%02%00%01%19%00%00%05%0E%00%00%00%00%00%00%00%00%02%00%00%07%00%00%05%0F%00%00%00%00%00%00%00%00 -- file://localhost/

然后我将路径发送到 MPMoviePlayerViewController,如下所示:

MPMoviePlayerViewController * playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:path];

[self presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)playerController];

playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
playerController=nil;

此时,电影播放器​​开始显示,然后在“显示”动画完成后立即隐藏。在日志中我得到这个:

An instance 0x87ca7b0 of class AVPlayerItem was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x87dfc30> (
<NSKeyValueObservance 0x933c280: Observer: 0x93367d0, Key path: nonForcedSubtitleDisplayEnabled, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x933c630>
<NSKeyValueObservance 0x933c360: Observer: 0x93356b0, Key path: presentationSize, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x87b53b0>

我不知道下一步该去哪里..帮忙?

** 编辑 **

我正在尝试播放的 MP4 来自 this buckbunny test clip .当我以 URL 的形式直接加载剪辑时,它会起作用。但是,如果我下载它并通过 NSData 保存它并尝试重新打开 NSData,这将不再有效。

** 编辑 2 **

@Till: "你正在用乱码字符串初始化 URL;//str=@"@¡^¼'äsîy1ehÿÜp"path = [NSURL fileURLWithPath:str]; 什么你在这儿等吗?”

既然我已经尝试过了:

path = [NSURL fileURLWithPath:[app getFilePathFor:name]];
// Printing description of path:
// file://localhost/Users/Jackson/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/CB1CA8D0-B5EC-4B5F-8C7F-0EF449508BA7/Documents/9d425856-14b0-4ad3-835a-970affacb05f

我仍然遇到完全相同的错误。

最佳答案

为什么要从保存的剪辑文件转换为 nsdata?

您应该只在您的应用程序文档目录中加载剪辑的路径。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:name];

NSURL *fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];

yourMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];

关于ios - MPMoviePlayerViewController 从 NSData (MP4) 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10374543/

相关文章:

iphone - 在视频之间快速切换的正确方法

ios - 在 ios 中将 nsMutablestring 转换为 nsdata

ios - 检查是否-[NSData writeToFile :atomically:] is done

iOS 限制 - 无法弄清楚如何

iphone - 使用UISlider更改音量时如何隐藏Iphone音量框?

iphone - 在窗口模式下播放视频

iphone - "The operation couldn’ t 完成。 ( cocoa 错误 512。)”

objective-c - iOS MapKit用户位置标注之Z-index

ios - cocos2d Sprite 表消耗大量内存

ios - 本地化 MPMoviePlayerViewController 的完成按钮