From 943027ffdd3436c05cdc27b0ff7cc0e7e0f475cf Mon Sep 17 00:00:00 2001 From: Adam Howard Date: Tue, 24 Nov 2015 00:40:36 +0000 Subject: [PATCH] implemented stage 1 ultra-basic background play of videos; see https://github.com/theScrabi/NewPipe/wiki/Background-Playback-Mini-Roadmap --- app/src/main/AndroidManifest.xml | 3 + .../org/schabi/newpipe/ActionBarHandler.java | 17 ++++- .../org/schabi/newpipe/BackgroundPlayer.java | 73 +++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 07029455e..b91e44fee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -71,6 +71,9 @@ android:parentActivityName=".VideoItemDetailActivity" > + + diff --git a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java index 4f28b606d..c14f93b56 100644 --- a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java +++ b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java @@ -287,13 +287,26 @@ public class ActionBarHandler { } public void playAudio() { - Intent intent = new Intent(); + boolean b = true;//todo: replace with preference + Intent intent; + if (b)//internal (background) music player: explicit intent + { + intent = new Intent(activity, BackgroundPlayer.class); + intent.setAction(Intent.ACTION_VIEW); + intent.setDataAndType(Uri.parse(audioStream.url), + MediaFormat.getMimeById(audioStream.format)); + intent.putExtra(Intent.EXTRA_TITLE, videoTitle); + intent.putExtra("title", videoTitle); + activity.startService(intent); + } + /*Intent intent = new Intent(); try { intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(audioStream.url), MediaFormat.getMimeById(audioStream.format)); intent.putExtra(Intent.EXTRA_TITLE, videoTitle); intent.putExtra("title", videoTitle); + activity.startActivity(intent); // HERE !!! } catch (Exception e) { e.printStackTrace(); @@ -317,6 +330,6 @@ public class ActionBarHandler { builder.create().show(); Log.e(TAG, "Either no Streaming player for audio was installed, or something important crashed:"); e.printStackTrace(); - } + }*/ } } diff --git a/app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java new file mode 100644 index 000000000..b47a59248 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/BackgroundPlayer.java @@ -0,0 +1,73 @@ +package org.schabi.newpipe; + +import android.app.IntentService; +import android.app.Notification; +import android.app.PendingIntent; +import android.content.Intent; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.os.PowerManager; + +import java.io.IOException; + +/** + * Created by Adam Howard on 08/11/15. + * + * Copyright (c) Adam Howard 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 . + */ +public class BackgroundPlayer extends IntentService /*implements MediaPlayer.OnPreparedListener*/ { + + private static final String TAG = BackgroundPlayer.class.toString(); + /** + * Creates an IntentService. Invoked by your subclass's constructor. + */ + public BackgroundPlayer() { + super(TAG); + } + + @Override + protected void onHandleIntent(Intent intent) { + String source = intent.getDataString(); + + MediaPlayer mediaPlayer = new MediaPlayer(); + //mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock apparently + mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); + try { + mediaPlayer.setDataSource(source); + mediaPlayer.prepare(); // IntentService already puts us in a separate worker thread, + //so calling the blocking prepare() method should be ok + } catch (IOException ioe) { + ioe.printStackTrace(); + //can't really do anything useful without a file to play; exit early + return; + } + //mediaPlayer.setOnPreparedListener(this); + mediaPlayer.start(); +/* + PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, + new Intent(getApplicationContext(), MainActivity.class), + PendingIntent.FLAG_UPDATE_CURRENT); + Notification notification = new Notification(); + notification.tickerText = text; + notification.icon = R.drawable.play0; + notification.flags |= Notification.FLAG_ONGOING_EVENT; + notification.setLatestEventInfo(getApplicationContext(), "MusicPlayerSample", + "Playing: " + songName, pi); + startForeground(NOTIFICATION_ID, notification);*/ + } +}