如何在 iOS 上使用 MPMoviePlayerController 或 AVPlayer 播放没有文件扩展名的电影文件?
我想在 iPad 上播放 iOS 4.3 中的电影。当文件名具有文件扩展名时,我已成功使用 MPMoviePlayerController 和 AVPlayer 从远程 URL 加载文件。然而,当我使用不返回文件名(只是一个无法猜测的随机名称)的 CDN 时,MPMoviePlayerController 或 AVPlayer 似乎都无法应对。
有没有办法告诉任一玩家这确实是一部 x 类型的电影并且应该继续播放?
MPMoviePlayerController 将从更改状态通知中返回以下错误:
{
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 1;
error = "Error Domain=MediaPlayerErrorDomain Code=-12847 \"This movie format is not supported.\" UserInfo=0x5b60030 {NSLocalizedDescription=This movie format is not supported.}";
}
我知道该文件是有效的 m4v 文件,因为当我重命名它时一切都很好。
I want to play a movie in iOS 4.3 on the iPad. I've successfully used MPMoviePlayerController and AVPlayer to load files from a remote URL when the filename has a file extension. However, when I use a CDN that doesn't return the filename (just an un-guessable random name), neither MPMoviePlayerController or AVPlayer seem to be able to cope.
Is there a way to tell either player that it really is a movie of type x and it should just get on playing it?
MPMoviePlayerController will return the following error from it's changed state notification:
{
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 1;
error = "Error Domain=MediaPlayerErrorDomain Code=-12847 \"This movie format is not supported.\" UserInfo=0x5b60030 {NSLocalizedDescription=This movie format is not supported.}";
}
I know that file is a valid m4v file, as when I rename it all is fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
文件位于 tmp
创建符号链接
通过 slink 播放视频
File at tmp
Create symlink
play video by slink
如果播放器无法猜测文件格式,您需要检查 CDN 是否发送回正确的 MIME 类型。我的猜测是您的 CDN 无法正确猜测 mimetype,播放器也无法正确猜测。
在大多数情况下,这是由于 CDN 呈现 HTTP 标头的方式造成的。 检查“Content-Type”标头是否设置为与您的内容匹配的视频格式。
If the player can't guess the file format you need to check that the CDN sends the right mime type back. My guess is that your CDN can't guess the mimetype correctly nor can the player.
In most cases this is due to how the CDN presents the HTTP header. Check that the "Content-Type" header is set to a video format matching your content.
WebKit 通过 Private AVURLAsset 选项来处理此问题:
AVURLAssetOutOfBandMIMETypeKey
,当您在 HTML 的video
标记中指定 MIME 类型时使用此选项,您可以使用此选项,例如:
由于它是私钥,因此如果您计划将其提交到 AppStore,您可能需要对其进行混淆。
WebKit 源代码可以在此处找到:
https://opensource.apple.com/source/WebCore/WebCore-7604.1.38.1.6/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm.auto.html
WebKit handle this by a Private AVURLAsset option:
AVURLAssetOutOfBandMIMETypeKey
, this option is used when you specify a MIME type in the HTML'svideo
tag,You can use this option like:
Since it is a private key, you may want to obfuscate it if you plan to submit it to AppStore.
The WebKit source can be found here:
https://opensource.apple.com/source/WebCore/WebCore-7604.1.38.1.6/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm.auto.html
最后,我找到了答案。
您应该使用 AVURLAsset(AVAsset 的子类)并在选项输入中设置 MIMEType:
Source -> https://stackoverflow.com/a/54087143/6736184
Finally, I found the answer.
You should use AVURLAsset (the subclass of AVAsset) and set the MIMEType in the options input :
Source -> https://stackoverflow.com/a/54087143/6736184
iPhone 支持 .mp4、.m4v、.mov 格式的视频 H.264、MPEG-4 以及 AAC、MP3、M4a、Apple 无损和 Audible 格式的音频文件。
您可以使用 NSFileManager 的 -contentsOfDirectoryAtPath:error: 方法来获取包含目录内容的数组(作为字符串)。然后您只需执行字符串操作即可。
iPhone support video H.264, MPEG-4 in .mp4, .m4v, .mov formats and audio files in AAC, MP3, M4a, Apple lossless and Audible.
You can use NSFileManager's -contentsOfDirectoryAtPath:error: method to get an array with the contents of a directory (as strings).Then you just do strings operations .
迪伦是对的。
MPMoviePlayer 和 AVPlayer 都需要文件扩展名才能从 URL 播放文件,否则将显示错误消息。最好使用某种技巧。
Dylan is correct.
Both MPMoviePlayer and AVPlayer needs a file extension in order to play the file from URL otherwise an error message will be shown. Better to use some kind of tricks.
如果您在获取连接的 ContentType 时遇到问题,您可以循环浏览可播放的 MIME 类型,并创建指向具有扩展名的实际文件的符号链接,然后检查它们是否可以播放。就像这样:
If you have problems to get the ContentType of your connection you could cycle through the playable MIME types and create symbolic links to the actual file with the extension and check if they are playable. Like so:
这有点像黑客,但您可以做的是通过一个方法运行每个名称,该方法检查后面是否有三个字符的句点。如果没有,只需自动附加 .m4v。或者获取 MIME 类型并根据返回的类型自动附加扩展名。如果有的话。使用 NSString 查找文档以获取更多信息。祝你好运!让我知道这是否有帮助。
It's sort of a hack, but what you could do is run each name through a method that checks for a period with three characters after it. If not, just append .m4v automatically. Or get the MIME type and append an extension automatically based on the returned type. If available. Look up documentation with NSString for more info. Good luck! Let me know if that helped.