Android提高第一篇之MediaPlayer

本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!

前面写了十四篇关于界面的入门文章,大家都看完和跟着练习之后,对于常用的Layout和View都会有一定的了解了,接下来的文章就不再强调介绍界面了,而是针对具体的常见功能而展开。

本文介绍MediaPlayer的使用。MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。

先贴出本文程序运行结果的截图,上面是播放/停止音频,可用SeekBar来调进度,下面是播放/停止视频,也是用SeekBar来调进度:

main.xml的源码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutandroid:id="@+id/LinearLayout01"
  3. android:layout_width="fill_parent"android:layout_height="fill_parent"
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:orientation="vertical">
  6. <SeekBarandroid:id="@+id/SeekBar01"android:layout_height="wrap_content"
  7. android:layout_width="fill_parent"></SeekBar>
  8. <LinearLayoutandroid:id="@+id/LinearLayout02"
  9. android:layout_width="wrap_content"android:layout_height="wrap_content">
  10. <Buttonandroid:id="@+id/Button01"android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"android:text="播放音频"></Button>
  12. <Buttonandroid:id="@+id/Button02"android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"android:text="停止播放"></Button>
  14. </LinearLayout>
  15. <SeekBarandroid:id="@+id/SeekBar02"android:layout_height="wrap_content"
  16. android:layout_width="fill_parent"></SeekBar>
  17. <SurfaceViewandroid:id="@+id/SurfaceView01"
  18. android:layout_width="fill_parent"android:layout_height="250px"></SurfaceView>
  19. <LinearLayoutandroid:id="@+id/LinearLayout02"
  20. android:layout_width="wrap_content"android:layout_height="wrap_content">
  21. <Buttonandroid:layout_width="wrap_content"
  22. android:layout_height="wrap_content"android:id="@+id/Button03"
  23. android:text="播放视频"></Button>
  24. <Buttonandroid:layout_width="wrap_content"
  25. android:layout_height="wrap_content"android:text="停止播放"android:id="@+id/Button04"></Button>
  26. </LinearLayout>
  27. </LinearLayout>

