Android开发入门之音乐播放器

这里没有用到Service。

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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/file_name" />

    <EditText
        android:id="@+id/et_file_name"
        android:text="Bruno Mars - Count On Me.mp3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="@string/play" />

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="@string/pause" />

        <Button
            android:id="@+id/btn_restart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="@string/restart" />

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="@string/stop" />
    </LinearLayout>

</LinearLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">音乐播放器</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="file_name">音乐文件</string>
    <string name="play">播放</string>
    <string name="pause">暂停</string>
    <string name="continu">继续</string>
    <string name="restart">重播</string>
    <string name="stop">停止</string>

</resources>

MainActivity.java:

package cn.leigo.mediaplayer;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText mFileNameEditText;
	private String path;
	private MediaPlayer mediaPlayer;
	private boolean pause;
	private int position;
	private TelephonyManager telephonyManager;

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

		mFileNameEditText = (EditText) findViewById(R.id.et_file_name);
		mediaPlayer = new MediaPlayer();

		telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		telephonyManager.listen(new MyPhoneStateListener(),
				PhoneStateListener.LISTEN_CALL_STATE);
	}

	private final class MyPhoneStateListener extends PhoneStateListener {

		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
			switch (state) {
			case TelephonyManager.CALL_STATE_RINGING:
				if (mediaPlayer.isPlaying()) {
					position = mediaPlayer.getCurrentPosition();
					mediaPlayer.stop();
				}
				break;

			case TelephonyManager.CALL_STATE_OFFHOOK:

				break;

			case TelephonyManager.CALL_STATE_IDLE:
				if (position > 0 && path != null) {
					play(position);
					position = 0;
				}
				break;

			default:
				break;
			}
		}

	}

	// @Override
	// protected void onPause() {
	// if (mediaPlayer.isPlaying()) {
	// position = mediaPlayer.getCurrentPosition();
	// mediaPlayer.stop();
	// }
	// super.onPause();
	// }
	//
	// @Override
	// protected void onResume() {
	// if (position > 0 && path != null) {
	// play(position);
	// position = 0;
	// }
	// super.onResume();
	// }

	@Override
	protected void onDestroy() {
		if (mediaPlayer != null) {
			mediaPlayer.release();
			mediaPlayer = null;
		}
		super.onDestroy();
	}

	public void doClick(View v) {
		switch (v.getId()) {
		case R.id.btn_play:
			String fileName = mFileNameEditText.getText().toString();
			File file = new File(Environment.getExternalStorageDirectory(),
					fileName);
			if (file.exists()) {
				path = file.getAbsolutePath();
				play(0);
			} else {
				path = null;
				Toast.makeText(this, "文件不存在!", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btn_pause:
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.pause(); // 暂停
				pause = true;
				((Button) v).setText(R.string.continu);
			} else {
				if (pause) {
					mediaPlayer.start(); // 继续播放
					pause = false;
					((Button) v).setText(R.string.pause);
				}
			}
			break;
		case R.id.btn_restart:
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.seekTo(0); // 从开始位置播放音乐
			} else {
				if (path != null) {
					play(0);
				}
			}
			break;
		case R.id.btn_stop:
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
			}
			break;
		default:
			break;
		}
	}

	private void play(int position) {
		try {
			mediaPlayer.reset(); // 把各项参数回复到初始状态
			mediaPlayer.setDataSource(path);
			mediaPlayer.prepare(); // 进行缓冲
			mediaPlayer.setOnPreparedListener(new MyPreparedListener(position));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private final class MyPreparedListener implements OnPreparedListener {
		private int position;

		public MyPreparedListener(int position) {
			this.position = position;
		}

		@Override
		public void onPrepared(MediaPlayer mp) {
			mediaPlayer.start();
			if (position > 0) {
				mediaPlayer.seekTo(position);
			}
		}
	}

}

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

Android开发入门之音乐播放器_第1张图片

你可能感兴趣的:(android,音乐播放器,MediaPlay)