I want to play a video , which format is video.h264
I have tried with AVPlayer
and MPMoviePlayerViewController
But, I failed to play. Is there an way to do this. Any other library available for achieve the same
I want to play a video , which format is video.h264
I have tried with AVPlayer
and MPMoviePlayerViewController
But, I failed to play. Is there an way to do this. Any other library available for achieve the same
First, read this post. Possible Locations for Sequence/Picture Parameter Set(s) for H.264 Stream
A .h264 file is in annex b format. Next read this:
https://developer.apple.com/reference/videotoolbox/vtdecompressionsession
Convert from annex b to avcc, set extradata when opening the decompression session. Decode frames -> display frames.
Here is some code that does just that:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = self.view;
NSString *resourceName = @"SunSpot_1080p_main.m4v";
NSString* movieFilePath = [[NSBundle mainBundle]
pathForResource:resourceName ofType:nil];
NSAssert(movieFilePath, @"movieFilePath is nil");
NSURL *fileURL = [NSURL fileURLWithPath:movieFilePath];
AVPlayerViewController *playerViewController =
[[AVPlayerViewController alloc] init];
playerViewController.player =
[AVPlayer playerWithURL:fileURL];
self.avPlayerViewcontroller = playerViewController;
[self resizePlayerToViewSize];
[view addSubview:playerViewController.view];
view.autoresizesSubviews = TRUE;
}
The full source code and github link can be found at my blog post on the subject. This new player view controller is very easy as compared to previous APIs, there really is nothing to it.
Unless something has changed recently (which is always possible in the video world...) iOS does not support 'raw' h264 video playback - e.g. MyVideoFile.h264.
Its worth mentioning that a video is typically wrapped like this when it gets to an iOS device, if it is being streamed:
If your video is short you may just have to package it into an mp4 container and it should play back then - i.e. create MyVideoFile.mp4 from your original file.
If it is longer, and you are streaming it, you will generally have to also have to use the HLS streaming format as this is one of Appel's rules (see https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/UsingHTTPLiveStreaming/UsingHTTPLiveStreaming.html#//apple_ref/doc/uid/TP40008332-CH102-SW5):
If your app delivers video over cellular networks, and the video exceeds either 10 minutes duration or 5 MB of data in a five minute period, you are required to use HTTP Live Streaming. (Progressive download may be used for smaller clips.)