Monkey original, reprinted. Reprinted Please note:Reposted from Cocos2D Development Network-cocos2dev.com. Thank you!
Original article address:Http://www.cocos2dev.com /? P = 545
A long time ago, I wrote a 2dx video tutorial for playing MP4 videos. Some netizens reported that it was no longer usable. This evening I wrote a simple 2dx video class. Cocos2dx 3.0 is used.
Class description:
LHVideoPlayerImplCpp. h/mm // playback MP4 interface used in cocos2dx
LHVideoPlayerImpl. h/m // specifies the oc interface of videoPlayer.
LHVideoPlayer. h/m // videoPlayer implementation, call MPMoviePlayerController to play MP4
LHVideoOverlayView. h/m // upper-layer operation layer of videoPlayer, with the skip film button.
In terms of functions, I will introduce two classes.
The first is the LHVideoPlayerImplCpp. h/mm file, which is called for 2dx. This class has two static methods:
Class LHVideoPlayerImplCpp {public:/*** start playing MP4 video * name video name, without the suffix ". mp4 ". (For example, if the file is test.mp4, the name is "test") * frameRect video display area. Full Screen (Rect (0, 0, visibleSize. width, visibleSize. height) */static void playMP4WithName (const char * name, cocos2d: Rect frameRect);/*** set the title of the skip film button, the video skipping function */static void setSkipTitle (const char * title) by default );};
The second is LHVideoPlayer. h/m, which is responsible for playing MP4.
1. This method is used to play MP4. The annotations are clear.
-(Void) playMP4WithName: (NSString *) name VideoFrame :( CGRect) rect {UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow]; // obtain the video file name NSString * url = [[NSBundle mainBundle] pathForResource: name ofType: @ "mp4"]; // initialize player _ player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: url]; [keyWindow. rootViewController. view addSubview: [_ player view]; // sets the player style [_ player setControlStyle: MPMovieControlStyleNone]; [[_ player view] setFrame: rect]; // when MP4 completes playback, the callback [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (movieFinishedCallback :) name: MPMoviePlayerPlaybackDidFinishNotification object: _ player]; // start playing the video [_ player play]; // upper-layer operation layer _ videoOverlayView = [[LHVideoOverlayView alloc] initWithFrame: rect]; [keyWindow. rootViewController. view addSubview: _ videoOverlayView];}
2. This method removes the playback view after the playback ends.
- (void)removePlayer:(MPMoviePlayerController*)player{ [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; [player.view removeFromSuperview]; [_player release]; _player = nil; [_videoOverlayView removeFromSuperview]; [_videoOverlayView release]; _videoOverlayView = nil;}
3. the playback is complete and the outside world is notified. PlayerPlayFinished: The method is empty and no notification is sent to the outside world. After reading this, I felt that there was no need, so I did not add it.
Usage:
1. Import the header file
# Include "LHVideoPlayerImplCpp. h"
2. Start and use the interface, and assume that you want to configure loading.mp4"
Size visibleSize = Director: getInstance ()-> getVisibleSize ();
LHVideoPlayerImplCpp: playMP4WithName ("loading", Rect (0, 0, visibleSize. width, visibleSize. height ));
LHVideoPlayerImplCpp: setSkipTitle ("Skip ");
3. After the video ends, the view is automatically removed.
Git address: https://github.com/sunny-liu/Code/tree/master/cocos2dx_3.0_mp4