开发者

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

开发者 https://www.devze.com 2023-02-09 04:19 出处:网络
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,

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.


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
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消