2015-11-24 00:40:36 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
2015-11-25 17:18:01 +00:00
|
|
|
import android.app.NotificationManager;
|
2015-11-29 17:05:44 +00:00
|
|
|
import android.app.PendingIntent;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.app.Service;
|
2015-12-17 17:49:12 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.content.Context;
|
2015-11-24 00:40:36 +00:00
|
|
|
import android.content.Intent;
|
2015-12-17 17:49:12 +00:00
|
|
|
import android.content.IntentFilter;
|
2015-11-24 00:40:36 +00:00
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.media.MediaPlayer;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
import android.os.IBinder;
|
2015-11-24 00:40:36 +00:00
|
|
|
import android.os.PowerManager;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.support.v7.app.NotificationCompat;
|
2015-11-25 17:18:01 +00:00
|
|
|
import android.util.Log;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.widget.Toast;
|
2015-11-24 00:40:36 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Adam Howard on 08/11/15.
|
|
|
|
* Copyright (c) Adam Howard <achdisposable1@gmail.com> 2015
|
|
|
|
*
|
|
|
|
* BackgroundPlayer.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-12-17 17:49:12 +00:00
|
|
|
/**Plays the audio stream of videos in the background.*/
|
2015-11-25 15:19:50 +00:00
|
|
|
public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPreparedListener*/ {
|
2015-11-24 00:40:36 +00:00
|
|
|
|
|
|
|
private static final String TAG = BackgroundPlayer.class.toString();
|
2015-12-17 17:49:12 +00:00
|
|
|
private static final String ACTION_STOP = TAG+".STOP";
|
|
|
|
private static final String ACTION_PLAYPAUSE = TAG+".PLAYPAUSE";
|
2015-11-25 17:18:01 +00:00
|
|
|
//private Looper mServiceLooper;
|
|
|
|
//private ServiceHandler mServiceHandler;
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-11-24 00:40:36 +00:00
|
|
|
public BackgroundPlayer() {
|
2015-11-25 15:19:50 +00:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2015-12-17 17:49:12 +00:00
|
|
|
/*PendingIntent pi = PendingIntent.getActivity(this, 0,
|
|
|
|
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);*/
|
2015-11-25 15:19:50 +00:00
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
Toast.makeText(this, "Playing in background", Toast.LENGTH_SHORT).show();//todo:translation string
|
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
String source = intent.getDataString();
|
2015-12-17 17:49:12 +00:00
|
|
|
//Log.i(TAG, "backgroundPLayer source:"+source);
|
2015-11-25 17:18:01 +00:00
|
|
|
String videoTitle = intent.getStringExtra("title");
|
|
|
|
|
2015-12-17 17:49:12 +00:00
|
|
|
//do nearly everything in a separate thread
|
2015-11-25 17:18:01 +00:00
|
|
|
PlayerThread player = new PlayerThread(source, videoTitle, this);
|
|
|
|
player.start();
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
// If we get killed after returning here, don't restart
|
2015-11-25 15:19:50 +00:00
|
|
|
return START_NOT_STICKY;
|
2015-11-24 00:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-11-25 15:19:50 +00:00
|
|
|
public IBinder onBind(Intent intent) {
|
2015-11-25 17:18:01 +00:00
|
|
|
// We don't provide binding (yet?), so return null
|
2015-11-25 15:19:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
//Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
private class PlayerThread extends Thread {
|
2015-12-17 17:49:12 +00:00
|
|
|
MediaPlayer mediaPlayer;
|
2015-11-25 17:18:01 +00:00
|
|
|
private String source;
|
|
|
|
private String title;
|
2015-12-17 17:49:12 +00:00
|
|
|
private int noteID = TAG.hashCode();
|
2015-11-25 17:18:01 +00:00
|
|
|
private BackgroundPlayer owner;
|
2015-12-17 17:49:12 +00:00
|
|
|
private NotificationManager noteMgr;
|
|
|
|
private NotificationCompat.Builder noteBuilder;
|
|
|
|
private WifiManager.WifiLock wifiLock;
|
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
public PlayerThread(String src, String title, BackgroundPlayer owner) {
|
|
|
|
this.source = src;
|
|
|
|
this.title = title;
|
|
|
|
this.owner = owner;
|
|
|
|
mediaPlayer = new MediaPlayer();
|
|
|
|
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
2015-11-25 15:19:50 +00:00
|
|
|
}
|
2015-11-25 17:18:01 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock
|
|
|
|
try {
|
|
|
|
mediaPlayer.setDataSource(source);
|
|
|
|
mediaPlayer.prepare(); //We are already in a separate worker thread,
|
|
|
|
//so calling the blocking prepare() method should be ok
|
|
|
|
|
2015-11-25 17:36:01 +00:00
|
|
|
//alternatively:
|
2015-11-25 17:18:01 +00:00
|
|
|
//mediaPlayer.setOnPreparedListener(this);
|
|
|
|
//mediaPlayer.prepareAsync(); //prepare async to not block main thread
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
ioe.printStackTrace();
|
|
|
|
Log.e(TAG, "video source:" + source);
|
|
|
|
Log.e(TAG, "video title:" + title);
|
|
|
|
//can't do anything useful without a file to play; exit early
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiManager wifiMgr = ((WifiManager)getSystemService(Context.WIFI_SERVICE));
|
2015-12-17 17:49:12 +00:00
|
|
|
wifiLock = wifiMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
|
2015-11-25 17:18:01 +00:00
|
|
|
|
|
|
|
mediaPlayer.setOnCompletionListener(new EndListener(wifiLock));//listen for end of video
|
|
|
|
|
|
|
|
//get audio focus
|
|
|
|
/*
|
|
|
|
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
|
|
|
|
AudioManager.AUDIOFOCUS_GAIN);
|
|
|
|
|
|
|
|
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
|
|
|
// could not get audio focus.
|
|
|
|
}*/
|
|
|
|
wifiLock.acquire();
|
|
|
|
mediaPlayer.start();
|
2015-12-17 17:49:12 +00:00
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
//mediaPlayer.getCurrentPosition()
|
|
|
|
int vidLength = mediaPlayer.getDuration();
|
2015-12-17 17:49:12 +00:00
|
|
|
|
|
|
|
//Intent genericIntent = new Intent(owner, owner.getClass());
|
|
|
|
|
|
|
|
//PendingIntent playPI = PendingIntent.getService(owner, noteID, genericIntent, 0);
|
|
|
|
PendingIntent playPI = PendingIntent.getBroadcast(owner, noteID, new Intent(ACTION_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
NotificationCompat.Action.Builder buttonBuilder =
|
|
|
|
new NotificationCompat.Action.Builder(R.drawable.ic_play_arrow_black,
|
|
|
|
"Play", playPI);//todo:translatable string
|
|
|
|
NotificationCompat.Action playButton = buttonBuilder.build();
|
|
|
|
|
|
|
|
PendingIntent stopPI = PendingIntent.getBroadcast(owner, noteID,
|
|
|
|
new Intent(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.setPriority(Integer.MAX_VALUE);
|
|
|
|
filter.addAction(ACTION_PLAYPAUSE);
|
|
|
|
filter.addAction(ACTION_STOP);
|
|
|
|
registerReceiver(broadcastReceiver, filter);
|
|
|
|
|
|
|
|
//playPauseButton
|
|
|
|
//todo: make it so that tapping the notification brings you back to the Video's DetailActivity
|
|
|
|
//using setContentIntent
|
|
|
|
noteBuilder = new NotificationCompat.Builder(owner);
|
2015-11-25 17:18:01 +00:00
|
|
|
noteBuilder
|
|
|
|
.setPriority(Notification.PRIORITY_LOW)
|
|
|
|
.setCategory(Notification.CATEGORY_TRANSPORT)
|
|
|
|
.setContentTitle(title)
|
|
|
|
.setContentText("NewPipe is playing in the background")//todo: translation string
|
2015-12-17 17:49:12 +00:00
|
|
|
//.setAutoCancel(!mediaPlayer.isPlaying())
|
2015-11-25 17:18:01 +00:00
|
|
|
.setOngoing(true)
|
2015-12-17 17:49:12 +00:00
|
|
|
.setDeleteIntent(stopPI)
|
|
|
|
//.setProgress(vidLength, 0, false) //doesn't fit with Notification.MediaStyle
|
|
|
|
.setSmallIcon(R.mipmap.ic_launcher)
|
|
|
|
.setTicker(title + " - NewPipe")
|
|
|
|
.addAction(playButton);
|
|
|
|
/* .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
|
|
.setLargeIcon(cover)*/
|
|
|
|
|
|
|
|
noteBuilder.setStyle(new NotificationCompat.MediaStyle()
|
|
|
|
//.setMediaSession(mMediaSession.getSessionToken())
|
|
|
|
.setShowActionsInCompactView(new int[] {0})
|
|
|
|
.setShowCancelButton(true)
|
|
|
|
.setCancelButtonIntent(stopPI)
|
|
|
|
);
|
2015-11-25 17:18:01 +00:00
|
|
|
|
|
|
|
startForeground(noteID, noteBuilder.build());
|
2015-12-17 17:49:12 +00:00
|
|
|
|
|
|
|
//currently decommissioned progressbar looping update code - works, but doesn't fit inside
|
|
|
|
//Notification.MediaStyle Notification layout.
|
|
|
|
noteMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
|
|
|
/*
|
|
|
|
//update every 2s or 4 times in the video, whichever is shorter
|
|
|
|
int sleepTime = Math.min(2000, (int)((double)vidLength/4));
|
2015-11-25 17:18:01 +00:00
|
|
|
while(mediaPlayer.isPlaying()) {
|
2015-12-17 17:49:12 +00:00
|
|
|
noteBuilder.setProgress(vidLength, mediaPlayer.getCurrentPosition(), false);
|
|
|
|
noteMgr.notify(noteID, noteBuilder.build());
|
2015-11-25 17:18:01 +00:00
|
|
|
try {
|
|
|
|
Thread.sleep(sleepTime);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.d(TAG, "sleep failure");
|
|
|
|
}
|
2015-12-17 17:49:12 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
Log.i(TAG, "received broadcast action:"+action);
|
|
|
|
if(action.equals(ACTION_PLAYPAUSE)) {
|
|
|
|
if(mediaPlayer.isPlaying()) {
|
|
|
|
mediaPlayer.pause();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mediaPlayer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(action.equals(ACTION_STOP)) {
|
|
|
|
mediaPlayer.stop();
|
|
|
|
afterPlayCleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void afterPlayCleanup() {
|
|
|
|
//noteBuilder.setProgress(0, 0, false);//remove progress bar
|
|
|
|
noteMgr.cancel(noteID);//remove notification
|
|
|
|
unregisterReceiver(broadcastReceiver);
|
|
|
|
mediaPlayer.release();//release system resources
|
|
|
|
|
|
|
|
|
|
|
|
wifiLock.release();//release wifilock
|
|
|
|
stopForeground(true);//remove foreground status of service; make us killable
|
|
|
|
|
|
|
|
stopSelf();
|
|
|
|
//todo:release cpu lock
|
|
|
|
}
|
|
|
|
|
|
|
|
private class EndListener implements MediaPlayer.OnCompletionListener {
|
|
|
|
private WifiManager.WifiLock wl;
|
|
|
|
public EndListener(WifiManager.WifiLock wifiLock) {
|
|
|
|
this.wl = wifiLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCompletion(MediaPlayer mp) {
|
|
|
|
afterPlayCleanup();
|
2015-11-25 17:18:01 +00:00
|
|
|
}
|
2015-11-25 15:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-24 00:40:36 +00:00
|
|
|
/*
|
2015-11-25 15:19:50 +00:00
|
|
|
private class ListenerThread extends Thread implements AudioManager.OnAudioFocusChangeListener {
|
2015-11-25 17:18:01 +00:00
|
|
|
@Override
|
|
|
|
public void onAudioFocusChange(int focusChange) {
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
}
|
2015-11-25 15:19:50 +00:00
|
|
|
}*/
|
2015-11-24 00:40:36 +00:00
|
|
|
}
|