Android应用开发之音乐播放器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lihongjian944043440/article/details/7306927

资源

<string name="app_name">MusicPlayer</string>
    <string name="play_text">播放</string>
    <string name="pause_text">暂停</string>
    <string name="continue_text">继续</string>
    <string name="reset_text">重播</string>
    <string name="stop_text">停止</string>
    <string name="music_text">音乐</string>
    <string name="nofile">文件不存在</string>
    <string name="SDCarderror">SDCard不存在</string>

布局

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

    <EditText
        android:id="@+id/musicTv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="I Knew I Love You.mp3"/>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*" >

        <TableRow >

            <Button
                android:id="@+id/playBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/play_text" />
            <Button
                android:id="@+id/pauseBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/pause_text" />
            <Button
                android:id="@+id/resetBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/reset_text" />
            <Button
                android:id="@+id/stopBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/stop_text" />
        </TableRow>
    </TableLayout>

</LinearLayout>

Activity

package cn.class3g.musicplay;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
import android.view.View;

public class MusicPlayerActivity extends Activity implements
		View.OnClickListener {

	Button playBtn, pauseBtn, resetBtn, stopBtn;
	EditText musicTv;

	String musicName = "";
	MediaPlayer player = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		player = new MediaPlayer();

		findViews();
	}

	private void findViews() {
		musicTv = (EditText) findViewById(R.id.musicTv);
		playBtn = (Button) findViewById(R.id.playBtn);
		pauseBtn = (Button) findViewById(R.id.pauseBtn);
		resetBtn = (Button) findViewById(R.id.resetBtn);
		stopBtn = (Button) findViewById(R.id.stopBtn);

		playBtn.setOnClickListener(this);
		pauseBtn.setOnClickListener(this);
		resetBtn.setOnClickListener(this);
		stopBtn.setOnClickListener(this);
	}

	public void onClick(View v) {
		musicName = musicTv.getText().toString();

		// 判断有没有sd卡
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			File file = new File(Environment.getExternalStorageDirectory(),
					musicName);
			// 判断有没有要播放的文件
			if (file.exists()) {
				try {
					switch (v.getId()) {
					case R.id.playBtn:
						playMusic(file);
						break;
					case R.id.pauseBtn:
						if (player == null)
							break;
						if (player.isPlaying()) {
							player.pause();
							pauseBtn.setText(R.string.continue_text);
						} else {
							player.start();
							pauseBtn.setText(R.string.pause_text);
						}
						break;
					case R.id.resetBtn:
						if(player.isPlaying()){
							player.seekTo(0);							
						}else{
							playMusic(file);
						}
						break;
					case R.id.stopBtn:
						if(player.isPlaying()){
							player.stop();
						}
						break;
					}
				} catch (Exception e) {
					Log.e("TAG", e.toString());
				}
			} else {
				Toast.makeText(this, R.string.nofile, 1).show();
			}
		} else {
			Toast.makeText(this, R.string.SDCarderror, 1).show();
		}

	}

	private void playMusic(File file) throws IllegalArgumentException, IllegalStateException, IOException {
		if (player != null) {
			player.reset();
			player.setDataSource(file.getAbsolutePath());
			player.prepare();
			player.start();
		}
	}

}

来电处理

protected void onDestroy() {
		if(player != null){
			if(player.isPlaying()){
				player.stop();
			}
			player.release();
		}
		super.onDestroy();
	}

	protected void onPause() {
		if(player != null){
			if(player.isPlaying()){
				player.pause();
			}
		}
		super.onPause();
	}

	protected void onResume() {
		if(player != null){
			if(!player.isPlaying()){
				player.start();
			}
		}
		super.onResume();
	}

注意:

int position = player.getCurrentPosition()

player.seekTo(position);

 

测试:

运行进行播放、暂停、继续、重置、停止等功能,

播放中来电的测试



阅读更多
换一批

没有更多推荐了,返回首页