一. 播放本地视频
对于IOS平台的视频播放,我们可以借助Cocos2d-iphone 的Extensions:CCVideoPlayer来实现
1.导入支持cocos2d-x的扩展库到项目中(这里可以参考Himi的第六章视频播放小节内容,这里的扩展库是Himi修改好的,我就直接拿来用了!希望没有侵权!)
2.添加MediaPalyer框架到项目中
3.修改ios里AppController.h 和AppController.mm文件
AppController.h
1. #import "../Classes/CCVideoPlayeriOS/CCVideoPlayer.h"
2. @class RootViewController;
3. @interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate,
4. UITextFieldDelegate,UIApplicationDelegate,CCVideoPlayerDelegate>
5. {
6. UIWindow *window;
7. RootViewController *viewController;
8. }
9. @property (nonatomic, retain) UIWindow *window;
10. @property (nonatomic, retain) RootViewController *viewController;
11. -(void)playVideo;
12. @end
AppController.mm
添加using namespace cocos2d;
因为playVideo用到了cocos2d-x里的api
CCSizesize = CCDirector::sharedDirector()->getWinSize();
在application函数里添加视频播放监听
[CCVideoPlayer setDelegate :self];
playVideo实现如下:
1. -(void)playVideo
2. {
3. CCSize size = CCDirector::sharedDirector()->getWinSize();
4. [CCVideoPlayer setScrrenSize:CGSizeMake(size.width-400, size.height-300)];
5. [CCVideoPlayer setNoSkip:true];
6. [CCVideoPlayer playMovieWithFile:@"xcm.mp4"];
7. // 播放网络视频
8. // [viewController playURLVideo];
9. }
4.添加混编类IOSPlayVideo
IOSPlayVideo.h
1. #ifndef __IOSPlayVideo_SCENE_H__
2. #define __IOSPlayVideo_SCENE_H__
3. class IOSPlayVideo
4. {
5. public:
6. static void playVideoForIOS();
7.
8. };
9.
10. #endif // __IOSPlayVideo_SCENE_H__
IOSPlayVideo.mm
1. #include "IOSPlayVideo.h"
2. #include "AppController.h"
3. void IOSPlayVideo::playVideoForIOS()
4. {
5. AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
6. [app playVideo];
7. }
5.添加一个cocos2d-x类 :Platform
Platform.h
1. #ifndef __Platform_SCENE_H__
2. #define __Platform_SCENE_H__
3. #include "cocos2d.h"
4.
5. using namespace cocos2d;
6. class Platform
7. {
8. public:
9. static void playVideo();//用于播放本地视频
10. static void playURLVideo();//用于播放网络视频
11. };
12.
13. #endif // __Platform_SCENE_H__
Platform.mm
1. #include "Platform.h"
2. #include "../cocos2dx/platform/CCPlatformConfig.h"
3. #if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
4. #include <jni.h>
5. #include "../cocos2dx/platform/android/jni/JniHelper.h"
6. #include <android/log.h>
7. #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
8. #include "IOSPlayVideo.h"
9. #endif
10.
11. void Platform::playVideo()
12. {
13. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
14. //Android播放本地视频
15. JniMethodInfo minfo;
16. bool isHave = JniHelper::getMethodInfo(minfo,"org/cocos2dx/playvideo/playvideo","playVedio", "()V");
17. if (isHave)
18. {
19. minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
20. }
21. #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
22. //IOS播放本地视频
23. IOSPlayVideo::playVideoForIOS();
24. #endif
25. }
26.
27. void Platform::playURLVideo()
28. {
29. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
30. //Android播放网络视频
31. JniMethodInfo minfo;
32. bool isHave = JniHelper::getMethodInfo(minfo,"org/cocos2dx/playvideo/playvideo","playURLVideo", "()V");
33. if (isHave)
34. {
35. minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
36. }
37. #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
38. //IOS播放网络视频
39. IOSPlayVideo::playVideoForIOS();
40. #endif
41. }
IOS项目里只需在需要的地方调用函数就可以播放视频了!
至于Android就稍微麻烦一点,需要用到Jni 技术, C++调用java
A.修改playvideo.java
修改后如下:
1. package org.cocos2dx.playvideo;
2.
3. import org.cocos2dx.lib.Cocos2dxActivity;
4. import org.cocos2dx.lib.Cocos2dxEditText;
5. import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
6. import org.cocos2dx.lib.Cocos2dxRenderer;
7.
8. import android.app.ActivityManager;
9. import android.content.Context;
10. import android.content.Intent;
11. import android.content.pm.ConfigurationInfo;
12. import android.os.Bundle;
13. import android.util.Log;
14. import android.view.ViewGroup;
15. import android.widget.FrameLayout;
16.
17. public class playvideo extends Cocos2dxActivity {
18. //当前类实例
19. public static playvideo instance;
20. //用于切换Activity
21. public static Intent intent;
22.
23. protected void onCreate(Bundle savedInstanceState){
24. super.onCreate(savedInstanceState);
25.
26. this;
27. if (detectOpenGLES20()) {
28. // get the packageName,it's used to set the resource path
29. String packageName = getApplication().getPackageName();
30. super.setPackageName(packageName);
31.
32. // FrameLayout
33. ViewGroup.LayoutParams framelayout_params =
34. new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
35. ViewGroup.LayoutParams.FILL_PARENT);
36. new FrameLayout(this);
37. framelayout.setLayoutParams(framelayout_params);
38.
39. // Cocos2dxEditText layout
40. ViewGroup.LayoutParams edittext_layout_params =
41. new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
42. ViewGroup.LayoutParams.WRAP_CONTENT);
43. new Cocos2dxEditText(this);
44. edittext.setLayoutParams(edittext_layout_params);
45.
46. // ...add to FrameLayout
47. framelayout.addView(edittext);
48.
49. // Cocos2dxGLSurfaceView
50. new Cocos2dxGLSurfaceView(this);
51.
52. // ...add to FrameLayout
53. framelayout.addView(mGLView);
54.
55. 2);
56. new Cocos2dxRenderer());
57. mGLView.setTextField(edittext);
58.
59. // Set framelayout as the content view
60. setContentView(framelayout);
61.
62.
63.
64. new Intent(playvideo.this, VedioActivity.class);
65.
66. }
67. else {
68. "activity", "don't support gles2.0");
69. finish();
70. }
71. }
72.
73. void playVideo()
74. {
75. instance.startActivity(intent);
76.
77. }
78. @Override
79. protected void onPause() {
80. super.onPause();
81. mGLView.onPause();
82. }
83.
84. @Override
85. protected void onResume() {
86. super.onResume();
87. mGLView.onResume();
88. }
89.
90. private boolean detectOpenGLES20()
91. {
92. ActivityManager am =
93. (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
94. ConfigurationInfo info = am.getDeviceConfigurationInfo();
95. return (info.reqGlEsVersion >= 0x20000);
96. }
97.
98. static {
99. "game");
100. }
101.
102. }
添加两个.java文件:VideoView.java和VedioActivity.java
VideoView.java
1. package org.cocos2dx.playvideo;
2.
3. import java.io.IOException;
4.
5. import android.content.Context;
6. import android.content.Intent;
7. import android.media.AudioManager;
8. import android.media.MediaPlayer;
9. import android.media.MediaPlayer.OnCompletionListener;
10. import android.media.MediaPlayer.OnErrorListener;
11. import android.net.Uri;
12. import android.util.AttributeSet;
13. import android.view.KeyEvent;
14. import android.view.MotionEvent;
15. import android.view.SurfaceHolder;
16. import android.view.SurfaceHolder.Callback;
17. import android.view.SurfaceView;
18. import android.view.View;
19. import android.view.ViewGroup.LayoutParams;
20. import android.widget.MediaController;
21. import android.widget.MediaController.MediaPlayerControl;
22.
23. public class VideoView extends SurfaceView implements MediaPlayerControl,Callback {
24.
25.
26. private Context mContext;
27.
28. // settable by the client
29. private Uri mUri;
30. private int mDuration;
31.
32. // All the stuff we need for playing and showing a video
33. private SurfaceHolder mSurfaceHolder = null;
34. private MediaPlayer mMediaPlayer = null;
35. private boolean mIsPrepared;
36. private int mVideoWidth;
37. private int mVideoHeight;
38. private int mSurfaceWidth;
39. private int mSurfaceHeight;
40. private MediaController mMediaController;
41. private OnCompletionListener mOnCompletionListener;
42. private MediaPlayer.OnPreparedListener mOnPreparedListener;
43. private int mCurrentBufferPercentage;
44. private one rrorListener mOnErrorListener;
45. private boolean mStartWhenPrepared;
46. private int mSeekWhenPrepared;
47.
48. private MySizeChangeLinstener mMyChangeLinstener;
49.
50. public int getVideoWidth() {
51. return mVideoWidth;
52. }
53.
54. public int getVideoHeight() {
55. return mVideoHeight;
56. }
57.
58. public void setVideoScale(int width, int height) {
59. LayoutParams lp = getLayoutParams();
60. lp.height = height;
61. lp.width = width;
62. setLayoutParams(lp);
63. }
64.
65. public interface MySizeChangeLinstener {
66. public void doMyThings();
67. }
68.
69. public void setMySizeChangeLinstener(MySizeChangeLinstener l) {
70. mMyChangeLinstener = l;
71. }
72.
73. public VideoView(Context context) {
74. super(context);
75. mContext = context;
76. initVideoView();
77. }
78.
79. public VideoView(Context context, AttributeSet attrs) {
80. this(context, attrs, 0);
81. mContext = context;
82. initVideoView();
83. }
84.
85. public VideoView(Context context, AttributeSet attrs, int defStyle) {
86. super(context, attrs, defStyle);
87. mContext = context;
88. initVideoView();
89. }
90.
91. @Override
92. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
93. //Log.i("@@@@", "onMeasure");
94. int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
95. int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
96. /*
97. * if (mVideoWidth > 0 && mVideoHeight > 0) { if ( mVideoWidth * height
98. * > width * mVideoHeight ) { //Log.i("@@@",
99. * "image too tall, correcting"); height = width * mVideoHeight /
100. * mVideoWidth; } else if ( mVideoWidth * height < width * mVideoHeight
101. * ) { //Log.i("@@@", "image too wide, correcting"); width = height *
102. * mVideoWidth / mVideoHeight; } else { //Log.i("@@@",
103. * "aspect ratio is correct: " + //width+"/"+height+"="+
104. * //mVideoWidth+"/"+mVideoHeight); } }
105. */
106. // Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height);
107. setMeasuredDimension(width, height);
108. }
109.
110. public int resolveAdjustedSize(int desiredSize, int measureSpec) {
111. int result = desiredSize;
112. int specMode = MeasureSpec.getMode(measureSpec);
113. int specSize = MeasureSpec.getSize(measureSpec);
114.
115. switch (specMode) {
116. case MeasureSpec.UNSPECIFIED:
117. /*
118. * Parent says we can be as big as we want. Just don't be larger
119. * than max size imposed on ourselves.
120. */
121. result = desiredSize;
122. break;
123.
124. case MeasureSpec.AT_MOST:
125. /*
126. * Parent says we can be as big as we want, up to specSize. Don't be
127. * larger than specSize, and don't be larger than the max size
128. * imposed on ourselves.
129. */
130. result = Math.min(desiredSize, specSize);
131. break;
132.
133. case MeasureSpec.EXACTLY:
134. // No choice. Do what we are told.
135. result = specSize;
136. break;
137. }
138. return result;
139. }
140.
141. private void initVideoView() {
142. 0;
143. 0;
144. this);
145. getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
146. true);
147. true);
148. requestFocus();
149. }
150.
151. public void setVideoPath(String path) {
152. setVideoURI(Uri.parse(path));
153. }
154.
155. public void setVideoURI(Uri uri) {
156. mUri = uri;
157. false;
158. 0;
159. openVideo();
160. requestLayout();
161. invalidate();
162. }
163.
164. public void stopPlayback() {
165. if (mMediaPlayer != null) {
166. mMediaPlayer.stop();
167. mMediaPlayer.release();
168. null;
169. }
170. }
171.
172. private void openVideo() {
173. if (mUri == null || mSurfaceHolder == null) {
174. // not ready for playback just yet, will try again later
175. return;
176. }
177. // Tell the music playback service to pause
178. // TODO: these constants need to be published somewhere in the
179. // framework.
180. new Intent("com.android.music.musicservicecommand");
181. "command", "pause");
182. mContext.sendBroadcast(i);
183.
184. if (mMediaPlayer != null) {
185. mMediaPlayer.reset();
186. mMediaPlayer.release();
187. null;
188. }
189. try {
190. new MediaPlayer();
191. mMediaPlayer.setOnPreparedListener(mPreparedListener);
192. mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
193. false;
194. //Log.v(TAG, "reset duration to -1 in openVideo");
195. 1;
196. mMediaPlayer.setOnCompletionListener(mCompletionListener);
197. mMediaPlayer.setOnErrorListener(mErrorListener);
198. mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
199. 0;
200. mMediaPlayer.setDataSource(mContext, mUri);
201. mMediaPlayer.setDisplay(mSurfaceHolder);
202. mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
203. true);
204. mMediaPlayer.prepareAsync();
205. attachMediaController();
206. catch (IOException ex) {
207. //Log.w(TAG, "Unable to open content: " + mUri, ex);
208. return;
209. catch (IllegalArgumentException ex) {
210. //Log.w(TAG, "Unable to open content: " + mUri, ex);
211. return;
212. }
213. }
214.
215. public void setMediaController(MediaController controller) {
216. if (mMediaController != null) {
217. mMediaController.hide();
218. }
219. mMediaController = controller;
220. attachMediaController();
221. }
222.
223. private void attachMediaController() {
224. if (mMediaPlayer != null && mMediaController != null) {
225. this);
226. this.getParent() instanceof View ? (View) this
227. this;
228. mMediaController.setAnchorView(anchorView);
229. mMediaController.setEnabled(mIsPrepared);
230. }
231. }
232.
233. new MediaPlayer.OnVideoSizeChangedListener() {
234. public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
235. mVideoWidth = mp.getVideoWidth();
236. mVideoHeight = mp.getVideoHeight();
237.
238. if (mMyChangeLinstener != null) {
239. mMyChangeLinstener.doMyThings();
240. }
241.
242. if (mVideoWidth != 0 && mVideoHeight != 0) {
243. getHolder().setFixedSize(mVideoWidth, mVideoHeight);
244. }
245. }
246. };
247.
248. new MediaPlayer.OnPreparedListener() {
249. public void onPrepared(MediaPlayer mp) {
250. // briefly show the mediacontroller
251. true;
252. if (mOnPreparedListener != null) {
253. mOnPreparedListener.onPrepared(mMediaPlayer);
254. }
255. if (mMediaController != null) {
256. true);
257. }
258. mVideoWidth = mp.getVideoWidth();
259. mVideoHeight = mp.getVideoHeight();
260. if (mVideoWidth != 0 && mVideoHeight != 0) {
261. // Log.i("@@@@", "video size: " + mVideoWidth +"/"+
262. // mVideoHeight);
263. getHolder().setFixedSize(mVideoWidth, mVideoHeight);
264. if (mSurfaceWidth == mVideoWidth
265. && mSurfaceHeight == mVideoHeight) {
266. // We didn't actually change the size (it was already at the
267. // size
268. // we need), so we won't get a "surface changed" callback,
269. // so
270. // start the video here instead of in the callback.
271. if (mSeekWhenPrepared != 0) {
272. mMediaPlayer.seekTo(mSeekWhenPrepared);
273. 0;
274. }
275. if (mStartWhenPrepared) {
276. mMediaPlayer.start();
277. false;
278. if (mMediaController != null) {
279. mMediaController.show();
280. }
281. else if (!isPlaying()
282. 0 || getCurrentPosition() > 0)) {
283. if (mMediaController != null) {
284. // Show the media controls when we're paused into a
285. // video and make 'em stick.
286. 0);
287. }
288. }
289. }
290. else {
291. // We don't know the video size yet, but should start anyway.
292. // The video size might be reported to us later.
293. if (mSeekWhenPrepared != 0) {
294. mMediaPlayer.seekTo(mSeekWhenPrepared);
295. 0;
296. }
297. if (mStartWhenPrepared) {
298. mMediaPlayer.start();
299. false;
300. }
301. }
302. }
303. };
304.
305. private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
306. public void onCompletion(MediaPlayer mp) {
307. if (mMediaController != null) {
308. mMediaController.hide();
309. }
310. if (mOnCompletionListener != null) {
311. mOnCompletionListener.onCompletion(mMediaPlayer);
312. }
313. }
314. };
315.
316. private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
317. public boolean one rror(MediaPlayer mp, int framework_err, int impl_err) {
318. //Log.d(TAG, "Error: " + framework_err + "," + impl_err);
319. if (mMediaController != null) {
320. mMediaController.hide();
321. }
322.
323. /* If an error handler has been supplied, use it and finish. */
324. if (mOnErrorListener != null) {
325. if (mOnErrorListener.onError(mMediaPlayer, framework_err,
326. impl_err)) {
327. return true;
328. }
329. }
330.
331. /*
332. * Otherwise, pop up an error dialog so the user knows that
333. * something bad has happened. Only try and pop up the dialog if
334. * we're attached to a window. When we're going away and no longer
335. * have a window, don't bother showing the user an error.
336. */
337. if (getWindowToken() != null) {
338.
339. /*
340. * if (framework_err ==
341. * MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
342. * messageId = com.android.internal.R.string.
343. * VideoView_error_text_invalid_progressive_playback; } else {
344. * messageId =
345. * com.android.internal.R.string.VideoView_error_text_unknown; }
346. *
347. * new AlertDialog.Builder(mContext)
348. * .setTitle(com.android.internal
349. * .R.string.VideoView_error_title) .setMessage(messageId)
350. * .setPositiveButton
351. * (com.android.internal.R.string.VideoView_error_button, new
352. * DialogInterface.OnClickListener() { public void
353. * onClick(DialogInterface dialog, int whichButton) { If we get
354. * here, there is no one rror listener, so at least inform them
355. * that the video is over.
356. *
357. * if (mOnCompletionListener != null) {
358. * mOnCompletionListener.onCompletion(mMediaPlayer); } } })
359. * .setCancelable(false) .show();
360. */
361. }
362. return true;
363. }
364. };
365.
366. private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
367. public void onBufferingUpdate(MediaPlayer mp, int percent) {
368. mCurrentBufferPercentage = percent;
369. }
370. };
371.
372. /**
373. * Register a callback to be invoked when the media file is loaded and ready
374. * to go.
375. *
376. * @param l
377. * The callback that will be run
378. */
379. public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
380. mOnPreparedListener = l;
381. }
382.
383. /**
384. * Register a callback to be invoked when the end of a media file has been
385. * reached during playback.
386. *
387. * @param l
388. * The callback that will be run
389. */
390. public void setOnCompletionListener(OnCompletionListener l) {
391. mOnCompletionListener = l;
392. }
393.
394. /**
395. * Register a callback to be invoked when an error occurs during playback or
396. * setup. If no listener is specified, or if the listener returned false,
397. * VideoView will inform the user of any errors.
398. *
399. * @param l
400. * The callback that will be run
401. */
402. public void setOnErrorListener(OnErrorListener l) {
403. mOnErrorListener = l;
404. }
405.
406. @Override
407. public boolean onTouchEvent(MotionEvent ev) {
408. if (mIsPrepared && mMediaPlayer != null && mMediaController != null) {
409. toggleMediaControlsVisiblity();
410. }
411. return false;
412. }
413.
414. @Override
415. public boolean onTrackballEvent(MotionEvent ev) {
416. if (mIsPrepared && mMediaPlayer != null && mMediaController != null) {
417. toggleMediaControlsVisiblity();
418. }
419. return false;
420. }
421.
422. @Override
423. public boolean onKeyDown(int keyCode, KeyEvent event) {
424. if (mIsPrepared && keyCode != KeyEvent.KEYCODE_BACK
425. && keyCode != KeyEvent.KEYCODE_VOLUME_UP
426. && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
427. && keyCode != KeyEvent.KEYCODE_MENU
428. && keyCode != KeyEvent.KEYCODE_CALL
429. null
430. null) {
431. if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK
432. || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
433. if (mMediaPlayer.isPlaying()) {
434. pause();
435. mMediaController.show();
436. else {
437. start();
438. mMediaController.hide();
439. }
440. return true;
441. else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
442. && mMediaPlayer.isPlaying()) {
443. pause();
444. mMediaController.show();
445. else {
446. toggleMediaControlsVisiblity();
447. }
448. }
449.
450. return super.onKeyDown(keyCode, event);
451. }
452.
453. private void toggleMediaControlsVisiblity() {
454. if (mMediaController.isShowing()) {
455. mMediaController.hide();
456. else {
457. mMediaController.show();
458. }
459. }
460.
461. public void start() {
462. if (mMediaPlayer != null && mIsPrepared) {
463. mMediaPlayer.start();
464. false;
465. else {
466. true;
467. }
468. }
469.
470. public void pause() {
471. if (mMediaPlayer != null && mIsPrepared) {
472. if (mMediaPlayer.isPlaying()) {
473. mMediaPlayer.pause();
474. }
475. }
476. false;
477. }
478.
479. public int getDuration() {
480. if (mMediaPlayer != null && mIsPrepared) {
481. if (mDuration > 0) {
482. return mDuration;
483. }
484. mDuration = mMediaPlayer.getDuration();
485. return mDuration;
486. }
487. 1;
488. return mDuration;
489. }
490.
491. public int getCurrentPosition() {
492. if (mMediaPlayer != null && mIsPrepared) {
493. return mMediaPlayer.getCurrentPosition();
494. }
495. return 0;
496. }
497.
498. public void seekTo(int msec) {
499. if (mMediaPlayer != null && mIsPrepared) {
500. mMediaPlayer.seekTo(msec);
501. else {
502. mSeekWhenPrepared = msec;
503. }
504. }
505.
506. public boolean isPlaying() {
507. if (mMediaPlayer != null && mIsPrepared) {
508. return mMediaPlayer.isPlaying();
509. }
510. return false;
511. }
512.
513. public int getBufferPercentage() {
514. if (mMediaPlayer != null) {
515. return mCurrentBufferPercentage;
516. }
517. return 0;
518. }
519.
520. public void surfaceChanged(SurfaceHolder holder, int format, int width,
521. int height) {
522. mSurfaceWidth = width;
523. mSurfaceHeight = height;
524. if (mMediaPlayer != null && mIsPrepared && mVideoWidth == width
525. && mVideoHeight == height) {
526. if (mSeekWhenPrepared != 0) {
527. mMediaPlayer.seekTo(mSeekWhenPrepared);
528. 0;
529. }
530. mMediaPlayer.start();
531. if (mMediaController != null) {
532. mMediaController.show();
533. }
534. }
535. }
536.
537. public void surfaceCreated(SurfaceHolder holder) {
538. mSurfaceHolder = holder;
539. openVideo();
540. }
541.
542. public void surfaceDestroyed(SurfaceHolder holder) {
543. // after we return from this we can't use the surface any more
544. null;
545. if (mMediaController != null)
546. mMediaController.hide();
547. if (mMediaPlayer != null) {
548. mMediaPlayer.reset();
549. mMediaPlayer.release();
550. null;
551. }
552. }
553.
554. public boolean canPause() {
555. // TODO Auto-generated method stub
556. return false;
557. }
558.
559. public boolean canSeekBackward() {
560. // TODO Auto-generated method stub
561. return false;
562. }
563.
564. public boolean canSeekForward() {
565. // TODO Auto-generated method stub
566. return false;
567. }
568. }
VedioActivity.java
1. package org.cocos2dx.playvideo;
2.
3. import android.app.Activity;
4. import android.media.MediaPlayer;
5. import android.net.Uri;
6. import android.os.Bundle;
7. import android.view.Window;
8. import android.view.WindowManager;
9.
10.
11. public class VedioActivity extends Activity implements MediaPlayer.OnCompletionListener{
12. @Override
13. protected void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. //隐藏标题栏
16. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
17. //隐藏状态栏
18. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
19.
20. //初始化视频View
21. new VideoView(this);
22. //设置显示视频View
23. setContentView(view);
24. //注册监听视频
25. this);
26. //设置视频文件路径
27. "android.resource://org.cocos2dx.playvideo/" + R.raw.xcm));
28. //播放视频
29. view.start();
30. }
31.
32. //当视频播放完后回调此函数
33. @Override
34. public void onCompletion(MediaPlayer mp) {
35. //结束当前Activity
36. this.finish();
37. }
38. }
注:在 res下新建一个名为raw的文件夹,将assets下xcm.mp4拷贝到raw文件夹下,否则 :raw 和xcm无法识别!!!
二.播放网络视频
IOS项目:
在RootViewController.h里添加playURLVideo函数
并导入头文件#import"MediaPlayer/MediaPlayer.h"
RootViewController.mm实现playURLVideo函数如下:
1. -(void)playURLVideo
2. {
3. NSURL *movieUrl = [NSURL URLWithString:@"http://v.youku.com/player/getRealM3U8/vid/XMzU5NDE3NTYw/type//video.m3u8"];
4.
5. MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
6.
7. [self presentMoviePlayerViewControllerAnimated:player];
8. }
然后修改AppController.mm里的函数
1. -(void)playVideo
2. {
3. // CCSize size = CCDirector::sharedDirector()->getWinSize();
4. // [CCVideoPlayer setScrrenSize:CGSizeMake(size.width-400, size.height-300)];
5. // [CCVideoPlayer setNoSkip:true];
6. // [CCVideoPlayer playMovieWithFile:@"xcm.mp4"];
7. //播放网络视频
8. [viewController playURLVideo];
9. }
Android项目:修改playvideo.java
修改后如下:
1. package org.cocos2dx.playvideo;标签:mMediaPlayer,视频,void,IOS,cocos2d,import,android,null,public From: https://blog.51cto.com/u_15923385/5971911
2.
3. import org.cocos2dx.lib.Cocos2dxActivity;
4.
5. import android.net.Uri;
6. import android.os.Bundle;
7. import android.content.Intent;
8.
9.
10. public class playvideo extends Cocos2dxActivity{
11. //当前类实例
12. public static playvideo instance;
13. //用于切换Activity
14. public static Intent intent;
15.
16. public void onCreate(Bundle savedInstanceState){
17. super.onCreate(savedInstanceState);
18. this;
19. }
20. void playURLVideo()
21. {
22. new Intent(Intent.ACTION_VIEW);
23. "video/* ";
24. "http://forum.ea3w.com/coll_ea3w/attach/2008_10/12237832415.3gp");
25. intent.setDataAndType(uri, type);
26. instance.startActivity(intent);
27. }
28. static {
29. "game");
30. }
31. }