mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 00:20:32 +00:00
reimplemented BackgroundPlayer extending Service, not IntentService. See http://stackoverflow.com/questions/17237746 and http://stackoverflow.com/questions/8690198
This commit is contained in:
parent
943027ffdd
commit
0f93a45b9d
@ -3,6 +3,7 @@
|
|||||||
package="org.schabi.newpipe" >
|
package="org.schabi.newpipe" >
|
||||||
|
|
||||||
<uses-permission android:name= "android.permission.INTERNET" />
|
<uses-permission android:name= "android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name= "android.permission.WAKE_LOCK" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||||
<application
|
<application
|
||||||
|
@ -298,38 +298,39 @@ public class ActionBarHandler {
|
|||||||
intent.putExtra(Intent.EXTRA_TITLE, videoTitle);
|
intent.putExtra(Intent.EXTRA_TITLE, videoTitle);
|
||||||
intent.putExtra("title", videoTitle);
|
intent.putExtra("title", videoTitle);
|
||||||
activity.startService(intent);
|
activity.startService(intent);
|
||||||
}
|
} else {
|
||||||
/*Intent intent = new Intent();
|
intent = new Intent();
|
||||||
try {
|
try {
|
||||||
intent.setAction(Intent.ACTION_VIEW);
|
intent.setAction(Intent.ACTION_VIEW);
|
||||||
intent.setDataAndType(Uri.parse(audioStream.url),
|
intent.setDataAndType(Uri.parse(audioStream.url),
|
||||||
MediaFormat.getMimeById(audioStream.format));
|
MediaFormat.getMimeById(audioStream.format));
|
||||||
intent.putExtra(Intent.EXTRA_TITLE, videoTitle);
|
intent.putExtra(Intent.EXTRA_TITLE, videoTitle);
|
||||||
intent.putExtra("title", videoTitle);
|
intent.putExtra("title", videoTitle);
|
||||||
|
|
||||||
activity.startActivity(intent); // HERE !!!
|
activity.startActivity(intent); // HERE !!!
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||||
builder.setMessage(R.string.noPlayerFound)
|
builder.setMessage(R.string.noPlayerFound)
|
||||||
.setPositiveButton(R.string.installStreamPlayer, new DialogInterface.OnClickListener() {
|
.setPositiveButton(R.string.installStreamPlayer, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.setAction(Intent.ACTION_VIEW);
|
intent.setAction(Intent.ACTION_VIEW);
|
||||||
intent.setData(Uri.parse(activity.getString(R.string.fdroidVLCurl)));
|
intent.setData(Uri.parse(activity.getString(R.string.fdroidVLCurl)));
|
||||||
activity.startActivity(intent);
|
activity.startActivity(intent);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
|
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
Log.i(TAG, "You unlocked a secret unicorn.");
|
Log.i(TAG, "You unlocked a secret unicorn.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
builder.create().show();
|
builder.create().show();
|
||||||
Log.e(TAG, "Either no Streaming player for audio was installed, or something important crashed:");
|
Log.e(TAG, "Either no Streaming player for audio was installed, or something important crashed:");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}*/
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,21 @@ package org.schabi.newpipe;
|
|||||||
|
|
||||||
import android.app.IntentService;
|
import android.app.IntentService;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.PendingIntent;
|
import android.app.Service;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.HandlerThread;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.os.Message;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
|
import android.support.v7.app.NotificationCompat;
|
||||||
|
import android.widget.Toast;
|
||||||
|
import android.os.Process;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -30,44 +40,145 @@ import java.io.IOException;
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
public class BackgroundPlayer extends IntentService /*implements MediaPlayer.OnPreparedListener*/ {
|
|
||||||
|
/**Plays the audio stream of videos in the background. */
|
||||||
|
public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPreparedListener*/ {
|
||||||
|
|
||||||
private static final String TAG = BackgroundPlayer.class.toString();
|
private static final String TAG = BackgroundPlayer.class.toString();
|
||||||
/**
|
private Looper mServiceLooper;
|
||||||
* Creates an IntentService. Invoked by your subclass's constructor.
|
private ServiceHandler mServiceHandler;
|
||||||
*/
|
|
||||||
public BackgroundPlayer() {
|
public BackgroundPlayer() {
|
||||||
super(TAG);
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
// Start up the thread running the service. Note that we create a
|
||||||
|
// separate thread because the service normally runs in the process's
|
||||||
|
// main thread, which we don't want to block. We also make it
|
||||||
|
// background priority so CPU-intensive work will not disrupt our UI.
|
||||||
|
super.onCreate();
|
||||||
|
HandlerThread thread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
// Get the HandlerThread's Looper and use it for our Handler
|
||||||
|
mServiceLooper = thread.getLooper();
|
||||||
|
mServiceHandler = new ServiceHandler(mServiceLooper);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
Toast.makeText(this, "Playing in background", Toast.LENGTH_SHORT).show();//todo:translation string
|
||||||
|
|
||||||
|
// For each start request, send a message to start a job and deliver the
|
||||||
|
// start ID so we know which request we're stopping when we finish the job
|
||||||
|
Message msg = mServiceHandler.obtainMessage();
|
||||||
|
msg.arg1 = startId;
|
||||||
|
msg.obj = intent;
|
||||||
|
mServiceHandler.sendMessage(msg);
|
||||||
|
|
||||||
|
// If we get killed, after returning from here, don't restart
|
||||||
|
return START_NOT_STICKY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
// We don't provide binding yet, so return null
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
//Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
|
||||||
|
mServiceLooper.quit();
|
||||||
|
}
|
||||||
|
|
||||||
protected void onHandleIntent(Intent intent) {
|
protected void onHandleIntent(Intent intent) {
|
||||||
String source = intent.getDataString();
|
String source = intent.getDataString();
|
||||||
|
|
||||||
|
if (intent.getAction().equals(ACTION_PLAY)) {
|
||||||
|
mMediaPlayer.setOnPreparedListener(this);
|
||||||
|
mMediaPlayer.prepareAsync(); // prepare async to not block main thread
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
MediaPlayer mediaPlayer = new MediaPlayer();
|
MediaPlayer mediaPlayer = new MediaPlayer();
|
||||||
//mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock apparently
|
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock
|
||||||
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||||
try {
|
try {
|
||||||
mediaPlayer.setDataSource(source);
|
mediaPlayer.setDataSource(source);
|
||||||
mediaPlayer.prepare(); // IntentService already puts us in a separate worker thread,
|
mediaPlayer.prepare(); //IntentService already puts us in a separate worker thread,
|
||||||
//so calling the blocking prepare() method should be ok
|
//so calling the blocking prepare() method should be ok
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
ioe.printStackTrace();
|
ioe.printStackTrace();
|
||||||
//can't really do anything useful without a file to play; exit early
|
//can't really do anything useful without a file to play; exit early
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WifiManager wifiMgr = ((WifiManager)getSystemService(Context.WIFI_SERVICE));
|
||||||
|
WifiManager.WifiLock wifiLock = wifiMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
|
||||||
|
|
||||||
|
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.setOnPreparedListener(this);
|
//mediaPlayer.setOnPreparedListener(this);
|
||||||
mediaPlayer.start();
|
mediaPlayer.start();
|
||||||
|
|
||||||
|
String videoTitle = intent.getStringExtra("title");
|
||||||
|
|
||||||
|
Notification noti = new NotificationCompat.Builder(this)
|
||||||
|
.setPriority(Notification.PRIORITY_LOW)
|
||||||
|
.setCategory(Notification.CATEGORY_TRANSPORT)
|
||||||
|
.setContentTitle(videoTitle)
|
||||||
|
.setContentText("NewPipe is playing in the background")//todo: add translatable string
|
||||||
|
.setOngoing(true)
|
||||||
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
startForeground(TAG.hashCode(), noti);
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
|
private class ListenerThread extends Thread implements AudioManager.OnAudioFocusChangeListener {
|
||||||
new Intent(getApplicationContext(), MainActivity.class),
|
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
}
|
||||||
Notification notification = new Notification();
|
|
||||||
notification.tickerText = text;
|
@Override
|
||||||
notification.icon = R.drawable.play0;
|
public void onAudioFocusChange(int focusChange) {
|
||||||
notification.flags |= Notification.FLAG_ONGOING_EVENT;
|
|
||||||
notification.setLatestEventInfo(getApplicationContext(), "MusicPlayerSample",
|
}*/
|
||||||
"Playing: " + songName, pi);
|
|
||||||
startForeground(NOTIFICATION_ID, notification);*/
|
private class EndListener implements MediaPlayer.OnCompletionListener {
|
||||||
|
private WifiManager.WifiLock wl;
|
||||||
|
public EndListener(WifiManager.WifiLock wifiLock) {
|
||||||
|
this.wl = wifiLock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCompletion(MediaPlayer mp) {
|
||||||
|
wl.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler that receives messages from the thread
|
||||||
|
private final class ServiceHandler extends Handler {
|
||||||
|
public ServiceHandler(Looper looper) {
|
||||||
|
super(looper);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
onHandleIntent((Intent)msg.obj);
|
||||||
|
// Stop the service using the startId, so that we don't stop
|
||||||
|
// the service in the middle of handling another job
|
||||||
|
stopSelfResult(msg.arg1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -509,8 +509,8 @@ public class YoutubeExtractor extends Extractor {
|
|||||||
//this page causes the NullPointerException, after finding it by searching for "tjvg":
|
//this page causes the NullPointerException, after finding it by searching for "tjvg":
|
||||||
//https://www.youtube.com/watch?v=Uqg0aEhLFAg
|
//https://www.youtube.com/watch?v=Uqg0aEhLFAg
|
||||||
String views = li.select("span.view-count").first().text();
|
String views = li.select("span.view-count").first().text();
|
||||||
Log.i(TAG, "title:"+info.title);
|
//Log.i(TAG, "title:"+info.title);
|
||||||
Log.i(TAG, "view count:"+views);
|
//Log.i(TAG, "view count:"+views);
|
||||||
try {
|
try {
|
||||||
info.view_count = Long.parseLong(li.select("span.view-count")
|
info.view_count = Long.parseLong(li.select("span.view-count")
|
||||||
.first().text().replaceAll("[^\\d]", ""));
|
.first().text().replaceAll("[^\\d]", ""));
|
||||||
|
Loading…
Reference in New Issue
Block a user