21天學(xué)習(xí)android開(kāi)發(fā)教程之MediaPlayer
本文介紹MediaPlayer的使用。MediaPlayer可以播放音頻和視頻,另外也可以通過(guò)VideoView來(lái)播放視頻,雖然VideoView比MediaPlayer簡(jiǎn)單易用,但定制性不如用MediaPlayer,要視情況選擇了。MediaPlayer播放音頻比較簡(jiǎn)單,但是要播放視頻就需要SurfaceView。SurfaceView比普通的自定義View更有繪圖上的優(yōu)勢(shì),它支持完全的OpenGL ES庫(kù)。
先貼出本文程序運(yùn)行結(jié)果的截圖,上面是播放/停止音頻,可用SeekBar來(lái)調(diào)進(jìn)度,下面是播放/停止視頻,也是用SeekBar來(lái)調(diào)進(jìn)度:

main.xml的源碼:
<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">
<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 android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="停止播放">
<seekbar android:id="@+id/SeekBar02" android:layout_height="wrap_content"
android:layout_width="fill_parent">
<surfaceview android:id="@+id/SurfaceView01"
android:layout_width="fill_parent" android:layout_height="250px">
<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 android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/Button04">
本文程序的源碼,有點(diǎn)長(zhǎng):
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;//互斥變量,防止定時(shí)器與SeekBar拖動(dòng)時(shí)進(jìn)度沖突
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//----------Media控件設(shè)置---------//
m=new MediaPlayer();
//播放結(jié)束之后彈出提示
m.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(testMedia.this, "結(jié)束", 1000).show();
m.release();
}
});
//----------定時(shí)器記錄播放進(jìn)度---------//
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();//恢復(fù)到未初始化的狀態(tài)
m=MediaPlayer.create(testMedia.this, R.raw.big);//讀取音頻
skb_audio.setMax(m.getDuration());//設(shè)置SeekBar的長(zhǎng)度
try {
m.prepare(); //準(zhǔn)備
} 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();//恢復(fù)到未初始化的狀態(tài)
m=MediaPlayer.create(testMedia.this, R.raw.test);//讀取視頻
skb_video.setMax(m.getDuration());//設(shè)置SeekBar的長(zhǎng)度
m.setAudioStreamType(AudioManager.STREAM_MUSIC);
m.setDisplay(surfaceHolder);//設(shè)置屏幕
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進(jìn)度改變事件
*/
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;
}
}
}
以上就是21天學(xué)習(xí)android開(kāi)發(fā)教程第一篇關(guān)于MediaPlayer的相關(guān)介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。
- 深入Android MediaPlayer的使用方法詳解
- Android提高之MediaPlayer播放網(wǎng)絡(luò)視頻的實(shí)現(xiàn)方法
- Android提高之MediaPlayer播放網(wǎng)絡(luò)音頻的實(shí)現(xiàn)方法
- Android提高之MediaPlayer音視頻播放
- 淺析Android 的 MediaPlayer類
- Android MediaPlayer實(shí)現(xiàn)音樂(lè)播放器實(shí)例代碼
- Android 使用mediaplayer播放res/raw文件夾中的音樂(lè)的實(shí)例
- Android實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放器(MediaPlayer)
- android多媒體音樂(lè)(MediaPlayer)播放器制作代碼
- Android多媒體應(yīng)用使用MediaPlayer播放音頻
相關(guān)文章
Android生存指南之:開(kāi)發(fā)中的注意事項(xiàng)
本篇文章是對(duì)在Android開(kāi)發(fā)中的一些注意事項(xiàng),需要的朋友可以參考下2013-05-05
Android中利用zxing實(shí)現(xiàn)自己的二維碼掃描識(shí)別詳解
這篇文章主要給大家介紹了關(guān)于Android中利用zxing實(shí)現(xiàn)自己的二維碼掃描識(shí)別的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用zxing具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-09-09
Android動(dòng)畫 實(shí)現(xiàn)開(kāi)關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼
這篇文章主要介紹了Android動(dòng)畫 實(shí)現(xiàn)開(kāi)關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android10 啟動(dòng)之SystemServer源碼分析
這篇文章主要為大家介紹了Android10 啟動(dòng)之SystemServer源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11
Flutter彈性布局Flex水平排列Row垂直排列Column使用示例
這篇文章主要為大家介紹了Flutter彈性布局Flex水平排列Row垂直排列Column使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
android SectorMenuView底部導(dǎo)航扇形菜單的實(shí)現(xiàn)代碼
這篇文章主要介紹了android SectorMenuView底部導(dǎo)航扇形菜單的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Kotlin的Collection與Sequence操作異同點(diǎn)詳解
這篇文章主要介紹了Kotlin的Collection與Sequence操作異同點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android 高德地圖之poi搜索功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了android 高德地圖之poi搜索功能的實(shí)現(xiàn)代碼,在實(shí)現(xiàn)此功能時(shí)遇到很多問(wèn)題,在文章都給大家提到,需要的朋友可以參考下2017-08-08

