2 erwa erwa erwa_erwa 于 2014.09.22 22:22 提问

做音乐播放器,用service做基本操作。急求大神解答。急!

package zuxia.android.service;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Button;
import android.widget.Toast;

public class MusicService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

public static final String PLAY_ACTION = "play_action";
public static final String PAUSE_ACTION = "pause_action";
public static final String LAST_ACTION="last_action";
public static final String NEXT_ACTION="next_action";

public MediaPlayer mediaPlayer;
private static final String SD_ROOT = Environment
        .getExternalStorageDirectory().getAbsolutePath();

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    mediaPlayer=new MediaPlayer();
    mediaPlayer.reset();
    try {

        mediaPlayer.setDataSource(SD_ROOT+"/3.mp3");

        mediaPlayer.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {

    String cmd=intent.getStringExtra("CMD");
    if(PLAY_ACTION.equals(cmd)){
        play();
    }else if(PAUSE_ACTION.equals(cmd)){
        pause();
    }  
    else if(LAST_ACTION.equals(cmd)){

        mediaPlayer.reset();
        try {

            mediaPlayer.setDataSource(SD_ROOT+"/1.mp3");
            mediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        play();
        Toast.makeText(this, "第二首", Toast.LENGTH_SHORT).show();
    }
    else if(NEXT_ACTION.equals(cmd)){

        mediaPlayer.reset();
        try {

            mediaPlayer.setDataSource(SD_ROOT+"/2.mp3");

            mediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        play();
        Toast.makeText(this, "第三首", Toast.LENGTH_SHORT).show();


    }
    super.onStart(intent, startId);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

private void play() {
    mediaPlayer.start();
}

private void pause() {
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!