您的位置:首页 > 移动开发 > Android开发

android视频播放的开发

2011-07-31 01:23 393 查看
目前android自带的解码库只支持MP4和3gp两种格式如果你下载"Rockplayer"的应用程序,该程序支持几乎所有的视频格式.

当你向设备加载视频的时候,视频最好是英文名称,因为android很少支持中文。


XML配置:

<?xmlversion="1.0"encoding="utf-8"?>
<!--XML的版本以及编码方式-->
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--添加一个垂直的线性布局-->
<SurfaceViewandroid:id="@+id/surfaceView"
android:layout_width="420px"
android:layout_height="300px"/>
<!--添加一个SurfaceView用于播放视频-->
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!--添加一个线性布局-->
<Buttonandroid:id="@+id/play2_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"/>
<!--添加一个按钮-->
<Buttonandroid:id="@+id/pause2_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"/>
<!--添加一个按钮-->
</LinearLayout>
</LinearLayout>

java代码:

packagejm.out;
importandroid.app.Activity;//引入Activity类
importandroid.graphics.PixelFormat;//引入PixelFormat类
importandroid.media.AudioManager;//引入AudioManager类
importandroid.media.MediaPlayer;//引入MediaPlayer类
importandroid.os.Bundle;//引入Bundle类
importandroid.view.SurfaceHolder;//引入SurfaceHolder类
importandroid.view.SurfaceView;//引入SurfaceView类
importandroid.view.View;//引入View类
importandroid.view.View.OnClickListener;//引入OnClickListener类
importandroid.widget.Button;//引入Button类
publicclassVedioActivityextendsActivityimplementsOnClickListener,SurfaceHolder.Callback{
Stringpath="/sdcard/app/liudehua.3gp";
Buttonplay_Button;
Buttonpause_Button;
booleanisPause=false;
SurfaceHoldersurfaceHolder;
MediaPlayermediaPlayer;
SurfaceViewsurfaceView;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play_Button=(Button)findViewById(R.id.play2_Button);
play_Button.setOnClickListener(this);
pause_Button=(Button)findViewById(R.id.pause2_Button);
pause_Button.setOnClickListener(this);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView=(SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176,144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer=newMediaPlayer();
}
publicvoidonClick(Viewv){
if(v==play_Button){//按下播放电影按钮
isPause=false;
playVideo(path);
}
elseif(v==pause_Button){//按下暂停按钮,
if(isPause==false){//如果正在播放则将其暂停
mediaPlayer.pause();
isPause=true;
}
else{//如果暂停中怎继续播放
mediaPlayer.start();
isPause=false;
}
}
}
privatevoidplayVideo(StringstrPath){//自定义播放影片函数
if(mediaPlayer.isPlaying()==true){
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);//设置Video影片以SurfaceHolder播放
try{
mediaPlayer.setDataSource(strPath);
mediaPlayer.prepare();
}
catch(Exceptione){
e.printStackTrace();
}
mediaPlayer.start();
}
publicvoidsurfaceChanged(SurfaceHolderarg0,intarg1,intarg2,intarg3){
}
publicvoidsurfaceCreated(SurfaceHolderarg0){
}
publicvoidsurfaceDestroyed(SurfaceHolderarg0){
}
}
最主要的是要注意视频文件的路径。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: