vIDeoVIEw.java
package com.uzwan.ddd;import java.io.fileDescriptor;import java.io.IOException;import androID.app.Activity;import androID.content.res.AssetfileDescriptor;import androID.media.MediaPlayer;import androID.net.Uri;import androID.util.Log;import androID.vIEw.MotionEvent;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import androID.vIEw.VIEw;/** * * @author Yichou * * create data:2013-4-22 22:19:49 */public class VIDeoVIEw extends SurfaceVIEw implements SurfaceHolder.Callback,VIEw.OntouchListener,MediaPlayer.OnPreparedListener,MediaPlayer.OnErrorListener,MediaPlayer.OnInfoListener,MediaPlayer.OnCompletionListener { private static final String TAG = "VIDeoVIEw"; private MediaPlayer mPlayer; private Activity gameActivity; private Uri resUri; private AssetfileDescriptor fd; private boolean surfaceCreated; private OnFinishListener onFinishListener; public VIDeoVIEw(Activity context) { super(context); this.gameActivity = context; final SurfaceHolder holder = getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); setontouchListener(this); mPlayer = new MediaPlayer(); mPlayer.setScreenOnWhilePlaying(true); mPlayer.setonPreparedListener(this); mPlayer.setonCompletionListener(this); mPlayer.setonErrorListener(this); mPlayer.setonInfoListener(this); } public VIDeoVIEw setonFinishListener(OnFinishListener onFinishListener) { this.onFinishListener = onFinishListener; return this; } public voID setVIDeo(Uri resUri) { this.resUri = resUri; try { mPlayer.setDataSource(gameActivity,resUri); } catch (Exception e) { } } public voID setVIDeo(AssetfileDescriptor fd) { this.fd = fd; try { mPlayer.setDataSource(fd.getfileDescriptor(),fd.getStartOffset(),fd.getLength()); } catch (IOException e) { e.printstacktrace(); } } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) { } @OverrIDe public voID surfaceCreated(final SurfaceHolder holder) { Log.i(TAG,"surfaceCreated"); surfaceCreated = true; mPlayer.setdisplay(holder); // 指定SurfaceHolder try { if(resUri != null) mPlayer.setDataSource(gameActivity,resUri); else if (fd != null) { mPlayer.setDataSource(fd.getfileDescriptor(),fd.getLength()); } } catch (Exception e) { } try { mPlayer.prepare(); } catch (Exception e1) { } mPlayer.start(); } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { Log.i(TAG,"surfaceDestroyed"); surfaceCreated = false; if(mPlayer != null){ mPlayer.stop(); mPlayer.reset(); } } @OverrIDe public voID onPrepared(MediaPlayer player) { int wWIDth = getWIDth(); int wHeight = getHeight(); /* 获得视频宽长 */ float vWIDth = 640; float vHeight = 960; /* 最适屏幕 */ float oriRatio = (float) wWIDth / (float) wHeight; float NowRatio = (float) vWIDth / (float) vHeight; float newWIDth = wWIDth; float newHeight = wHeight; if(NowRatio > oriRatio) { newHeight = wWIDth / NowRatio; } if(NowRatio < oriRatio) { newWIDth = wHeight * NowRatio; } androID.Widget.FrameLayout.LayoutParams para = new androID.Widget.FrameLayout.LayoutParams((int)(newWIDth),(int)(newHeight)); para.gravity = androID.vIEw.Gravity.CENTER; this.setLayoutParams(para); mPlayer.seekTo(posttion); mPlayer.start(); } private voID dispose() { mPlayer.release(); mPlayer = null; resUri = null; if (fd != null) { try { fd.close(); } catch (IOException e) { e.printstacktrace(); } fd = null; } } @OverrIDe public voID onCompletion(MediaPlayer mp) { Log.i(TAG,"onCompletion"); stop(); //dispose(); //if(onFinishListener != null) //onFinishListener.onVIDeoFinish(); } @OverrIDe public boolean onInfo(MediaPlayer mp,int what,int extra) { return true; } @OverrIDe public boolean onError(MediaPlayer mp,int extra) { return true; } @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { //stop(); } return true; } public voID stop() { mPlayer.stop(); dispose(); if(onFinishListener != null) onFinishListener.onVIDeoFinish(); } int posttion; public voID pause() { posttion = mPlayer.getCurrentposition(); mPlayer.pause(); } /** */ public voID resume() { if(surfaceCreated){ mPlayer.start(); }else { try { if(resUri != null) mPlayer.setDataSource(gameActivity,resUri); else if (fd != null) { mPlayer.setDataSource(fd.getfileDescriptor(),fd.getLength()); } } catch (Exception e) { } } } public interface OnFinishListener { public voID onVIDeoFinish(); }}
/****************************************************************************copyright (c) 2010-2011 cocos2d-x.orghttp://www.cocos2d-x.orgPermission is hereby granted,free of charge,to any person obtaining a copyof this software and associated documentation files (the "Software"),to dealin the Software without restriction,including without limitation the rightsto use,copy,modify,merge,publish,distribute,sublicense,and/or sellcopIEs of the Software,and to permit persons to whom the Software isfurnished to do so,subject to the following conditions:The above copyright notice and this permission notice shall be included inall copIEs or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS ORIMPLIED,INCLUDING BUT NOT liMITED TO THE WARRANTIES OF MERCHANTABIliTY,fitness FOR A PARTIculaR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR copYRIGHT HolDERS BE liABLE FOR ANY CLaim,damAGES OR OTHERliABIliTY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAliNGS INTHE SOFTWARE.****************************************************************************/package com.wenchang.killYou;import java.io.IOException;import java.io.inputStream;import java.util.Timer;import java.util.TimerTask;import org.cocos2dx.lib.Cocos2dxActivity;import org.cocos2dx.lib.Cocos2dxGLSurfaceVIEw;import com.wenchang.killYou.VIDeoVIEw.OnFinishListener;import androID.content.res.AssetfileDescriptor;import androID.content.res.AssetManager;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.drawable.BitmapDrawable;import androID.graphics.drawable.Drawable;import androID.net.Uri;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEwGroup;import androID.Widget.Imagebutton;import androID.Widget.ImageVIEw;public class killYou extends Cocos2dxActivity implements OnFinishListener{ static killYou instance; VIEwGroup group; VIDeoVIEw vIDeoVIEw; private Imagebutton btn = null; private static int msg = 0; protected voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); instance = this; group = (VIEwGroup)getwindow().getDecorVIEw(); Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { @OverrIDe public voID run() { if (killYou.msg > 0) { handler.sendEmptyMessage(killYou.msg); killYou.msg = 0; } } }; timer.schedule(timerTask,20); } public Cocos2dxGLSurfaceVIEw onCreateVIEw() { Cocos2dxGLSurfaceVIEw glSurfaceVIEw = new Cocos2dxGLSurfaceVIEw(this); // killYou should create stencil buffer glSurfaceVIEw.setEGLConfigChooser(5,6,5,16,8); return glSurfaceVIEw; } static { System.loadlibrary("cocos2dcpp"); } private voID a(String name) { Log.i("","name=" + name); vIDeoVIEw = new VIDeoVIEw(this); vIDeoVIEw.setonFinishListener(this); try { AssetfileDescriptor afd = getAssets().openFd(name); vIDeoVIEw.setVIDeo(afd); } catch (IOException e) { e.printstacktrace(); } group.addVIEw(vIDeoVIEw); vIDeoVIEw.setZOrderMediaOverlay(true); //添加跳过图片 AssetManager am = getResources().getAssets(); inputStream is = null; try { is = am.open("ui_btn_skip.png"); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } btn = new Imagebutton(this); Drawable d = Drawable.createFromStream(is,"ui_btn_skip.png"); btn.setBackgroundDrawable(d); //重新定义跳过图片宽高 float wRate = (float) (93.0 / 640.0); float hRate = (float) (44.0 / 960.0); float newWIDth = group.getWIDth() * wRate; float newHeight = group.getHeight() * hRate; androID.Widget.FrameLayout.LayoutParams para = new androID.Widget.FrameLayout.LayoutParams((int)(newWIDth),(int)(newHeight)); para.leftmargin = (int)(group.getWIDth() - newWIDth * 1.5); para.topmargin = (int)(group.getHeight() - newHeight * 1.5); para.gravity = androID.vIEw.Gravity.RIGHT; btn.setLayoutParams(para); group.addVIEw(btn); //跳过按钮事件 btn.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw arg0) { // Todo auto-generated method stub onVIDeoFinish(); }} ); } public static voID playVIDeo(final String name) { if (instance != null) { instance.runOnUiThread(new Runnable() { @OverrIDe public voID run() { instance.a(name); } }); } } public static voID sendMessage(int what) { killYou.msg = what; } private Handler handler = new Handler() { @OverrIDe public voID handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 11: Log.i("ABCDEFG","11"); instance.a("start.mp4"); break; } } }; @OverrIDe public voID onVIDeoFinish() { group.removeVIEw(vIDeoVIEw); vIDeoVIEw = null; group.removeVIEw(btn); btn = null; }}
HelloWorldScene.cpp
#include "HelloWorldScene.h"#include "layer\LoginLayer.h"#include "layer\RegisterLayer.h"#include "base\LayerManager.h"USING_NS_CC;HelloWorldScene* HelloWorldScene::create(){ HelloWorldScene *pRet = new HelloWorldScene(); if (pRet && pRet->init()) { pRet->autorelease(); return pRet; } else { CC_SAFE_DELETE(pRet); return NulL; }}// on "init" you need to initialize your instancebool HelloWorldScene::init(){ ////////////////////////////// // 1. super init first if ( !BaseScene::init() ) {return false;} //设置管理器的父场景 LMINS->setParentScene(this); //以登录layer启动 LMINS->runWithLayer(LM::LoginLayer); cclOG("PLAY vIDeo2.mp4"); //playVIDeo("vIDeo2.mp4"); sendMessage(11); return true;}#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "platform/androID/jni/JniHelper.h"#endifvoID playVIDeo(const char *name){#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo t; if (JniHelper::getStaticmethodInfo(t,"com/wenchang/killYou/killYou","playVIDeo","(Ljava/lang/String;)V")) { t.env->CallStaticVoIDMethod(t.classID,t.methodID,t.env->NewStringUTF(name)); }#endif}voID sendMessage(int what){#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo t; if (JniHelper::getStaticmethodInfo(t,"sendMessage","(I)V")) { t.env->CallStaticVoIDMethod(t.classID,what); }#endif}总结
以上是内存溢出为你收集整理的cocos2d-x之Android播放视频c++代码全部内容,希望文章能够帮你解决cocos2d-x之Android播放视频c++代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)