如何在应用程序被用户杀死的机器人如saavan音乐应用程序中播放媒体播放器

时间:2015-12-31 07:49:12

标签: android android-service android-music-player

我实现了在app内完美运行的媒体播放器。但是当app杀死时我想在后台播放媒体播放器,我该怎么做。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您可以使用Media Playback

或使用:Service

 public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final String ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;

    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent.getAction().equals(ACTION_PLAY)) {
            mMediaPlayer =... // initialize it here
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.prepareAsync(); // prepare async to not block main thread
        }
    }

    /**
     * Called when MediaPlayer is ready
     */
    public void onPrepared(MediaPlayer player) {
        player.start();
    }
}

答案 1 :(得分:2)

最后我解决了使用服务的问题感谢hlep

公共类ForegroundService扩展Service {     private static final String LOG_TAG =“ForegroundService”;

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Start Foreground Intent ");
        Intent notificationIntent = new Intent(this, MusicActivity.class);
        notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        Intent previousIntent = new Intent(this, ForegroundService.class);
        previousIntent.setAction(Constants.ACTION.PREV_ACTION);
        PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
                previousIntent, 0);

        Intent playIntent = new Intent(this, ForegroundService.class);
        playIntent.setAction(Constants.ACTION.PLAY_ACTION);
        PendingIntent pplayIntent = PendingIntent.getService(this, 0,
                playIntent, 0);

        Intent nextIntent = new Intent(this, ForegroundService.class);
        nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
        PendingIntent pnextIntent = PendingIntent.getService(this, 0,
                nextIntent, 0);

        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.mipmap.ic_launcher);

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("My Music Player")
                .setTicker("MY Music Player")
                .setContentText("My Music")
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(
                        Bitmap.createScaledBitmap(icon, 128, 128, false))
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .addAction(android.R.drawable.ic_media_previous,
                        "Previous", ppreviousIntent)
                .addAction(android.R.drawable.ic_media_play, "Play",
                        pplayIntent)
                .addAction(android.R.drawable.ic_media_next, "Next",
                        pnextIntent).build();
        startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                notification);
    } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
        Log.i(LOG_TAG, "Clicked Previous");
    } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
        Log.i(LOG_TAG, "Clicked Play");
    } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
        Log.i(LOG_TAG, "Clicked Next");
    } else if (intent.getAction().equals(
            Constants.ACTION.STOPFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Stop Foreground Intent");
        stopForeground(true);
        stopSelf();
    }
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(LOG_TAG, "In onDestroy");
}

@Override
public IBinder onBind(Intent intent) {
    // Used only in case of bound services.
    return null;
}

}