I'm working on an application that which is media player.
Question: How do I make media player (application) work without issues from turning the screen off? Question: loadInBackground() returns the uri but onLoadFinished not called when screen off.
Few words to explain trouble better:
The media player contains Loader
which loads the song and another Loader
which loads related suggestions. I've also implemented the method to play_next()
which relies on a listener of media player on finished (button in right upper corner).
The media player is initialized in the service class which I've made so the user can search new songs, and prepare the next_song()
with the button (and the playing continues because I connect to service each time Activity
is loaded and I return from service media player so I can attach listener for onFinish
method).
The thing that bothers me is that when the user turns off the screen, the activity goes to idle state (status from android monitor - log cat) and once in idle state (aka turned off screen ) if the song ends, it will start new intent which is media player to start initializing and auto-playing song. It works when the screen is on but it doesn't if it goes to idle state.
If I turn on the screen I get activity to act like this:
Little pink dot is a progress bar. So the activity tries to refresh itself? In the onCreate()
method I call start_loader
which initializes and does the things with Loader
.
I've seen some power manager tools and seen the bad comments about it which imply to the battery usage but I did try it and from log cat it just went to idle state again (if it matters).
onPause()
Activity and onResume()
? Also i get message from loadInBackground() which is uri from song and from there on it freezes doesn't continue.
you need to create Service
for that that run on Background.so when you play song don'tstop if you keep you screen off. Service in Android
upper link perfectly describe service.
example of service ...
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;
import com.example.divyesh.musiclite.Pojos.SongsList;
import java.io.File;
import java.io.IOException;
/**
* Created by Divyesh on 11/18/2017.
*/
public class MediaSongServiece extends Service {
SongsList s;
private static Boolean destroy = false;
private String TAG = "HELLO";
private MusicIntentReceiver reciever;
private SharedPreferences prefrence;
private static MediaPlayer player;
private int thisStartId = 1;
private String ss[];
SharedPreferences.Editor editor;
public IBinder onBind(Intent arg0) {
return null;
}
public static void requestPlayMedia() {
player.start();
}
public void requestPauseMedia() {
player.pause();
}
@Override
public void onCreate() {
super.onCreate();
reciever = new MusicIntentReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(reciever, filter);
Log.d("service", "onCreate");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
stopSelf();
}
@Override
public boolean onUnbind(Intent intent) {
stopSelf(thisStartId);
return super.onUnbind(intent);
}
public void onStart(Intent intent, int startId) {
if (intent.equals(null)) {
stopSelf();
}
if (destroy == false) {
thisStartId = startId;
ss = intent.getExtras().getStringArray("getArray");
Log.e(TAG, "onStart: " + ss[0] + ss[1] + " path" + ss[5]);
s = new SongsList();
s.setAll(ss);
if (player != null) {
player.stop();
player.reset();
try {
player.setDataSource(getApplicationContext(), Uri.fromFile(new File(s.getPath())));
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.setLooping(true);
player.setVolume(100, 100);
player.start();
Log.e(TAG, "onStart: m= is not null" + player.isPlaying());
} else {
player = MediaPlayer.create(getApplicationContext(), Uri.fromFile(new File(s.getPath())));
player.setLooping(true);
player.setVolume(100, 100);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Log.e(TAG, "onStart: m= is null WE created new player");
}
} else {
Log.e(TAG, "onelse destroy ");
recover();
}
}
private void recover() {
destroy = false;
prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
for (int i = 0; i <= 5; i++) {
ss[i] = prefrence.getString("" + i, "");
}
String currentPose = prefrence.getString("current_pos", "");
Log.e(TAG, "recover: Shared Daata is" + ss[5] + "_______" + currentPose);
}
@Override
public void onDestroy() {
unregisterReceiver(reciever);
player.stop();
player.release();
stopSelf(thisStartId);
}
@Override
public void onLowMemory() {
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
editor = prefrence.edit();
destroy = true;
}
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
editor = prefrence.edit();
destroy = true;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void saveData() {
player.pause();
for (int i = 0; i < ss.length; i++) {
editor.putString("" + i, ss[i]);
}
editor.putString("current_pos", "" + player.getCurrentPosition());
editor.commit();
}
public class MusicIntentReceiver extends BroadcastReceiver {
public String TAG = "ss";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(TAG, "Headset is unplugged");
Toast.makeText(context, " Headset is unpluged ", Toast.LENGTH_SHORT).show();
Log.e(TAG, "onReceive: " + " is play song " + player.isPlaying());
break;
case 1:
Log.d(TAG, "Headset is plugged");
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.