1

i'm trying to create an app that play music (Radio Stream) in background (Without Service) and also allow user to set Gif as live wallpaper (using WallpaperService).

ISSUES :

  1. My problem sometimes the streaming radio take time to play and when i set gif as wallpaper and radio still doesn't play ,When i close my app from (Recent Tasks/by swipe) . music plays although i close my app (reason is WallpaperService).

WHAT I WANT

  1. When user closes the app from (recent tasks /by swipe) I want to stop music even though live wallpaper is working.

MainActivity :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MusicButton = findViewById(R.id.toggleButton);
        mediaPlayer = new MediaPlayer();
        new PlayerTask().execute("URL");

@SuppressLint("StaticFieldLeak")
    public class PlayerTask extends AsyncTask<String, Void, Boolean> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
            mediaPlayer.setAudioAttributes(attribs);

        } else {
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
    }

    @Override
    protected Boolean doInBackground(String... strings) {
        try {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.prepare();
            prepared = true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();

            }
        });
        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        MusicButton.setVisibility(View.VISIBLE);
        MusicButton.setChecked(true);

}


@Override
protected void onDestroy() {
    super.onDestroy();
    MApplication.sBus.post(PlaybackEvent.PLAY);
    try {
        MApplication.sBus.unregister(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 @Subscribe
public void handlePlaybackEvent(PlaybackEvent event) {
    switch (event) {
        case PLAY:
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                MusicButton.setChecked(true);
            }
            break;
        case PAUSE:
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
                MusicButton.setChecked(false);
            }
            break;
    }
}

KillNotificationService Service for cancel Notification if user close app

public class KillNotificationService extends Service {

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert nMgr != null;
        nMgr.cancelAll();
        MApplication.sBus.post(PlaybackEvent.PLAY);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Please help me i have spent 3 days to solve this problem without any result

(sorry for my bad english)

ali
  • 189
  • 3
  • 14

1 Answers1

0

understand the activity life cycle here, you will see that an activity have a onPause()/onStop() and many such states.

If you want to stop your music when the app is temporarily closed (worked for me): create a onPause() under your main class and pause/stop your music there.

 @Override
protected void onPause() {
    mediaPlayer.stop();
    super.onPause();
}

same goes for onStop(). Tip: make sure your media player is not null before starting :) Thanks

Nishant Joshi
  • 61
  • 1
  • 6