Android--MediaPlayer實現MP3播放小程式

來源:互聯網
上載者:User

標籤: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播放小程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.