標籤:android mediaplayer
Main.xml程式如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="等待音頻檔案播放..." /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@+drawable/play" /> <ImageButton android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@+drawable/pause" /> <ImageButton android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@+drawable/stop" /> </LinearLayout> <SeekBar android:id="@+id/seekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
.java程式如下:
package org.lxh.demo;import java.io.IOException;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnFocusChangeListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageButton;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;public class Hello extends Activity {private ImageButton play = null;private ImageButton pause = null;private ImageButton stop = null;private TextView info = null;private MediaPlayer myMediaPlayer = null;private boolean pauseFlag = false;private boolean playFlag = true;private SeekBar seekbar = null;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); // 生命週期方法super.setContentView(R.layout.main); // 設定要使用的布局管理器this.info = (TextView) super.findViewById(R.id.info);this.play = (ImageButton) super.findViewById(R.id.play);this.pause = (ImageButton) super.findViewById(R.id.pause);this.stop = (ImageButton) super.findViewById(R.id.stop);this.seekbar = (SeekBar) super.findViewById(R.id.seekbar);this.play.setOnClickListener(new PlayOnClickListener());this.pause.setOnClickListener(new PauseOnClickListener());this.stop.setOnClickListener(new StopOnClickListener());this.seekbar.setOnSeekBarChangeListener(new SeekBarChangeListener());}private class PlayOnClickListener implements OnClickListener {public void onClick(View arg0) {if (Hello.this.myMediaPlayer == null) {Hello.this.myMediaPlayer = MediaPlayer.create(Hello.this,R.raw.mldn_ad);// 找到資源Hello.this.myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {// 檔案播放完public void onCompletion(MediaPlayer myMediaPlayer) {Hello.this.playFlag = false;Hello.this.myMediaPlayer.release();}});Hello.this.seekbar.setMax(Hello.this.myMediaPlayer.getDuration());UpdateSeekBar update = new UpdateSeekBar();update.execute(1000);try {Hello.this.myMediaPlayer.prepare();} catch (IllegalStateException e) {Hello.this.info.setText("檔案播放異常..." + e);} catch (IOException e) {Hello.this.info.setText("檔案播放異常..." + e);}Hello.this.myMediaPlayer.start();Hello.this.info.setText("現正播放音頻檔案...");}}}private class UpdateSeekBar extends AsyncTask<Integer, Integer, String> {@Overrideprotected void onPostExecute(String result) {}@Overrideprotected void onProgressUpdate(Integer... progress) {Hello.this.seekbar.setProgress(progress[0]);//更新拖動條}@Overrideprotected String doInBackground(Integer... params) {while (Hello.this.playFlag) {try {Thread.sleep(params[0]);} catch (InterruptedException e) {e.printStackTrace();}this.publishProgress(Hello.this.myMediaPlayer.getCurrentPosition());}return null;}}private class PauseOnClickListener implements OnClickListener {public void onClick(View arg0) {if (Hello.this.myMediaPlayer != null) {if (Hello.this.pauseFlag) {Hello.this.myMediaPlayer.start();Hello.this.pauseFlag = false;} else {Hello.this.myMediaPlayer.pause();Hello.this.pauseFlag = true;}}}}private class StopOnClickListener implements OnClickListener {public void onClick(View arg0) {if (Hello.this.myMediaPlayer != null) {Hello.this.myMediaPlayer.stop();Hello.this.info.setText("停止播放音頻");}}}private class SeekBarChangeListener implements OnSeekBarChangeListener {public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {}public void onStartTrackingTouch(SeekBar arg0) {}public void onStopTrackingTouch(SeekBar arg0) {//拖動進度條後Hello.this.myMediaPlayer.seekTo(Hello.this.seekbar.getProgress());}}}運行執行個體如下:
註:本執行個體可能還有很多BUG,僅用於MediaPlayer的基本使用展示。
Android--MediaPlayer實現MP3播放小程式