A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈君 金牌黑马   /  2014-9-23 22:29  /  1281 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. main.xml的源码:

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

  3. package com.testMedia;

  4. import java.io.IOException;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7. import android.app.Activity;
  8. import android.media.AudioManager;
  9. import android.media.MediaPlayer;
  10. import android.os.Bundle;
  11. import android.view.SurfaceHolder;
  12. import android.view.SurfaceView;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.SeekBar;
  16. import android.widget.Toast;

  17. public class testMedia extends Activity {
  18. /** Called when the activity is first created. */

  19. private SeekBar skb_audio = null;
  20. private Button btn_start_audio = null;
  21. private Button btn_stop_audio = null;

  22. private SeekBar skb_video = null;
  23. private Button btn_start_video = null;
  24. private Button btn_stop_video = null;
  25. private SurfaceView surfaceView;
  26. private SurfaceHolder surfaceHolder;

  27. private MediaPlayer m = null;
  28. private Timer mTimer;
  29. private TimerTask mTimerTask;

  30. private boolean isChanging = false;// 互斥变量,防止定时器与SeekBar拖动时进度冲突

  31. @Override
  32. public void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.main);

  35. // ----------Media控件设置---------//
  36. m = new MediaPlayer();

  37. // 播放结束之后弹出提示
  38. m.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  39. @Override
  40. public void onCompletion(MediaPlayer arg0) {
  41. Toast.makeText(testMedia.this, "结束", 1000).show();
  42. m.release();
  43. }
  44. });

  45. // ----------定时器记录播放进度---------//
  46. mTimer = new Timer();
  47. mTimerTask = new TimerTask() {
  48. @Override
  49. public void run() {
  50. if (isChanging == true)
  51. return;

  52. if (m.getVideoHeight() == 0)
  53. skb_audio.setProgress(m.getCurrentPosition());
  54. else
  55. skb_video.setProgress(m.getCurrentPosition());
  56. }
  57. };

  58. mTimer.schedule(mTimerTask, 0, 10);

  59. btn_start_audio = (Button) this.findViewById(R.id.Button01);
  60. btn_stop_audio = (Button) this.findViewById(R.id.Button02);
  61. btn_start_audio.setOnClickListener(new ClickEvent());
  62. btn_stop_audio.setOnClickListener(new ClickEvent());
  63. skb_audio = (SeekBar) this.findViewById(R.id.SeekBar01);
  64. skb_audio.setOnSeekBarChangeListener(new SeekBarChangeEvent());

  65. btn_start_video = (Button) this.findViewById(R.id.Button03);
  66. btn_stop_video = (Button) this.findViewById(R.id.Button04);
  67. btn_start_video.setOnClickListener(new ClickEvent());
  68. btn_stop_video.setOnClickListener(new ClickEvent());
  69. skb_video = (SeekBar) this.findViewById(R.id.SeekBar02);
  70. skb_video.setOnSeekBarChangeListener(new SeekBarChangeEvent());
  71. surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);
  72. surfaceHolder = surfaceView.getHolder();
  73. surfaceHolder.setFixedSize(100, 100);
  74. surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  75. }

  76. // ----------按键事件处理---------//
  77. class ClickEvent implements View.OnClickListener {
  78. @Override
  79. public void onClick(View v) {
  80. if (v == btn_start_audio) {
  81. m.reset();// 恢复到未初始化的状态
  82. m = MediaPlayer.create(testMedia.this, R.raw.big);// 读取音频
  83. skb_audio.setMax(m.getDuration());// 设置SeekBar的长度
  84. try {
  85. m.prepare(); // 准备
  86. } catch (IllegalStateException e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. } catch (IOException e) {
  90. // TODO Auto-generated catch block
  91. e.printStackTrace();
  92. }
  93. m.start(); // 播放
  94. } else if (v == btn_stop_audio || v == btn_stop_video) {
  95. m.stop();
  96. } else if (v == btn_start_video) {
  97. m.reset();// 恢复到未初始化的状态
  98. m = MediaPlayer.create(testMedia.this, R.raw.test);// 读取视频
  99. skb_video.setMax(m.getDuration());// 设置SeekBar的长度
  100. m.setAudioStreamType(AudioManager.STREAM_MUSIC);
  101. m.setDisplay(surfaceHolder);// 设置屏幕

  102. try {
  103. m.prepare();

  104. } catch (IllegalArgumentException e) {
  105. // TODO Auto-generated catch block
  106. e.printStackTrace();
  107. } catch (IllegalStateException e) {
  108. // TODO Auto-generated catch block
  109. e.printStackTrace();
  110. } catch (IOException e) {
  111. // TODO Auto-generated catch block
  112. e.printStackTrace();
  113. }
  114. m.start();
  115. }

  116. }
  117. }

  118. // ----------SeekBar进度改变事件---------//
  119. class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
  120. @Override
  121. public void onProgressChanged(SeekBar seekBar, int progress,
  122. boolean fromUser) {
  123. // TODO Auto-generated method stub
  124. }

  125. @Override
  126. public void onStartTrackingTouch(SeekBar seekBar) {
  127. isChanging = true;
  128. }

  129. @Override
  130. public void onStopTrackingTouch(SeekBar seekBar) {
  131. m.seekTo(seekBar.getProgress());
  132. isChanging = false;
  133. }
  134. }
  135. }
复制代码

本文介绍Android MediaPlayer的使用,MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。
先贴出本文程序运行结果的截图,上面是播放/停止音频,可用SeekBar来调进度,下面是播放/停止视频,也是用SeekBar来调进度:

2 个回复

倒序浏览
{:3_62:}{:3_62:}{:3_62:}
回复 使用道具 举报
先收藏着以后多多的看,基础还待加强呀
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马