Skip to content

Commit

Permalink
[01] Added support for devices older than Lollipop - minSdk=16
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Medic authored and Stevan Medic committed Mar 24, 2016
1 parent b8f78c0 commit 42823ef
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 19 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ This is agains YT terms of service and could not be present on Google Play Store

Application provides mechanisms for searching videos and playlists on YT, as well as logging into a Google account in order to acquire private playlists, which normally cannot be accessed.

<img src="https://github.com/smedic/Android-YouTube-Background-Player/blob/master/raw/Screenshot_2016-03-16-12-14-49.png" alt="alt text" width="360" height="640">
<img src="https://github.com/smedic/Android-YouTube-Background-Player/blob/master/raw/Screenshot_2016-03-16-12-14-41.png" alt="alt text" width="360" height="640">

<img src="https://github.com/smedic/Android-YouTube-Background-Player/blob/master/raw/Screenshot_2016-03-24-10-21-27.png" alt="alt text" width="360" height="640">
<img src="https://github.com/smedic/Android-YouTube-Background-Player/blob/master/raw/Screenshot_2016-03-24-10-20-31.png" alt="alt text" width="360" height="640">
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
android:name=".BackgroundAudioService"
android:process=":youtube"></service>

<receiver android:name="receivers.MediaButtonIntentReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

</application>
</manifest>
34 changes: 18 additions & 16 deletions app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
Expand All @@ -34,6 +35,7 @@
import android.util.Log;
import android.util.SparseArray;

import com.smedic.tubtub.receivers.MediaButtonIntentReceiver;
import com.smedic.tubtub.utils.Config;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
Expand Down Expand Up @@ -130,7 +132,6 @@ private void handleIntent(Intent intent) {
* @param intent
*/
private void handleMedia(Intent intent) {

int intentMediaType = intent.getIntExtra(Config.YOUTUBE_MEDIA_TYPE, Config.YOUTUBE_NO_NEW_REQUEST);
switch (intentMediaType) {
case Config.YOUTUBE_NO_NEW_REQUEST: //video is paused,so no new playback requests should be processed
Expand Down Expand Up @@ -166,7 +167,17 @@ private void initMediaSessions() {
// permission in AndroidManifest.xml.
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

mSession = new MediaSessionCompat(getApplicationContext(), "simple player session");
ComponentName eventReceiver = new ComponentName(getApplicationContext().getPackageName(),
MediaButtonIntentReceiver.class.getName());
PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(
getApplicationContext(),
0,
new Intent(Intent.ACTION_MEDIA_BUTTON),
PendingIntent.FLAG_UPDATE_CURRENT
);

mSession = new MediaSessionCompat(getApplicationContext(), "simple player session",
eventReceiver, buttonReceiverIntent);

try {
mController = new MediaControllerCompat(getApplicationContext(), mSession.getSessionToken());
Expand All @@ -181,6 +192,7 @@ public void onPlay() {

@Override
public void onPause() {

super.onPause();
pauseVideo();
buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY));
Expand Down Expand Up @@ -252,6 +264,7 @@ private void buildNotification(NotificationCompat.Action action) {
builder.setShowWhen(false);
builder.setContentIntent(clickPendingIntent);
builder.setDeleteIntent(stopPendingIntent);
builder.setOngoing(false);
builder.setStyle(style);

//load bitmap for largeScreen
Expand All @@ -274,19 +287,9 @@ public void onPrepareLoad(Drawable drawable) {
});
}

if (mediaType == Config.YOUTUBE_PLAYLIST) {
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS));
} else {
builder.addAction(0, null, null);
}

builder.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS));
builder.addAction(action);

if (mediaType == Config.YOUTUBE_PLAYLIST) {
builder.addAction(generateAction(android.R.drawable.ic_media_next, "Next", ACTION_NEXT));
} else {
builder.addAction(0, null, null);
}
builder.addAction(generateAction(android.R.drawable.ic_media_next, "Next", ACTION_NEXT));

style.setShowActionsInCompactView(0, 1, 2);

Expand Down Expand Up @@ -328,7 +331,6 @@ private NotificationCompat.Action generateAction(int icon, String title, String
* Plays next video
*/
private void playNext() {

if (previousWasCalled) {
previousWasCalled = false;
iterator.next();
Expand All @@ -347,7 +349,6 @@ private void playNext() {
* Plays previous video
*/
private void playPrevious() {

if (nextWasCalled) {
iterator.previous();
nextWasCalled = false;
Expand Down Expand Up @@ -398,6 +399,7 @@ private void stopPlayer() {
* Extracts link from youtube video ID, so mediaPlayer can play it
*/
private void extractUrlAndPlay() {
Log.d(TAG, "extract url");
String youtubeLink = "http://youtube.com/watch?v=" + videoItem.getId();
YouTubeUriExtractor ytEx = new YouTubeUriExtractor(this) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ private Videos(String tableName) {
* @return
*/
public boolean create(YouTubeVideo video) {
if(checkIfExists(video.getId())){
return false;
}
// Gets the data repository in write mode
SQLiteDatabase db = dbHelper.getWritableDatabase();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.smedic.tubtub.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
* Created by smedic on 23.3.16..
*/
public class MediaButtonIntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("SMEDIC" , "onRecive");
}
}
Binary file removed raw/Screenshot_2016-03-16-12-14-41.png
Binary file not shown.
Binary file removed raw/Screenshot_2016-03-16-12-14-49.png
Binary file not shown.
Binary file added raw/Screenshot_2016-03-24-10-20-31.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added raw/Screenshot_2016-03-24-10-21-27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 42823ef

Please sign in to comment.