Android提高之MediaPlayer音视频播放

时间:2022-09-23 18:00:48

前面文章已经详细介绍了android界面的入门技术,相信大家在看完和跟着练习之后,会对于常用的layout和view都会有一定的了解了,接下来就不再强调介绍界面了,而是针对具体的常见功能而展开。

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

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

Android提高之MediaPlayer音视频播放

main.xml的源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>
<linearlayout android: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">
 <seekbar android:id="@+id/seekbar01" android:layout_height="wrap_content"
 android:layout_width="fill_parent"></seekbar>
 <linearlayout android:id="@+id/linearlayout02"
 android:layout_width="wrap_content" android:layout_height="wrap_content">
 <button android:id="@+id/button01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="播放音频"></button>
 <button android:id="@+id/button02" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="停止播放"></button>
 </linearlayout>
 <seekbar android:id="@+id/seekbar02" android:layout_height="wrap_content"
 android:layout_width="fill_parent"></seekbar>
 
 <surfaceview android:id="@+id/surfaceview01"
 android:layout_width="fill_parent" android:layout_height="250px"></surfaceview>
 <linearlayout android:id="@+id/linearlayout02"
 android:layout_width="wrap_content" android:layout_height="wrap_content">
 <button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:id="@+id/button03"
  android:text="播放视频"></button>
 <button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/button04"></button>
 </linearlayout>
</linearlayout>

java部分源码有点长,需要大家耐心解读,详细程序如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.testmedia;
import java.io.ioexception;
import java.util.timer;
import java.util.timertask;
import android.app.activity;
import android.media.audiomanager;
import android.media.mediaplayer;
import android.os.bundle;
import android.view.surfaceholder;
import android.view.surfaceview;
import android.view.view;
import android.widget.button;
import android.widget.seekbar;
import android.widget.toast;
 
 
public class testmedia extends activity {
  /** called when the activity is first created. */
 private seekbar skb_audio=null;
  private button btn_start_audio = null;
  private button btn_stop_audio = null;
 
  private seekbar skb_video=null;
  private button btn_start_video = null;
  private button btn_stop_video = null;
  private surfaceview surfaceview;
  private surfaceholder surfaceholder;
  
  private mediaplayer m = null;
  private timer mtimer;
  private timertask mtimertask;
  
  private boolean ischanging=false;//互斥变量,防止定时器与seekbar拖动时进度冲突
   @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    
    //----------media控件设置---------//
    m=new mediaplayer();
    
    //播放结束之后弹出提示
    m.setoncompletionlistener(new mediaplayer.oncompletionlistener(){
  @override
  public void oncompletion(mediaplayer arg0) {
  toast.maketext(testmedia.this, "结束", 1000).show();
  m.release();
  }
    });
    
   //----------定时器记录播放进度---------//
    mtimer = new timer();
    mtimertask = new timertask() {
      @override
      public void run() {
       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(new clickevent());
    btn_stop_audio.setonclicklistener(new clickevent());
    skb_audio=(seekbar)this.findviewbyid(r.id.seekbar01);
    skb_audio.setonseekbarchangelistener(new seekbarchangeevent());
    
    btn_start_video = (button) this.findviewbyid(r.id.button03);
    btn_stop_video = (button) this.findviewbyid(r.id.button04);
    btn_start_video.setonclicklistener(new clickevent());
    btn_stop_video.setonclicklistener(new clickevent());
    skb_video=(seekbar)this.findviewbyid(r.id.seekbar02);
    skb_video.setonseekbarchangelistener(new seekbarchangeevent());
    surfaceview = (surfaceview) findviewbyid(r.id.surfaceview01);
    surfaceholder = surfaceview.getholder();
    surfaceholder.setfixedsize(100, 100);
    surfaceholder.settype(surfaceholder.surface_type_push_buffers);
  }
 /*
  * 按键事件处理
  */
 class clickevent implements view.onclicklistener{
 @override
 public void onclick(view v) {
 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 (illegalstateexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
  } catch (ioexception e) { 
  // todo auto-generated catch block 
  e.printstacktrace(); 
  }
  m.start(); //播放
 }
 else if(v==btn_stop_audio || v==btn_stop_video)
 {
  m.stop();
 }
 else if(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 (illegalargumentexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
  } catch (illegalstateexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
  } catch (ioexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
  }
  m.start();
 }
 }
 }
 /*
  * seekbar进度改变事件
  */
 class seekbarchangeevent implements seekbar.onseekbarchangelistener{
 @override
 public void onprogresschanged(seekbar seekbar, int progress,
  boolean fromuser) {
 // todo auto-generated method stub
 }
 @override
 public void onstarttrackingtouch(seekbar seekbar) {
   ischanging=true;
 }
 
 @override
 public void onstoptrackingtouch(seekbar seekbar) {
 m.seekto(seekbar.getprogress());
   ischanging=false;
 }
 }
}

希望本文所述关于mediaplayer的使用能对大家有所帮助。