一. 播放本地视频
对于IOS平台的视频播放,我们可以借助Cocos2d-iphone 的Extensions:CCVideoPlayer来实现
1.导入支持cocos2d-x的扩展库到项目中(这里可以参考Himi的第六章视频播放小节内容,这里的扩展库是Himi修改好的,我就直接拿来用了!希望没有侵权!)
2.添加MediaPalyer框架到项目中
3.修改ios里AppController.h 和AppController.mm文件
AppController.h
- #import "../Classes/CCVideoPlayeriOS/CCVideoPlayer.h"
- @class RootViewController;
- @interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate,
- UITextFieldDelegate,UIApplicationDelegate,CCVideoPlayerDelegate>
- {
- UIWindow *window;
- RootViewController *viewController;
- }
- @property (nonatomic, retain) UIWindow *window;
- @property (nonatomic, retain) RootViewController *viewController;
- -(void)playVideo;
- @end
AppController.mm
添加using namespace cocos2d;
因为playVideo用到了cocos2d-x里的api
CCSizesize = CCDirector::sharedDirector()->getWinSize();
在application函数里添加视频播放监听
[CCVideoPlayer setDelegate :self];
playVideo实现如下:
- -(void)playVideo
- {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- [CCVideoPlayer setScrrenSize:CGSizeMake(size.width-400, size.height-300)];
- [CCVideoPlayer setNoSkip:true];
- [CCVideoPlayer playMovieWithFile:@"xcm.mp4"];
- // 播放网络视频
- // [viewController playURLVideo];
- }
4.添加混编类IOSPlayVideo
IOSPlayVideo.h
- #ifndef __IOSPlayVideo_SCENE_H__
- #define __IOSPlayVideo_SCENE_H__
- class IOSPlayVideo
- {
- public:
- static void playVideoForIOS();
-
- };
-
- #endif // __IOSPlayVideo_SCENE_H__
IOSPlayVideo.mm
- #include "IOSPlayVideo.h"
- #include "AppController.h"
- void IOSPlayVideo::playVideoForIOS()
- {
- AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
- [app playVideo];
- }
5.添加一个cocos2d-x类 :Platform
Platform.h
- #ifndef __Platform_SCENE_H__
- #define __Platform_SCENE_H__
- #include "cocos2d.h"
-
- using namespace cocos2d;
- class Platform
- {
- public:
- static void playVideo();//用于播放本地视频
- static void playURLVideo();//用于播放网络视频
- };
-
- #endif // __Platform_SCENE_H__
Platform.mm
- #include "Platform.h"
- #include "../cocos2dx/platform/CCPlatformConfig.h"
- #if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
- #include <jni.h>
- #include "../cocos2dx/platform/android/jni/JniHelper.h"
- #include <android/log.h>
- #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
- #include "IOSPlayVideo.h"
- #endif
-
- void Platform::playVideo()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- //Android播放本地视频
- JniMethodInfo minfo;
- bool isHave = JniHelper::getMethodInfo(minfo,"org/cocos2dx/playvideo/playvideo","playVedio", "()V");
- if (isHave)
- {
- minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
- }
- #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
- //IOS播放本地视频
- IOSPlayVideo::playVideoForIOS();
- #endif
- }
-
- void Platform::playURLVideo()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- //Android播放网络视频
- JniMethodInfo minfo;
- bool isHave = JniHelper::getMethodInfo(minfo,"org/cocos2dx/playvideo/playvideo","playURLVideo", "()V");
- if (isHave)
- {
- minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
- }
- #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
- //IOS播放网络视频
- IOSPlayVideo::playVideoForIOS();
- #endif
- }
IOS项目里只需在需要的地方调用函数就可以播放视频了!
至于Android就稍微麻烦一点,需要用到Jni 技术, C++调用java
A.修改playvideo.java
修改后如下:
添加两个.java文件:VideoView.java和VedioActivity.java
VideoView.java
- package org.cocos2dx.playvideo;
-
- import java.io.IOException;
-
- import android.content.Context;
- import android.content.Intent;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.media.MediaPlayer.OnErrorListener;
- import android.net.Uri;
- import android.util.AttributeSet;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceHolder.Callback;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.MediaController;
- import android.widget.MediaController.MediaPlayerControl;
-
- public class VideoView extends SurfaceView implements MediaPlayerControl,Callback {
-
-
- private Context mContext;
-
-
- private Uri mUri;
- private int mDuration;
-
-
- private SurfaceHolder mSurfaceHolder = null;
- private MediaPlayer mMediaPlayer = null;
- private boolean mIsPrepared;
- private int mVideoWidth;
- private int mVideoHeight;
- private int mSurfaceWidth;
- private int mSurfaceHeight;
- private MediaController mMediaController;
- private OnCompletionListener mOnCompletionListener;
- private MediaPlayer.OnPreparedListener mOnPreparedListener;
- private int mCurrentBufferPercentage;
- private OnErrorListener mOnErrorListener;
- private boolean mStartWhenPrepared;
- private int mSeekWhenPrepared;
-
- private MySizeChangeLinstener mMyChangeLinstener;
-
- public int getVideoWidth() {
- return mVideoWidth;
- }
-
- public int getVideoHeight() {
- return mVideoHeight;
- }
-
- public void setVideoScale(int width, int height) {
- LayoutParams lp = getLayoutParams();
- lp.height = height;
- lp.width = width;
- setLayoutParams(lp);
- }
-
- public interface MySizeChangeLinstener {
- public void doMyThings();
- }
-
- public void setMySizeChangeLinstener(MySizeChangeLinstener l) {
- mMyChangeLinstener = l;
- }
-
- public VideoView(Context context) {
- super(context);
- mContext = context;
- initVideoView();
- }
-
- public VideoView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- mContext = context;
- initVideoView();
- }
-
- public VideoView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- mContext = context;
- initVideoView();
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
- int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
- int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
-
-
-
-
-
-
-
-
-
-
-
- setMeasuredDimension(width, height);
- }
-
- public int resolveAdjustedSize(int desiredSize, int measureSpec) {
- int result = desiredSize;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
-
- switch (specMode) {
- case MeasureSpec.UNSPECIFIED:
-
-
-
-
- result = desiredSize;
- break;
-
- case MeasureSpec.AT_MOST:
-
-
-
-
-
- result = Math.min(desiredSize, specSize);
- break;
-
- case MeasureSpec.EXACTLY:
-
- result = specSize;
- break;
- }
- return result;
- }
-
- private void initVideoView() {
- mVideoWidth = 0;
- mVideoHeight = 0;
- getHolder().addCallback(this);
- getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- setFocusable(true);
- setFocusableInTouchMode(true);
- requestFocus();
- }
-
- public void setVideoPath(String path) {
- setVideoURI(Uri.parse(path));
- }
-
- public void setVideoURI(Uri uri) {
- mUri = uri;
- mStartWhenPrepared = false;
- mSeekWhenPrepared = 0;
- openVideo();
- requestLayout();
- invalidate();
- }
-
- public void stopPlayback() {
- if (mMediaPlayer != null) {
- mMediaPlayer.stop();
- mMediaPlayer.release();
- mMediaPlayer = null;
- }
- }
-
- private void openVideo() {
- if (mUri == null || mSurfaceHolder == null) {
-
- return;
- }
-
-
-
- Intent i = new Intent("com.android.music.musicservicecommand");
- i.putExtra("command", "pause");
- mContext.sendBroadcast(i);
-
- if (mMediaPlayer != null) {
- mMediaPlayer.reset();
- mMediaPlayer.release();
- mMediaPlayer = null;
- }
- try {
- mMediaPlayer = new MediaPlayer();
- mMediaPlayer.setOnPreparedListener(mPreparedListener);
- mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
- mIsPrepared = false;
-
- mDuration = -1;
- mMediaPlayer.setOnCompletionListener(mCompletionListener);
- mMediaPlayer.setOnErrorListener(mErrorListener);
- mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
- mCurrentBufferPercentage = 0;
- mMediaPlayer.setDataSource(mContext, mUri);
- mMediaPlayer.setDisplay(mSurfaceHolder);
- mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- mMediaPlayer.setScreenOnWhilePlaying(true);
- mMediaPlayer.prepareAsync();
- attachMediaController();
- } catch (IOException ex) {
-
- return;
- } catch (IllegalArgumentException ex) {
-
- return;
- }
- }
-
- public void setMediaController(MediaController controller) {
- if (mMediaController != null) {
- mMediaController.hide();
- }
- mMediaController = controller;
- attachMediaController();
- }
-
- private void attachMediaController() {
- if (mMediaPlayer != null && mMediaController != null) {
- mMediaController.setMediaPlayer(this);
- View anchorView = this.getParent() instanceof View ? (View) this
- .getParent() : this;
- mMediaController.setAnchorView(anchorView);
- mMediaController.setEnabled(mIsPrepared);
- }
- }
-
- MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener = new MediaPlayer.OnVideoSizeChangedListener() {
- public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
- mVideoWidth = mp.getVideoWidth();
- mVideoHeight = mp.getVideoHeight();
-
- if (mMyChangeLinstener != null) {
- mMyChangeLinstener.doMyThings();
- }
-
- if (mVideoWidth != 0 && mVideoHeight != 0) {
- getHolder().setFixedSize(mVideoWidth, mVideoHeight);
- }
- }
- };
-
- MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
- public void onPrepared(MediaPlayer mp) {
-
- mIsPrepared = true;
- if (mOnPreparedListener != null) {
- mOnPreparedListener.onPrepared(mMediaPlayer);
- }
- if (mMediaController != null) {
- mMediaController.setEnabled(true);
- }
- mVideoWidth = mp.getVideoWidth();
- mVideoHeight = mp.getVideoHeight();
- if (mVideoWidth != 0 && mVideoHeight != 0) {
-
-
- getHolder().setFixedSize(mVideoWidth, mVideoHeight);
- if (mSurfaceWidth == mVideoWidth
- && mSurfaceHeight == mVideoHeight) {
-
-
-
-
-
- if (mSeekWhenPrepared != 0) {
- mMediaPlayer.seekTo(mSeekWhenPrepared);
- mSeekWhenPrepared = 0;
- }
- if (mStartWhenPrepared) {
- mMediaPlayer.start();
- mStartWhenPrepared = false;
- if (mMediaController != null) {
- mMediaController.show();
- }
- } else if (!isPlaying()
- && (mSeekWhenPrepared != 0 || getCurrentPosition() > 0)) {
- if (mMediaController != null) {
-
-
- mMediaController.show(0);
- }
- }
- }
- } else {
-
-
- if (mSeekWhenPrepared != 0) {
- mMediaPlayer.seekTo(mSeekWhenPrepared);
- mSeekWhenPrepared = 0;
- }
- if (mStartWhenPrepared) {
- mMediaPlayer.start();
- mStartWhenPrepared = false;
- }
- }
- }
- };
-
- private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
- public void onCompletion(MediaPlayer mp) {
- if (mMediaController != null) {
- mMediaController.hide();
- }
- if (mOnCompletionListener != null) {
- mOnCompletionListener.onCompletion(mMediaPlayer);
- }
- }
- };
-
- private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
- public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
-
- if (mMediaController != null) {
- mMediaController.hide();
- }
-
-
- if (mOnErrorListener != null) {
- if (mOnErrorListener.onError(mMediaPlayer, framework_err,
- impl_err)) {
- return true;
- }
- }
-
-
-
-
-
-
-
- if (getWindowToken() != null) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- return true;
- }
- };
-
- private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
- public void onBufferingUpdate(MediaPlayer mp, int percent) {
- mCurrentBufferPercentage = percent;
- }
- };
-
-
-
-
-
-
-
-
- public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
- mOnPreparedListener = l;
- }
-
-
-
-
-
-
-
-
- public void setOnCompletionListener(OnCompletionListener l) {
- mOnCompletionListener = l;
- }
-
-
-
-
-
-
-
-
-
- public void setOnErrorListener(OnErrorListener l) {
- mOnErrorListener = l;
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- if (mIsPrepared && mMediaPlayer != null && mMediaController != null) {
- toggleMediaControlsVisiblity();
- }
- return false;
- }
-
- @Override
- public boolean onTrackballEvent(MotionEvent ev) {
- if (mIsPrepared && mMediaPlayer != null && mMediaController != null) {
- toggleMediaControlsVisiblity();
- }
- return false;
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (mIsPrepared && keyCode != KeyEvent.KEYCODE_BACK
- && keyCode != KeyEvent.KEYCODE_VOLUME_UP
- && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
- && keyCode != KeyEvent.KEYCODE_MENU
- && keyCode != KeyEvent.KEYCODE_CALL
- && keyCode != KeyEvent.KEYCODE_ENDCALL && mMediaPlayer != null
- && mMediaController != null) {
- if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK
- || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
- if (mMediaPlayer.isPlaying()) {
- pause();
- mMediaController.show();
- } else {
- start();
- mMediaController.hide();
- }
- return true;
- } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
- && mMediaPlayer.isPlaying()) {
- pause();
- mMediaController.show();
- } else {
- toggleMediaControlsVisiblity();
- }
- }
-
- return super.onKeyDown(keyCode, event);
- }
-
- private void toggleMediaControlsVisiblity() {
- if (mMediaController.isShowing()) {
- mMediaController.hide();
- } else {
- mMediaController.show();
- }
- }
-
- public void start() {
- if (mMediaPlayer != null && mIsPrepared) {
- mMediaPlayer.start();
- mStartWhenPrepared = false;
- } else {
- mStartWhenPrepared = true;
- }
- }
-
- public void pause() {
- if (mMediaPlayer != null && mIsPrepared) {
- if (mMediaPlayer.isPlaying()) {
- mMediaPlayer.pause();
- }
- }
- mStartWhenPrepared = false;
- }
-
- public int getDuration() {
- if (mMediaPlayer != null && mIsPrepared) {
- if (mDuration > 0) {
- return mDuration;
- }
- mDuration = mMediaPlayer.getDuration();
- return mDuration;
- }
- mDuration = -1;
- return mDuration;
- }
-
- public int getCurrentPosition() {
- if (mMediaPlayer != null && mIsPrepared) {
- return mMediaPlayer.getCurrentPosition();
- }
- return 0;
- }
-
- public void seekTo(int msec) {
- if (mMediaPlayer != null && mIsPrepared) {
- mMediaPlayer.seekTo(msec);
- } else {
- mSeekWhenPrepared = msec;
- }
- }
-
- public boolean isPlaying() {
- if (mMediaPlayer != null && mIsPrepared) {
- return mMediaPlayer.isPlaying();
- }
- return false;
- }
-
- public int getBufferPercentage() {
- if (mMediaPlayer != null) {
- return mCurrentBufferPercentage;
- }
- return 0;
- }
-
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- mSurfaceWidth = width;
- mSurfaceHeight = height;
- if (mMediaPlayer != null && mIsPrepared && mVideoWidth == width
- && mVideoHeight == height) {
- if (mSeekWhenPrepared != 0) {
- mMediaPlayer.seekTo(mSeekWhenPrepared);
- mSeekWhenPrepared = 0;
- }
- mMediaPlayer.start();
- if (mMediaController != null) {
- mMediaController.show();
- }
- }
- }
-
- public void surfaceCreated(SurfaceHolder holder) {
- mSurfaceHolder = holder;
- openVideo();
- }
-
- public void surfaceDestroyed(SurfaceHolder holder) {
-
- mSurfaceHolder = null;
- if (mMediaController != null)
- mMediaController.hide();
- if (mMediaPlayer != null) {
- mMediaPlayer.reset();
- mMediaPlayer.release();
- mMediaPlayer = null;
- }
- }
-
- public boolean canPause() {
-
- return false;
- }
-
- public boolean canSeekBackward() {
-
- return false;
- }
-
- public boolean canSeekForward() {
-
- return false;
- }
- }
VedioActivity.java
- package org.cocos2dx.playvideo;
-
- import android.app.Activity;
- import android.media.MediaPlayer;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
-
-
- public class VedioActivity extends Activity implements MediaPlayer.OnCompletionListener{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
-
- VideoView view =new VideoView(this);
-
- setContentView(view);
-
- view.setOnCompletionListener(this);
-
- view.setVideoURI(Uri.parse("android.resource://org.cocos2dx.playvideo/" + R.raw.xcm));
-
- view.start();
- }
-
-
- @Override
- public void onCompletion(MediaPlayer mp) {
-
- this.finish();
- }
- }
注:在 res下新建一个名为raw的文件夹,将assets下xcm.mp4拷贝到raw文件夹下,否则 :raw 和xcm无法识别!!!
二.播放网络视频
IOS项目:
在RootViewController.h里添加playURLVideo函数
并导入头文件#import"MediaPlayer/MediaPlayer.h"
RootViewController.mm实现playURLVideo函数如下:
- -(void)playURLVideo
- {
- NSURL *movieUrl = [NSURL URLWithString:@"http://v.youku.com/player/getRealM3U8/vid/XMzU5NDE3NTYw/type//video.m3u8"];
-
- MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
-
- [self presentMoviePlayerViewControllerAnimated:player];
- }
然后修改AppController.mm里的函数
- -(void)playVideo
- {
- // CCSize size = CCDirector::sharedDirector()->getWinSize();
- // [CCVideoPlayer setScrrenSize:CGSizeMake(size.width-400, size.height-300)];
- // [CCVideoPlayer setNoSkip:true];
- // [CCVideoPlayer playMovieWithFile:@"xcm.mp4"];
- //播放网络视频
- [viewController playURLVideo];
- }
Android项目:修改playvideo.java
修改后如下:
- package org.cocos2dx.playvideo;
-
- import org.cocos2dx.lib.Cocos2dxActivity;
-
- import android.net.Uri;
- import android.os.Bundle;
- import android.content.Intent;
-
-
- public class playvideo extends Cocos2dxActivity{
-
- public static playvideo instance;
-
- public static Intent intent;
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- instance =this;
- }
- void playURLVideo()
- {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- String type = "video/* ";
- Uri uri = Uri.parse("http://forum.ea3w.com/coll_ea3w/attach/2008_10/12237832415.3gp");
- intent.setDataAndType(uri, type);
- instance.startActivity(intent);
- }
- static {
- System.loadLibrary("game");
- }
- }