Menu Home

MediaPlayer(一)——处理网络音频文件

Android MediaPlayer状态转换图

图片来自Google Android Doc

图片来自Google Android Doc

activity_main.xml:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/TV01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Status :" />

    <TextView
        android:id="@+id/statusTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Unknown" />

    <TextView
        android:id="@+id/bufferPercentTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Buffer Percentage : " />

    <ProgressBar
        android:id="@+id/bufferSB"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100" />

    <Button
        android:id="@+id/startBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />

    <Button
        android:id="@+id/stopBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" />

</LinearLayout>

main_activity.java

public class MainActivity extends Activity implements OnCompletionListener,
		OnBufferingUpdateListener, OnClickListener, OnPreparedListener,
		OnErrorListener {

	Button startBtn;
	Button stopBtn;
	TextView statusTV;
	ProgressBar bufferSB;
	MediaPlayer mediaPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		startBtn = (Button) findViewById(R.id.startBtn);
		startBtn.setOnClickListener(this);
		startBtn.setEnabled(false);

		stopBtn = (Button) findViewById(R.id.stopBtn);
		stopBtn.setOnClickListener(this);
		stopBtn.setEnabled(false);

		statusTV = (TextView) findViewById(R.id.statusTV);
		statusTV.setText("Creating mediaPlayer");

		bufferSB = (ProgressBar) findViewById(R.id.bufferSB);

		mediaPlayer = new MediaPlayer();
		mediaPlayer.setOnCompletionListener(this);
		mediaPlayer.setOnBufferingUpdateListener(this);
		mediaPlayer.setOnPreparedListener(this);
		mediaPlayer.setOnErrorListener(this);

		statusTV.setText("Created mediaPlayer");

		statusTV.setText("seting DataSource");
		try {
			mediaPlayer.setDataSource("http://lnmcc.net/wordpress/wp-content/uploads/2013/10/Rolling-In-The-Deep.mp3");
			statusTV.setText("setted DataSource");

			statusTV.setText("calling prepareAsync");
			// 不同于prepare(), prepareAsync()会立即返回,后台开始缓冲
			mediaPlayer.prepareAsync();
		} catch (IOException e) {
			Log.v("mediaPlayer.setDataSource", e.getMessage());
		}
	}

	@Override
	public void onClick(View v) {

		if (v == startBtn) {
			mediaPlayer.start();
			statusTV.setText("start play");

			startBtn.setEnabled(false);
			stopBtn.setEnabled(true);
		} else if (v == stopBtn) {
			mediaPlayer.pause();
			statusTV.setText("pause play");

			stopBtn.setEnabled(false);
			startBtn.setEnabled(true);
		}
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {

		statusTV.setText("invoke onError");

		switch (what) {
		case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
			statusTV.setText("MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK" + extra);
			break;
		case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
			statusTV.setText("MEDIA_ERROR_SERVER_DIED" + extra);
			break;
		case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
			statusTV.setText("MEDIA_ERROR_UNSUPPORTED" + extra);
			break;
		case MediaPlayer.MEDIA_ERROR_UNKNOWN:
			statusTV.setText("MEDIA_ERROR_UNKNOWN" + extra);
			break;
		}
		return false;
	}

	@Override
	public void onCompletion(MediaPlayer mp) {

		statusTV.setText("invoke onCompletion");
		stopBtn.setEnabled(false);
		startBtn.setEnabled(true);
	}

	@Override
	// 当后台缓冲数据发生变化时,会调用这个方法
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		bufferSB.setProgress(percent);
	}
	
	@Override
	//数据缓冲完成,可以播放
	public void onPrepared(MediaPlayer mp) {
		
		statusTV.setText("invoke onPrepared");
		startBtn.setEnabled(true);
	}

}

最后要在AndroidManifest.xml中加入Internet权限:

<uses-permission android:name="android.permission.INTERNET" />

git clone https://github.com/lnmcc/NetAudioPlayerExample.git

Leave a Reply

Your email address will not be published.