- Kill notification when app is killed

This commit is contained in:
Stefan Schueller 2018-12-26 15:24:02 +01:00
parent 67690dd278
commit de52e1d9d5
2 changed files with 17 additions and 9 deletions

View File

@ -38,6 +38,7 @@ import net.schueller.peertube.model.VideoList;
import net.schueller.peertube.network.GetVideoDataService; import net.schueller.peertube.network.GetVideoDataService;
import net.schueller.peertube.network.RetrofitInstance; import net.schueller.peertube.network.RetrofitInstance;
import net.schueller.peertube.provider.SearchSuggestionsProvider; import net.schueller.peertube.provider.SearchSuggestionsProvider;
import net.schueller.peertube.service.VideoPlayerService;
import java.util.ArrayList; import java.util.ArrayList;
@ -151,6 +152,11 @@ public class VideoListActivity extends AppCompatActivity {
return true; return true;
} }
@Override
protected void onDestroy() {
super.onDestroy();
stopService(new Intent(this, VideoPlayerService.class));
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {

View File

@ -37,6 +37,8 @@ import static net.schueller.peertube.activity.VideoListActivity.EXTRA_VIDEOID;
public class VideoPlayerService extends Service { public class VideoPlayerService extends Service {
private static final String TAG = "VideoPlayerService";
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();
private static final String PLAYBACK_CHANNEL_ID = "playback_channel"; private static final String PLAYBACK_CHANNEL_ID = "playback_channel";
@ -64,12 +66,12 @@ public class VideoPlayerService extends Service {
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ACTION_PAUSE) { // this means that pause is available, hence the audio is playing if (playbackState == ACTION_PAUSE) { // this means that pause is available, hence the audio is playing
Log.v("VideoPlayerService", "ACTION_PLAY: " + playbackState); Log.v(TAG, "ACTION_PLAY: " + playbackState);
registerReceiver(myNoisyAudioStreamReceiver, becomeNoisyIntentFilter); registerReceiver(myNoisyAudioStreamReceiver, becomeNoisyIntentFilter);
} }
if (playbackState == ACTION_PLAY) { // this means that play is available, hence the audio is paused or stopped if (playbackState == ACTION_PLAY) { // this means that play is available, hence the audio is paused or stopped
Log.v("VideoPlayerService", "ACTION_PAUSE: " + playbackState); Log.v(TAG, "ACTION_PAUSE: " + playbackState);
unregisterReceiver(myNoisyAudioStreamReceiver); unregisterReceiver(myNoisyAudioStreamReceiver);
} }
} }
@ -89,7 +91,7 @@ public class VideoPlayerService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.v("VideoPlayerService", "onDestroy..."); Log.v(TAG, "onDestroy...");
playerNotificationManager.setPlayer(null); playerNotificationManager.setPlayer(null);
player.release(); player.release();
@ -106,7 +108,7 @@ public class VideoPlayerService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("VideoPlayerService", "onStartCommand..."); Log.v(TAG, "onStartCommand...");
playVideo(); playVideo();
return START_STICKY; return START_STICKY;
} }
@ -114,27 +116,27 @@ public class VideoPlayerService extends Service {
public void setCurrentVideo(Video video) public void setCurrentVideo(Video video)
{ {
Log.v("VideoPlayerService", "setCurrentVideo..."); Log.v(TAG, "setCurrentVideo...");
currentVideo = video; currentVideo = video;
} }
public void setCurrentStreamUrl(String streamUrl) public void setCurrentStreamUrl(String streamUrl)
{ {
Log.v("VideoPlayerService", "setCurrentStreamUrl..."); Log.v(TAG, "setCurrentStreamUrl...");
currentStreamUrl = streamUrl; currentStreamUrl = streamUrl;
} }
//Playback speed control //Playback speed control
public void setPlayBackSpeed(float speed) { public void setPlayBackSpeed(float speed) {
Log.v("VideoPlayerService", "setPlayBackSpeed..."); Log.v(TAG, "setPlayBackSpeed...");
player.setPlaybackParameters(new PlaybackParameters(speed)); player.setPlaybackParameters(new PlaybackParameters(speed));
} }
public void playVideo() { public void playVideo() {
Context context = this; Context context = this;
Log.v("VideoPlayerService", "playVideo..."); Log.v(TAG, "playVideo...");
// Produces DataSource instances through which media data is loaded. // Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),
@ -203,7 +205,7 @@ public class VideoPlayerService extends Service {
@Override @Override
public void onNotificationCancelled(int notificationId) { public void onNotificationCancelled(int notificationId) {
Log.v("VideoPlayerService", "onNotificationCancelled..."); Log.v(TAG, "onNotificationCancelled...");
// TODO: only kill the notification if we no longer have a bound activity // TODO: only kill the notification if we no longer have a bound activity
stopForeground(true); stopForeground(true);