1 , How to play a video
iOS provides MPMoviePlayerController, Mpmovieplayerviewcontroller two classes that can be used to easily play video and network streaming \ Network audio.
Tip: Network audio is also played using this controller. Youtobe is implemented with MPMoviePlayerController.
Mpmovieplayerviewcontroller can only play video in full screen.
All two of the above classes are defined in the MediaPlayer framework and must be added manually :
2 , MPMoviePlayerController Supported Formats
N MPMoviePlayerController
N Inherits from NSObject
N There is a view inside to show the video content
n Add the view to the other controller's view to display the video content
n MPMoviePlayerController video formats that can be played include:
N-H, MPEG-4, etc.
n supported file extensions include: Avi,mkv,mov,m4v,mp4, etc.
n can be downloaded from the Apple website: http://support.apple.com/kb/HT1425 Download Some of the video files used for testing, the files are relatively small
N Hint: MPMoviePlayerController does not support all video formats, if you want to play an unsupported video format, you need to use a third-party framework for decoding, such as VLC
N HTTPS://GITHUB.COM/VIDEOLAN/VLC
3 , MPMoviePlayerController the Use
Load Video Resources ( Note that if the URL is nil The same can be loaded ) :
Nsassert (Self.url, @ "URL cannot be empty");
[[MPMoviePlayerController alloc] initWithContentURL:self.url];
Show:
[Self.view AddSubview:self.moviePlayer.view];
By setting the Autoresizingmask property, you can automatically adjust the view size when the screen is converted vertically
Play:
[Self.movieplayer play];
Fullscreen:
[Self.movieplayer Setfullscreen:yes Animated:yes];
"Remarks" MPMoviePlayerController the playback status is monitored through the Notification center.
For example:
#import "MJMoviePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface Mjmovieplayerviewcontroller ()
@property (nonatomic, strong) MPMoviePlayerController *movieplayer;
@end
@implementation Mjmovieplayerviewcontroller
-(MPMoviePlayerController *) MoviePlayer
{
if (!_movieplayer) {
Controller responsible for controlling media playback
Nsurl *url =
[[NSBundle Mainbundle] urlforresource:@ "Promo_full.mp4" withextension:nil];
_movieplayer =
[[MPMoviePlayerController alloc] initwithcontenturl:url];
_movieplayer.view.frame = Self.view.bounds;
_movieplayer.view.autoresizingmask =
uiviewautoresizingflexiblewidth| Uiviewautoresizingflexibleheight;
[Self.view Addsubview:_movieplayer.view];
}
return _movieplayer;
}
-(void) viewdidload
{
[Super Viewdidload];
[Self.movieplayer play];
}
@end
Operation Result:
4 , Common listener Notification Events
Status change:
Mpmovieplayerplaybackstatedidchangenotification
End of playback:
Mpmovieplayerplaybackdidfinishnotification
Exit Full Screen:
Mpmovieplayerdidexitfullscreennotification
Screenshot Complete:
Mpmovieplayerthumbnailimagerequestdidfinishnotification
Screenshot method:
-requestthumbnailimagesattimes:timeoption:
For example:
#import "MJMoviePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface Mjmovieplayerviewcontroller ()
@property (nonatomic, strong) MPMoviePlayerController *movieplayer;
@end
@implementation Mjmovieplayerviewcontroller
-(MPMoviePlayerController *) MoviePlayer
{
if (!_movieplayer) {
Controller responsible for controlling media playback
_movieplayer = [[MPMoviePlayerController alloc] InitWithContentURL:self.movieURL];
_movieplayer.view.frame = Self.view.bounds;
_movieplayer.view.autoresizingmask = Uiviewautoresizingflexiblewidth | Uiviewautoresizingflexibleheight;
[Self.view Addsubview:_movieplayer.view];
}
return _movieplayer;
}
-(void) viewdidload
{
[Super Viewdidload];
[Self.movieplayer play];
[Self addnotification];
}
-(void) Viewdidappear: (BOOL) animated
{
Self.moviePlayer.fullscreen = YES;
}
#pragma mark-Add notification
-(void) addnotification
{
1. Add listening for playback status
Nsnotificationcenter *NC = [Nsnotificationcenter defaultcenter];
[NC addobserver:self selector: @selector (statechanged) Name:
Mpmovieplayerplaybackstatedidchangenotification Object:nil];
2. Play complete
[NC addobserver:self selector: @selector (finished) name:
Mpmovieplayerplaybackdidfinishnotification Object:nil];
3. Fullscreen
[NC addobserver:self selector: @selector (finished) name:
Mpmovieplayerdidexitfullscreennotification Object:nil];
4. Screenshot completion Notification
[NC addobserver:self selector: @selector (capturefinished:) Name:
Mpmovieplayerthumbnailimagerequestdidfinishnotification Object:nil];
How much time in the array, just a few times
Mpmovietimeoptionexact Precision
Mpmovietimeoptionnearestkeyframe probably
[Self.movieplayer requestthumbnailimagesattimes:@[@ (5.0), @ (20.0)] timeoption:mpmovietimeoptionnearestkeyframe];
}
-(void) capturefinished: (nsnotification *) notification
{
NSLog (@ "%@", notification);
[Self.delegate Movieplayerdidcapturedwithimage:notification.userinfo[mpmovieplayerthumbnailimagekey]];
}
-(void) finished
{
1. Delete Notification Listener
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
2. Return to parent form
Who applies, who releases!
[Self.delegate movieplayerdidfinished];
NSLog (@ "Done");
}
/**
Mpmovieplaybackstatestopped, stop.
Mpmovieplaybackstateplaying, play
mpmovieplaybackstatepaused, pause
mpmovieplaybackstateinterrupted, interrupt
Mpmovieplaybackstateseekingforward, Next.
Mpmovieplaybackstateseekingbackward before a
*/
-(void) statechanged
{
Switch (self.moviePlayer.playbackState) {
Case mpmovieplaybackstateplaying:
NSLog (@ "Play");
Break
Case mpmovieplaybackstatepaused:
NSLog (@ "pause");
Break
Case mpmovieplaybackstatestopped:
trigger when execution [Self.movieplayer stop] or forward back does not work
NSLog (@ "Stop");
Break
Default
Break
}
}
@end
Video playback of iOS development