尝试播放(使用 MPMoviePlayerViewController)我下载并存储在文件中的视频/歌曲时出现问题

发布于 2024-10-10 09:42:13 字数 1088 浏览 2 评论 0原文

这是我的问题。

我有一个 MPMoviePlayerViewController 可以播放网络上的一些视频。那部分有效。

但为了稍后在没有互联网连接的情况下播放它们,我将它们与那段代码一起存储在手机上。

    NSData * data = [NSData dataWithContentsOfURL:[self dataURL]];

    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;

    [data writeToFile:[baseDocumentPath stringByAppendingPathComponent:_itemId] 
       atomically:YES];

这部分没问题,如果我从手机中取出它们,我可以在 iMac 上播放这些文件。

但在那之后,当我这样做时,

    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
    videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[baseDocumentPath stringByAppendingPathComponent:file.itemId]]];

模态视图控制器中只有一个灰色的窗口。而且我没有收到玩家的任何通知。

有什么想法吗?

谢谢。

Here is my problem.

I have a MPMoviePlayerViewController that play some videos wich are on the web. That part works.

But in order to play them later, without internet connection, I store them on the phone with that piece of code

    NSData * data = [NSData dataWithContentsOfURL:[self dataURL]];

    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;

    [data writeToFile:[baseDocumentPath stringByAppendingPathComponent:_itemId] 
       atomically:YES];

That part is ok, I can play the files on my iMac if i take them from the phone.

But after that when i do

    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * baseDocumentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;
    videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[baseDocumentPath stringByAppendingPathComponent:file.itemId]]];

There is just a gray Window in the modal viewController. And i get no notifications from the player.

Any ideas?

Thanks.

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

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

发布评论

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

评论(2

聆听风音 2024-10-17 09:42:13

我今天遇到了同样的问题。

iOS 似乎不会加载任何文件扩展名错误的媒体文件。恕我直言,这是非常愚蠢的行为,因为我使用随机名称(UUID)存储媒体文件。

一个快速的解决方法是使用以下代码创建原始文件的符号链接并为其指定正确的扩展名。现在 iOS 会愉快地加载该文件。

// Create a symlink for iOS as it won't load any files with the wrong extension
NSString *fixedFileName = [fileName stringByAppendingString:@".mp4"];
[[NSFileManager defaultManager] createSymbolicLinkAtPath:fixedFileName 
                                     withDestinationPath:fileName error:NULL];

希望有帮助。如果符号链接已经存在,我们只是忽略发生错误的事实。

I came across this very same problem today.

It seems that iOS won't load any media files that have the wrong file extension. IMHO this is pretty stupid behavior, as I'm storing my media files with random names (UUIDs).

A quick workaround was to use the following code to create a symlink to the original file and give it the correct extension. Now iOS will happily load the file.

// Create a symlink for iOS as it won't load any files with the wrong extension
NSString *fixedFileName = [fileName stringByAppendingString:@".mp4"];
[[NSFileManager defaultManager] createSymbolicLinkAtPath:fixedFileName 
                                     withDestinationPath:fileName error:NULL];

Hope that helps. We simply ignore the fact that an error occurred, in case the symlink already exists.

壹場煙雨 2024-10-17 09:42:13

有人找到了导致问题的原因。

文件名没有扩展名(如 .mp4),因此 MPMovieController 不会尝试读取它(这对我来说听起来很疯狂 -_- )。如果我手动将 .mp4 添加到我的视频文件中。应用程序可以读取它...我要将每个文件的扩展名附加到其名称中。

无论如何,谢谢:)

Someone found what causes the problem.

The file name has no extension (like .mp4) so the MPMovieController doesn't try to read it (that sounds crazy to me -_- ). If I manually had .mp4 to my video file. the app can read it... I'm gonna append the extension of each file to its name.

Thanks anyway :)

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