|
package com.yfit.musicplayer;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private PlayOnClickListener playListener;
private EditText audioFileText;
private String fileName;
private MediaPlayer player;
private int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playListener = new PlayOnClickListener();
player = new MediaPlayer();
audioFileText = (EditText)this.findViewById(R.id.audiofile);
Button play = (Button)this.findViewById(R.id.play);
Button pause = (Button)this.findViewById(R.id.pause);
Button reset = (Button)this.findViewById(R.id.reset);
Button stop = (Button)this.findViewById(R.id.stop);
play.setOnClickListener(playListener);
pause.setOnClickListener(playListener);
reset.setOnClickListener(playListener);
stop.setOnClickListener(playListener);
}
private final class PlayOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
fileName = audioFileText.getText().toString();
Button button = (Button) v;
try {
switch(v.getId()){//通过button传递过来的id获取
case R.id.play:
play();
break;
case R.id.pause:
if(player.isPlaying()){
player.pause();
button.setText(R.string.continue1);
}else{
player.start();
button.setText(R.string.pause);
}
break;
case R.id.reset:
if(player.isPlaying()){
player.seekTo(0);
}else{
play();
}
break;
case R.id.stop:
if(player.isPlaying()){
player.stop();
}
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
this.fileName = savedInstanceState.getString("savedInstanceState");
this.position = savedInstanceState.getInt("position");
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("position", position);
outState.putString("fileName", fileName);
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
}
@Override
protected void onPause() {//电话进来时,该Activity处于暂停状态,在没有被杀死的情况下,并保存播放的位置
if(player.isPlaying()){
position = player.getCurrentPosition();//播放的当前位置
player.stop();//停止播放
}
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onResume() {//当电话结束时,该Activity重新被加载,
if(position>0 && player!=null){
try {
play();
player.seekTo(position);
position = 0;
} catch (Exception e) {
e.printStackTrace();
}
}
Log.i(TAG, "onResume");
super.onResume();
}
@Override
protected void onDestroy() {
player.release();
Log.i(TAG, "onDestroy");
super.onDestroy();
}
private void play() throws IOException {
File file = new File(Environment.getExternalStorageDirectory(), fileName);
player.reset();//重置初始状态
player.setDataSource(file.getAbsolutePath());
player.prepare();
player.start();//开始播放
}
} |
|