非常久曾经写的一个2dx播放MP4视频的教材。有网友反映已经不能用了,今天晚上写了个简单的2dx播放视频类。使用的是cocos2dx 3.0。
类说明:
LHVideoPlayerImplCpp.h/mm // cocos2dx中使用的播放MP4接口
LHVideoPlayerImpl.h/m // videoPlayer的oc接口
LHVideoPlayer.h/m // videoPlayer的实现。调用MPMoviePlayerController播放MP4
LHVideoOverlayView.h/m // videoPlayer的上层操作层,有跳过影片button。
我就功能点而言,介绍当中的两个类。
第一个是LHVideoPlayerImplCpp.h/mm文件。这个是负责给2dx调用的。
该类有两个静态方法:
class LHVideoPlayerImplCpp {
public:
    /**
     * 開始播放MP4视频
     * name 视频名称。不要带后缀".mp4"。(比方文件是test.mp4, 那么name就传"test")
     * frameRect 视频显示的区域。全屏(Rect(0, 0, visibleSize.width, visibleSize.height))
     */
    static void playMP4WithName(const char* name, cocos2d::Rect frameRect);
    
    /**
     * 设置跳过影片buttontitle,默认无跳过影片功能
     */
    static void setSkipTitle(const char* title);
};
- (void)playMP4WithName: (NSString *)name VideoFrame:(CGRect)rect
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    
    // 获取视频文件的名称
    NSString *url = [[NSBundle mainBundle]pathForResource:name ofType:@"mp4"];
    // 初始化player
    _player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
    [keyWindow.rootViewController.view addSubview: [_player view]];
    
    // 设置player样式
    [_player setControlStyle: MPMovieControlStyleNone];
    [[_player view] setFrame: rect];
    
    // 当MP4完毕播放的回调
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(movieFinishedCallback:)
     name:MPMoviePlayerPlaybackDidFinishNotification object:_player];
    
    // 開始播放影片
    [_player play];
    
    // 上层操作层
    _videoOverlayView = [ [LHVideoOverlayView alloc] initWithFrame: rect];
    [keyWindow.rootViewController.view addSubview: _videoOverlayView];
}- (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、播放完毕,通知外界。
playerPlayFinished 这种方法是空的,没有通知外界。
我看了下,感觉没什么须要。所以没加。
用法:
1、导入头文件
#include “LHVideoPlayerImplCpp.h”
2、開始调用接口,如果你要播放的是“loading.mp4”
Size visibleSize = Director::getInstance()->getVisibleSize();
LHVideoPlayerImplCpp::playMP4WithName(“loading”, Rect(0, 0, visibleSize.width, visibleSize.height));
LHVideoPlayerImplCpp::setSkipTitle(“Skip”);
3、影片结束之后,会自己主动移除视图。
git地址:https://github.com/sunny-liu/Code/tree/master/cocos2dx_3.0_mp4
原文:http://www.cnblogs.com/ljbguanli/p/6915087.html