How to prevent mediaplayer to stop when screen goes off?

微笑、不失礼 提交于 2019-12-03 16:08:31
acj

This line from Logcat is the important one:

Caused by: java.lang.InstantiationException: can't instantiate class com.floritfoto.apps.ave.Music; no empty constructor

Your service needs another constructor that takes no arguments:

public Music() {
    super("Music");
}

EDIT:

Using a service is the correct approach if you want to keep the music playing when the screen is off. However, the phone will try to sleep when the screen is off, and this can interrupt your MediaPlayer.

The most reliable solution is to use a partial WakeLock to prevent the device from sleeping while you're playing music. Be sure to release the WakeLock properly when you're not actively playing music; otherwise the battery will drain.

You may also want to use startForeground(), which will reduce the risk of your service being killed when there is memory pressure. It will also create a nice user experience by showing a persistent notification when your service is running.

Instantiating the Music class with Music track = Music(fileDescriptor); is probably doing some harm. A better approach is to pass the file descriptor as an Extra in the Intent that you pass to startService():

Intent serviceIntent = new Intent(this, Music.class);
serviceIntent.putExtra("ServiceFileDescriptor", fileDescriptor);
startService(serviceIntent);

Then, retrieve the file descriptor from that same Intent when it's passed to your service's onStartCommand() method:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStart();

    Bundle bundle = intent.getExtras();
    // NOTE: The next line will vary depending on the data type for the file
    // descriptor. I'm assuming that it's an int.
    int fileDescriptor = bundle.getIntExtra("ServiceFileDescriptor");
    mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(fileDescriptor);
        ...
    ...
    return START_STICKY;
}

A few of things to note here. I've moved the code from your original constructor (which should be removed) into onStartCommand(). You can remove the onStart() method as well, since it will only be called on pre-2.0 devices. If you want to support modern Android versions, you'll need to use onStartCommand() instead. Finally, the START_STICKY return value will ensure that the service stays running until you call stopService() from your activity.

EDIT 2:

Using a service enables your users to move between activities without interrupting the MediaPlayer. You don't have much control over how long an Activity will stay in memory, but an active Service (especially if you call startForeground()) won't be killed unless there is very strong memory pressure.

To interact with the MediaPlayer after the service is started, you have a couple of options. You can pass additional commands to the service by creating Intents and using the action string (and/or some extras) to tell the service what you would like it to do. Just call startActivity() again with the new Intent, and onStartCommand() will be called in the service, at which point you can manipulate the MediaPlayer. The second option is to use a bound service (example here) and to bind/unbind each time you enter/leave an activity that needs to communicate with the service. Using a bound service "feels" as though you're directly manipulating the service, but it's also more complex since you need to manage binding and unbinding.

as an option you can keep the screen activated to maintain the MediaPlayer reproducing the media:

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!