如何在android的后台创建播放Media Player的服务类?

时间:2012-09-11 08:55:14

标签: android

我想在后台播放Media Player。还想从android的主要活动中播放,停止,暂停,下一个进程? 有谁有想法?

1 个答案:

答案 0 :(得分:0)

public class MusicService extends Service {

    private static final String TAG = "MyService";
    public static MediaPlayer mediaplayer;
    String musicfile;

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(" ibinder ", "");
        return null;
    }

    @Override
    public void onCreate() {
        // Toast.makeText(this, "My Service Created", 0).show();
        Log.i(TAG, "onCreate");

        mediaplayer = new MediaPlayer();
        mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", 0).show();
        Log.w(TAG, "onDestroy");
        mediaplayer.stop();
    }

    @Override
    public void onStart(Intent intent, int startid) {
         Toast.makeText(this,
         "My Service Started :" + intent + " start id :" + startid, 0)
         .show();

         mediaplayer.start();

    }


}