How to check whether a video or audio file format is playable in iPhone programmatically?

China☆狼群 提交于 2019-12-08 06:28:07

问题


I have some audio and video files stored in a server. I need to play them, but I do not know the files stored in server supported by iphone to play,

How can I check whether the file is supported by IPhone before start playing, I am using MPMoviePlayerController to play the files.


回答1:


Before playing them, its not possible to identify. Movie player will throw error when incorrect format of file is passed to it. For that, first you have to observe for noticiation: MPMoviePlayerLoadStateDidChangeNotification as following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

When you catch this notification, check for players load state. If its loadState is "MPMovieLoadStateUnknown", then you can say that some error occured while playing the movie/audio.




回答2:


You can create an AVAsset from the file like so:

NSURL *myURL = [NSURL URLWithString:@"http://www.myserver.com/myfile.mp4"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:myURL];

You can then check its playable property:

if (asset.playable) {
    //Do Something
}


来源:https://stackoverflow.com/questions/4920197/how-to-check-whether-a-video-or-audio-file-format-is-playable-in-iphone-programm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!