本文程序的源码,有点长:

  1. packagecom.testMedia;
  2. importjava.io.IOException;
  3. importjava.util.Timer;
  4. importjava.util.TimerTask;
  5. importandroid.app.Activity;
  6. importandroid.media.AudioManager;
  7. importandroid.media.MediaPlayer;
  8. importandroid.os.Bundle;
  9. importandroid.view.SurfaceHolder;
  10. importandroid.view.SurfaceView;
  11. importandroid.view.View;
  12. importandroid.widget.Button;
  13. importandroid.widget.SeekBar;
  14. importandroid.widget.Toast;
  15. publicclasstestMediaextendsActivity{
  16. /**Calledwhentheactivityisfirstcreated.*/
  17. privateSeekBarskb_audio=null;
  18. privateButtonbtn_start_audio=null;
  19. privateButtonbtn_stop_audio=null;
  20. privateSeekBarskb_video=null;
  21. privateButtonbtn_start_video=null;
  22. privateButtonbtn_stop_video=null;
  23. privateSurfaceViewsurfaceView;
  24. privateSurfaceHoldersurfaceHolder;
  25. privateMediaPlayerm=null;
  26. privateTimermTimer;
  27. privateTimerTaskmTimerTask;
  28. privatebooleanisChanging=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突
  29. @Override
  30. publicvoidonCreate(BundlesavedInstanceState){
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. //----------Media控件设置---------//
  34. m=newMediaPlayer();
  35. //播放结束之后弹出提示
  36. m.setOnCompletionListener(newMediaPlayer.OnCompletionListener(){
  37. @Override
  38. publicvoidonCompletion(MediaPlayerarg0){
  39. Toast.makeText(testMedia.this,"结束",1000).show();
  40. m.release();
  41. }
  42. });
  43. //----------定时器记录播放进度---------//
  44. mTimer=newTimer();
  45. mTimerTask=newTimerTask(){
  46. @Override
  47. publicvoidrun(){
  48. if(isChanging==true)
  49. return;
  50. if(m.getVideoHeight()==0)
  51. skb_audio.setProgress(m.getCurrentPosition());
  52. else
  53. skb_video.setProgress(m.getCurrentPosition());
  54. }
  55. };
  56. mTimer.schedule(mTimerTask,0,10);
  57. btn_start_audio=(Button)this.findViewById(R.id.Button01);
  58. btn_stop_audio=(Button)this.findViewById(R.id.Button02);
  59. btn_start_audio.setOnClickListener(newClickEvent());
  60. btn_stop_audio.setOnClickListener(newClickEvent());
  61. skb_audio=(SeekBar)this.findViewById(R.id.SeekBar01);
  62. skb_audio.setOnSeekBarChangeListener(newSeekBarChangeEvent());
  63. btn_start_video=(Button)this.findViewById(R.id.Button03);
  64. btn_stop_video=(Button)this.findViewById(R.id.Button04);
  65. btn_start_video.setOnClickListener(newClickEvent());
  66. btn_stop_video.setOnClickListener(newClickEvent());
  67. skb_video=(SeekBar)this.findViewById(R.id.SeekBar02);
  68. skb_video.setOnSeekBarChangeListener(newSeekBarChangeEvent());
  69. surfaceView=(SurfaceView)findViewById(R.id.SurfaceView01);
  70. surfaceHolder=surfaceView.getHolder();
  71. surfaceHolder.setFixedSize(100,100);
  72. surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  73. }
  74. /*
  75. *按键事件处理
  76. */
  77. classClickEventimplementsView.OnClickListener{
  78. @Override
  79. publicvoidonClick(Viewv){
  80. if(v==btn_start_audio)
  81. {
  82. m.reset();//恢复到未初始化的状态
  83. m=MediaPlayer.create(testMedia.this,R.raw.big);//读取音频
  84. skb_audio.setMax(m.getDuration());//设置SeekBar的长度
  85. try{
  86. m.prepare();//准备
  87. }catch(IllegalStateExceptione){
  88. //TODOAuto-generatedcatchblock
  89. e.printStackTrace();
  90. }catch(IOExceptione){
  91. //TODOAuto-generatedcatchblock
  92. e.printStackTrace();
  93. }
  94. m.start();//播放
  95. }
  96. elseif(v==btn_stop_audio||v==btn_stop_video)
  97. {
  98. m.stop();
  99. }
  100. elseif(v==btn_start_video)
  101. {
  102. m.reset();//恢复到未初始化的状态
  103. m=MediaPlayer.create(testMedia.this,R.raw.test);//读取视频
  104. skb_video.setMax(m.getDuration());//设置SeekBar的长度
  105. m.setAudioStreamType(AudioManager.STREAM_MUSIC);
  106. m.setDisplay(surfaceHolder);//设置屏幕
  107. try{
  108. m.prepare();
  109. }catch(IllegalArgumentExceptione){
  110. //TODOAuto-generatedcatchblock
  111. e.printStackTrace();
  112. }catch(IllegalStateExceptione){
  113. //TODOAuto-generatedcatchblock
  114. e.printStackTrace();
  115. }catch(IOExceptione){
  116. //TODOAuto-generatedcatchblock
  117. e.printStackTrace();
  118. }
  119. m.start();
  120. }
  121. }
  122. }
  123. /*
  124. *SeekBar进度改变事件
  125. */
  126. classSeekBarChangeEventimplementsSeekBar.OnSeekBarChangeListener{
  127. @Override
  128. publicvoidonProgressChanged(SeekBarseekBar,intprogress,
  129. booleanfromUser){
  130. //TODOAuto-generatedmethodstub
  131. }
  132. @Override
  133. publicvoidonStartTrackingTouch(SeekBarseekBar){
  134. isChanging=true;
  135. }
  136. @Override
  137. publicvoidonStopTrackingTouch(SeekBarseekBar){
  138. m.seekTo(seekBar.getProgress());
  139. isChanging=false;
  140. }
  141. }
  142. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值