音乐播放器

效果图

播放器实现了,当电话来是保存当前的播放点,以及当内存不足的时候播放器实例销毁的时候保存播放点以及播放的文件,当重新创建实例的时候获得播放的文件以及播放点。

音乐播放api介绍

MediaPlayer mediaPlayer = new MediaPlayer();

if (mediaPlayer.isPlaying()) {

   mediaPlayer.reset();//重置为初始状态

}

mediaPlayer.setDataSource("/sdcard/god.mp3");

mediaPlayer.prepare();//缓冲 

mediaPlayer.start();//开始或恢复播放

mediaPlayer.pause();//暂停播放

mediaPlayer.start();//恢复播放

mediaPlayer.stop();//停止播放

mediaPlayer.release();//释放资源

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {//播出完毕事件

        @Override public void onCompletion(MediaPlayer arg0) {

      mediaPlayer.release();

        }

});

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {// 错误处理事件

         @Override public boolean onError(MediaPlayer player, int arg1, int arg2) {

  mediaPlayer.release();

  return false;

         }

});

实现代码:

PlayActivity
package com.tjp.auto;

import java.io.File;

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

public class PlayActivity extends Activity {

private EditText filenamEditText;
private MediaPlayer mediaPlayer;
private String fileName;
private final static String TAG="PlayActivity";
private int position=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonOnClikListiner buttonOnClikListinero
=new ButtonOnClikListiner();
filenamEditText
=(EditText) this.findViewById(R.id.filename);
mediaPlayer
=new MediaPlayer();
Button start
=(Button) this.findViewById(R.id.start);
Button pause
=(Button) this.findViewById(R.id.pause);
Button stop
=(Button) this.findViewById(R.id.stop);
Button replay
=(Button) this.findViewById(R.id.replay);
start.setOnClickListener(buttonOnClikListinero);
pause.setOnClickListener(buttonOnClikListinero);
stop.setOnClickListener(buttonOnClikListinero);
replay.setOnClickListener(buttonOnClikListinero);
}

@Override
protected void onDestroy() {
mediaPlayer.release();
//释放
super.onDestroy();
}

@Override
protected void onPause() {//如果忽然电话到来停止播放
if(mediaPlayer.isPlaying()){
position
=mediaPlayer.getCurrentPosition();//保存当前播放点
mediaPlayer.stop();
}
super.onPause();
}

@Override
protected void onResume() {//如果电话结束继续播放音乐
if(position>0&&fileName!=null){
play();
mediaPlayer.seekTo(position);
position
=0;
}
super.onResume();
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {//当实例重新打开的时候得到文件名和播放的位置
this.fileName=savedInstanceState.getString("fileName");
this.position=savedInstanceState.getInt("position");
Log.i(TAG,
"onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {//当内存不足的时候实例销毁保存文件名和播放的位置
outState.putString("fileName", fileName);
outState.putInt(
"position", position);
Log.i(TAG,
"onSaveInstanceState");
super.onSaveInstanceState(outState);
}

private final class ButtonOnClikListiner implements View.OnClickListener{

@Override
public void onClick(View v) {
fileName
=filenamEditText.getText().toString();
Button button
=(Button)v;
try {
switch (v.getId()) {
case R.id.start:
play();
break;
case R.id.stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
case R.id.replay:
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(
0);//跳转到开始
}else{
play();
}
break;
case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
//暂停
button.setText(R.string.resuese);
}
else{
mediaPlayer.start();
//继续播放
button.setText(R.string.pause);
}
break;

default:
break;
}
}
catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}

protected void play(){
try {
File autoFile
=new File(Environment.getExternalStorageDirectory(),fileName);//从sdk里面读取
mediaPlayer.reset();//清除设置
mediaPlayer.setDataSource(autoFile.getAbsolutePath());
mediaPlayer.prepare();
//缓冲
mediaPlayer.start();//播放
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/filename"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:id
="@+id/filename"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/start"
android:id
="@+id/start"
/>
<Button
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/pause"
android:id
="@+id/pause"
/>
<Button
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/stop"
android:id
="@+id/stop"
/>
<Button
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/replay"
android:id
="@+id/replay"
/>
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="com.tjp.auto"
android:versionCode
="1"
android:versionName
="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PlayActivity"
android:label
="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PlayActivity!</string>
<string name="app_name">音乐播放器</string>
<string name="filename">音乐文件名</string>
<string name="start">开始</string>
<string name="stop">停止</string>
<string name="pause">暂停</string>
<string name="replay">重播</string>
<string name="resuese">继续</string>
</resources>
posted @ 2011-06-02 00:29  飞鹰小谭  阅读(596)  评论(0编辑  收藏  举报