MPMoviePlayer 加载并播放应用程序文档中保存的电影

发布于 2024-09-27 20:00:00 字数 413 浏览 1 评论 0原文

我正在编写一个应用程序,将照片卷中的电影存储到应用程序的本地文档文件夹中。我可以使用 MPMoviePlayer 播放远程电影,但是尝试播放存储在应用程序文档文件夹中的电影总是返回 MPMovieLoadStateUnknown

通知均从默认通知中心(MPMoviePlayerLoadStateDidChangeNotificationMPMoviePlayerPlaybackDidFinishNotification)发送和接收。 在控制台中收到 loadStateUnknown 消息后不久就会出现一个警告框,表示电影无法播放,然后应用程序会收到电影播放完成的通知。

我认为可能是找不到文件名(MPMoviePlayer只能采用URL作为资产位置)的情况。有没有人处理过这个问题或类似问题?

I am writing an application that stores the movies in the photo roll into the local documents folder for the app. I can play remote movies using the MPMoviePlayer, however trying to play a movie stored in the documents folder for the app always returns MPMovieLoadStateUnknown.

The notifications are all getting sent and received from the default notification center (MPMoviePlayerLoadStateDidChangeNotification, MPMoviePlayerPlaybackDidFinishNotification).
An alert box shows up shortly after getting the loadStateUnknown message in the console, saying that the movie could not be played and the app then receives the movie playback completed notification.

I think that it may be a case that the filename (MPMoviePlayer can only take a URL as the asset location) cannot be found. Has anyone dealt with this issue or similar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花之痕靓丽 2024-10-04 20:00:00

鉴于其他答案似乎都无法解决问题,我倾向于认为问题可能出在您正在生成的文档文件路径上。

您应该使用以下方法生成应用程序文档文件夹的路径:

NSString *userDocumentsPath = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) 
{
     userDocumentsPath = [paths objectAtIndex:0];
}

Given that none of the other answers seem to resolve the problem, I'm inclined to think that the problem might be with the Documents file path you are producing.

You should be using the following method to generate the path to the application's documents folder:

NSString *userDocumentsPath = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) 
{
     userDocumentsPath = [paths objectAtIndex:0];
}
失去的东西太少 2024-10-04 20:00:00

您是否尝试过将本地文件路径转换为 ​​url

[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",filePath]];

如果文件路径中有空格,则必须在创建 URL 之前将其转换

filePath = [filePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
filePath = [filePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

have you tried converting the local file path to a url

[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",filePath]];

if there are spaces in you file path, you will have to convert them prior to creating the URL

filePath = [filePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
filePath = [filePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
芯好空 2024-10-04 20:00:00

核心数据模板将文件路径转换为 ​​URL,如下所示:

NSURL *storeUrl = [NSURL fileURLWithPath:whatever];

这似乎执行了所有正确的空格转义等操作,因为我使用它来加载路径中带有空格的文件。

The Core Data template converts a file path to a URL like this:

NSURL *storeUrl = [NSURL fileURLWithPath:whatever];

That seems to do all of the correct space escaping and stuff because I'm using this to load a file with a space in the path.

扶醉桌前 2024-10-04 20:00:00

我不确定这是否会对您有帮助。
在本教程中,有一些代码行可能会带您走上正确的道路:

    // Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
...

您将在以下方法中找到此代码片段:

- (void) moviePlayerLoadStateChanged:(NSNotification*)notification 

这是本教程的链接:
教程

希望这会对您有所帮助...

I am not sure if this will help you.
In this tutorial there are some code lines which probably will bring you on the right path:

    // Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
...

You will find this snippet in following method:

- (void) moviePlayerLoadStateChanged:(NSNotification*)notification 

here is the link to the tutorial:
Tutorial

Hopefully this will help you...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文