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){
}
}
最主要的是要注意视频文件的路径。
相关文章推荐
- Android开发之初探视频的播放
- Android视频播放项目总结之 使用第三方Vitamio库,开发万能播放器(三)
- Android开发之MediaPlayer和SurfaceView播放视频
- Android应用开发学习笔记之播放视频
- Android开发之视频录制与播放
- android使用video开发简单的视频播放!
- Android开发之使用VideoView播放视频
- Android开发之初探视频的播放
- Android开发之MediaPlayer多媒体(音频,视频)播放工具类
- Android开发VR实战之播放360度全景视频
- [置顶] Android开发实战使用(VR技术实现360°全景视频播放功能)
- 【Android开发VR实战】二.播放360°全景视频(自用笔记)
- android开发之播放资源文件中的视频
- Android应用开发学习笔记之播放视频
- android开发视频播放器-----视频播放 Vitamio框架的使用(一)
- 【Android开发VR实战】二.播放360°全景视频
- 【Android开发VR实战】二.播放360°全景视频
- 【Android开发】多媒体应用开发-使用VideoView播放视频
- Android开发之播放RTSP协议视频流
- android开发视频播放相关使用