本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!
前面写了十四篇关于界面的入门文章,大家都看完和跟着练习之后,对于常用的Layout和View都会有一定的了解了,接下来的文章就不再强调介绍界面了,而是针对具体的常见功能而展开。
本文介绍MediaPlayer的使用。MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。
先贴出本文程序运行结果的截图,上面是播放/停止音频,可用SeekBar来调进度,下面是播放/停止视频,也是用SeekBar来调进度:
main.xml的源码:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutandroid:id="@+id/LinearLayout01"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical">
- <SeekBarandroid:id="@+id/SeekBar01"android:layout_height="wrap_content"
- android:layout_width="fill_parent"></SeekBar>
- <LinearLayoutandroid:id="@+id/LinearLayout02"
- android:layout_width="wrap_content"android:layout_height="wrap_content">
- <Buttonandroid:id="@+id/Button01"android:layout_width="wrap_content"
- android:layout_height="wrap_content"android:text="播放音频"></Button>
- <Buttonandroid:id="@+id/Button02"android:layout_width="wrap_content"
- android:layout_height="wrap_content"android:text="停止播放"></Button>
- </LinearLayout>
- <SeekBarandroid:id="@+id/SeekBar02"android:layout_height="wrap_content"
- android:layout_width="fill_parent"></SeekBar>
- <SurfaceViewandroid:id="@+id/SurfaceView01"
- android:layout_width="fill_parent"android:layout_height="250px"></SurfaceView>
- <LinearLayoutandroid:id="@+id/LinearLayout02"
- android:layout_width="wrap_content"android:layout_height="wrap_content">
- <Buttonandroid:layout_width="wrap_content"
- android:layout_height="wrap_content"android:id="@+id/Button03"
- android:text="播放视频"></Button>
- <Buttonandroid:layout_width="wrap_content"
- android:layout_height="wrap_content"android:text="停止播放"android:id="@+id/Button04"></Button>
- </LinearLayout>
- </LinearLayout>
本文程序的源码,有点长:
- packagecom.testMedia;
- importjava.io.IOException;
- importjava.util.Timer;
- importjava.util.TimerTask;
- importandroid.app.Activity;
- importandroid.media.AudioManager;
- importandroid.media.MediaPlayer;
- importandroid.os.Bundle;
- importandroid.view.SurfaceHolder;
- importandroid.view.SurfaceView;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.SeekBar;
- importandroid.widget.Toast;
- publicclasstestMediaextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- privateSeekBarskb_audio=null;
- privateButtonbtn_start_audio=null;
- privateButtonbtn_stop_audio=null;
- privateSeekBarskb_video=null;
- privateButtonbtn_start_video=null;
- privateButtonbtn_stop_video=null;
- privateSurfaceViewsurfaceView;
- privateSurfaceHoldersurfaceHolder;
- privateMediaPlayerm=null;
- privateTimermTimer;
- privateTimerTaskmTimerTask;
- privatebooleanisChanging=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //----------Media控件设置---------//
- m=newMediaPlayer();
- //播放结束之后弹出提示
- m.setOnCompletionListener(newMediaPlayer.OnCompletionListener(){
- @Override
- publicvoidonCompletion(MediaPlayerarg0){
- Toast.makeText(testMedia.this,"结束",1000).show();
- m.release();
- }
- });
- //----------定时器记录播放进度---------//
- mTimer=newTimer();
- mTimerTask=newTimerTask(){
- @Override
- publicvoidrun(){
- if(isChanging==true)
- return;
- if(m.getVideoHeight()==0)
- skb_audio.setProgress(m.getCurrentPosition());
- else
- skb_video.setProgress(m.getCurrentPosition());
- }
- };
- mTimer.schedule(mTimerTask,0,10);
- btn_start_audio=(Button)this.findViewById(R.id.Button01);
- btn_stop_audio=(Button)this.findViewById(R.id.Button02);
- btn_start_audio.setOnClickListener(newClickEvent());
- btn_stop_audio.setOnClickListener(newClickEvent());
- skb_audio=(SeekBar)this.findViewById(R.id.SeekBar01);
- skb_audio.setOnSeekBarChangeListener(newSeekBarChangeEvent());
- btn_start_video=(Button)this.findViewById(R.id.Button03);
- btn_stop_video=(Button)this.findViewById(R.id.Button04);
- btn_start_video.setOnClickListener(newClickEvent());
- btn_stop_video.setOnClickListener(newClickEvent());
- skb_video=(SeekBar)this.findViewById(R.id.SeekBar02);
- skb_video.setOnSeekBarChangeListener(newSeekBarChangeEvent());
- surfaceView=(SurfaceView)findViewById(R.id.SurfaceView01);
- surfaceHolder=surfaceView.getHolder();
- surfaceHolder.setFixedSize(100,100);
- surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
- /*
- *按键事件处理
- */
- classClickEventimplementsView.OnClickListener{
- @Override
- publicvoidonClick(Viewv){
- if(v==btn_start_audio)
- {
- m.reset();//恢复到未初始化的状态
- m=MediaPlayer.create(testMedia.this,R.raw.big);//读取音频
- skb_audio.setMax(m.getDuration());//设置SeekBar的长度
- try{
- m.prepare();//准备
- }catch(IllegalStateExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- m.start();//播放
- }
- elseif(v==btn_stop_audio||v==btn_stop_video)
- {
- m.stop();
- }
- elseif(v==btn_start_video)
- {
- m.reset();//恢复到未初始化的状态
- m=MediaPlayer.create(testMedia.this,R.raw.test);//读取视频
- skb_video.setMax(m.getDuration());//设置SeekBar的长度
- m.setAudioStreamType(AudioManager.STREAM_MUSIC);
- m.setDisplay(surfaceHolder);//设置屏幕
- try{
- m.prepare();
- }catch(IllegalArgumentExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IllegalStateExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- m.start();
- }
- }
- }
- /*
- *SeekBar进度改变事件
- */
- classSeekBarChangeEventimplementsSeekBar.OnSeekBarChangeListener{
- @Override
- publicvoidonProgressChanged(SeekBarseekBar,intprogress,
- booleanfromUser){
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicvoidonStartTrackingTouch(SeekBarseekBar){
- isChanging=true;
- }
- @Override
- publicvoidonStopTrackingTouch(SeekBarseekBar){
- m.seekTo(seekBar.getProgress());
- isChanging=false;
- }
- }
- }