码迷,mamicode.com
首页 > 移动开发 > 详细

Android--MediaPlayer实现MP3播放小程序

时间:2015-01-26 22:56:41      阅读:385      评论:0      收藏:0      [点我收藏+]

标签: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> {

		@Override
		protected void onPostExecute(String result) {
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			Hello.this.seekbar.setProgress(progress[0]);//更新拖动条
		}

		@Override
		protected 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播放小程序

标签:android   mediaplayer   

原文地址:http://blog.csdn.net/yayun0516/article/details/43155563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!