標籤:風飛雪未揚 從零開始學android 安卓播放視頻 音頻 android系統播放器
在win電腦中播放視頻和音頻往往會使用到電腦當中的windowsMediaPlayer播放器,而在Android中則同樣會使用mediaplayer播放器對音頻和視頻進行播放。
是mediaplayer的生命週期
dle狀態:當使用關鍵字new執行個體化一個MediaPlayer對象或者是調用了類中的reset()方法會進入到此狀態;End狀態:當調用release()方法之後將進入到此狀態,此時會釋放掉所有佔用的硬體和軟體資源,並且不會再進入到其他的任何一種狀態了;Initialized狀態:當MediaPlayer對象設定好了要播放的媒體檔案(setDataSource())之後進入到此狀態;Prepared狀態:進入到預播放狀態(prepare()、prepareAsync()),進入到此狀態則表示目前的媒體檔案沒有任何的問題,可以使用OnPreparedListener監聽此狀態;如果使用者調用的是prepare()方法(同步)則表示該MediaPlayer對象已經進入到了Prepared狀態;如果使用者調用的是prepareAsync()方法(非同步),則表示該MediaPlayer對象進入到了Preparing狀態並返回,而內部播放引擎會繼續執行未完成的準備操作。Started狀態:進行中媒體播放(start()),此時可以使用seekTo()方法指定媒體播放的位置;Paused狀態:在Started狀態下使用Paused狀態可以暫停MediaPlayer的播放,暫停之後可以通過start()方法將其變回到Started狀態,繼續播放;Stop狀態:在Started和Paused狀態下都可以通過stop()方法停止MediaPlayer的播放,在Stop狀態下要想重新進行播放,則可以使用prepare()和prepareAsync()方法進入到就緒狀態。PlaybackCompleted狀態:當媒體播放完畢之後會進入到此狀態,使用者可以使用OnCompletionListener監聽此狀態,此時可以使用start()方法重新播放,也可以使用stop()方法停止播放,或者使用seekTo()方法來重新置放播放位置;Error狀態:當使用者播放操作之中出現了某些錯誤(檔案格式不正確、播放檔案過大等)則進入到此狀態,使用者可以使用OnErrorListener來監聽此狀態,如果MediaPlayer進入到了此狀態後可以使用reset()方法重新變回Idle狀態;
下面使用簡單的例子來使用進行音訊播放
xim檔案
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="16dp" android:src="@drawable/play" /> <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/imageButton2" android:src="@drawable/stop" /> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/imageButton1" android:layout_centerHorizontal="true" android:src="@drawable/pause" /> <ImageButton android:id="@+id/imageButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageButton1" android:layout_below="@+id/imageButton1" android:layout_marginTop="19dp" android:src="@drawable/vioceup" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/imageButton4" android:layout_centerHorizontal="true" android:layout_marginTop="14dp" android:text="等待播放音頻檔案" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageButton android:id="@+id/imageButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageButton3" android:layout_alignRight="@+id/imageButton3" android:layout_alignTop="@+id/imageButton4" android:src="@drawable/voicedown" /></RelativeLayout></span>
java檔案
<span style="font-size:18px;">package com.example.mediaplayer;import java.io.IOException;import android.app.Activity;import android.content.Context;import android.media.AudioManager;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private ImageButton play, stop, pause, voiceup, voicedown;// 設定ImageButtonprivate TextView info;// 設定資訊提示欄private MediaPlayer mediaPlayer = null;// 建立MediaPlayer對象private Boolean PauseFlag = false;// 為play設定播放狀態@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);play = (ImageButton) this.findViewById(R.id.imageButton1);stop = (ImageButton) this.findViewById(R.id.imageButton3);pause = (ImageButton) this.findViewById(R.id.imageButton2);voicedown = (ImageButton) this.findViewById(R.id.imageButton5);voiceup = (ImageButton) this.findViewById(R.id.imageButton4);info = (TextView) this.findViewById(R.id.textView1);// 獲得音量操作對象final AudioManager audio = (AudioManager) super.getSystemService(Context.AUDIO_SERVICE);// 播放按鈕的監聽play.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 執行個體化MEdiaplay對象mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.media);// 對播放完成設定監聽操作MainActivity.this.mediaPlayer.setOnCompletionListener(new OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer mp) {// TODO Auto-generated method stub// 播放完成後釋放資源mediaPlayer.release();// 設定播放按鈕的狀態play.setEnabled(true);}});if (mediaPlayer != null) {mediaPlayer.stop();}try {// 進入播放裝備狀態mediaPlayer.prepare();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(MainActivity.this, "播放出錯", 2).show();}// 開始播放音頻mediaPlayer.start();info.setText("現正播放音樂");// 播放後不能對該首歌曲繼續開啟播放play.setEnabled(false);}});// 對暫停播放的監聽pause.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (mediaPlayer != null) {// 對暫停狀態進行判斷,實現點擊播放和再次點擊暫停功能if (PauseFlag) {mediaPlayer.start();PauseFlag = false;info.setText("現正播放音樂");} else {mediaPlayer.pause();info.setText("停止播放音樂");PauseFlag = true;}}}});// 對停止按鈕的監聽stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (mediaPlayer != null) {mediaPlayer.stop();info.setText("停止播放音頻");play.setEnabled(true);}}});//對音量降低進行監聽voicedown.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubaudio.adjustVolume(AudioManager.ADJUST_LOWER, 0);Toast.makeText(MainActivity.this, "音量降低", 1).show();}});//對音量提高進行監聽voiceup.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubaudio.adjustVolume(AudioManager.ADJUST_RAISE, 0);Toast.makeText(MainActivity.this, "音量增加", 1).show();}});}}</span>
使用mediaplayer可以很方便的對音頻進行控制,大家可以參照APi實現更多的功能。
下節預報:mediaplayer系統播放器(播放視頻)
從零開始學android<Mediaplayer播放器組件(播放音頻).四十八.>