android音乐播放器

package cn.yinuo.service;

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.view.View;
import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {
    private String filenameText;
    private File audioFile;
    private MediaPlayer mediaPlayer;
    private int position;
    musicActivity music;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
       
        mediaPlayer = new MediaPlayer();
        ButtonClickListen listener = new ButtonClickListen();
    
        filenameText =music.musicname;
       
        Button playbutton = (Button) this.findViewById(R.id.start);
        Button pausebutton = (Button) this.findViewById(R.id.pause);
        Button resetbutton = (Button) this.findViewById(R.id.next);
        Button stopbutton = (Button) this.findViewById(R.id.stop);
        playbutton.setOnClickListener(listener);
        pausebutton.setOnClickListener(listener);
        resetbutton.setOnClickListener(listener);
        stopbutton.setOnClickListener(listener);
    }
   
    @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    position = savedInstanceState.getInt("position");
    String path = savedInstanceState.getString("path");
    if(path!=null){
    audioFile = new File(path);
    }
super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt("position", position);
    if(audioFile!=null){
    outState.putString("path", audioFile.getAbsolutePath());
    }
super.onSaveInstanceState(outState);
}

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

@Override
protected void onResume() {
if(position > 0 && audioFile != null){
try {
play();
mediaPlayer.seekTo(position);
} catch (Exception e) {
e.printStackTrace();
}
}
super.onResume();
}

@Override
protected void onPause() {
if(mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
super.onPause();
}

private class ButtonClickListen implements View.OnClickListener{
private boolean pause;

@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.start:
audioFile = new File(Environment.getExternalStorageDirectory(), filenameText);
if(!audioFile.exists()){
Toast.makeText(MainActivity.this, "文件不存", 1).show();
return;
}
play();
break;

case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
pause = true;
((Button)v).setText(R.string.hello);
}else{
if(pause){
mediaPlayer.start();//继续播放音乐
pause = false;
((Button)v).setText(R.string.hello);
}
}
break;

case R.id.next:
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}else{
audioFile = new File(Environment.getExternalStorageDirectory(), filenameText);
if(!audioFile.exists()){
Toast.makeText(MainActivity.this, "文件不存", 1).show();
return;
}
play();
}
break;

case R.id.stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}  
    }

private void play() throws IOException {
mediaPlayer.reset();
mediaPlayer.setDataSource(audioFile.getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.start();
}
}

你可能感兴趣的:(android,OS,音乐)