Skip to content

Commit

Permalink
[01] Fixed bugs and code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Medic committed Mar 25, 2016
1 parent 47410f3 commit 2429238
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ dependencies {
compile files('libs/jsr305-1.3.9.jar')
compile files('libs/google-api-services-youtube-v3-rev160-1.21.0.jar')
compile files('libs/picasso-2.5.2.jar')
compile files('libs/okhttp-3.2.0.jar') //used for picasso caching
compile files('libs/youtubeExtractor.jar')

compile 'com.pes.materialcolorpicker:library:1.0.+'
}
Binary file added app/libs/okhttp-3.2.0.jar
Binary file not shown.
97 changes: 63 additions & 34 deletions app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public class BackgroundAudioService extends Service implements MediaPlayer.OnCom

private static final String TAG = "SMEDIC service";

private static final int YOUTUBE_ITAG = 140; //mp4a - stereo, 44.1 KHz 128 Kbps
private static final int YOUTUBE_ITAG_140 = 140; //mp4a - stereo, 44.1 KHz 128 Kbps
private static final int YOUTUBE_ITAG_18 = 17; //mp4 - stereo, 44.1 KHz 96-100 Kbps

public static final String ACTION_PLAY = "action_play";
public static final String ACTION_PAUSE = "action_pause";
Expand All @@ -68,7 +69,7 @@ public class BackgroundAudioService extends Service implements MediaPlayer.OnCom
private MediaSessionCompat mSession;
private MediaControllerCompat mController;

private int mediaType = Config.YOUTUBE_NO_NEW_REQUEST;
private int mediaType = Config.YOUTUBE_MEDIA_NO_NEW_REQUEST;

private YouTubeVideo videoItem;

Expand Down Expand Up @@ -132,22 +133,23 @@ 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);
int intentMediaType = intent.getIntExtra(Config.YOUTUBE_TYPE, Config.YOUTUBE_MEDIA_NO_NEW_REQUEST);
switch (intentMediaType) {
case Config.YOUTUBE_NO_NEW_REQUEST: //video is paused,so no new playback requests should be processed
case Config.YOUTUBE_MEDIA_NO_NEW_REQUEST: //video is paused,so no new playback requests should be processed
mMediaPlayer.start();
break;
case Config.YOUTUBE_VIDEO:
mediaType = Config.YOUTUBE_VIDEO;
case Config.YOUTUBE_MEDIA_TYPE_VIDEO:
mediaType = Config.YOUTUBE_MEDIA_TYPE_VIDEO;
videoItem = (YouTubeVideo) intent.getSerializableExtra(Config.YOUTUBE_TYPE_VIDEO);
if (videoItem.getId() != null) {
playVideo();
}
break;
case Config.YOUTUBE_PLAYLIST: //new playlist playback request
mediaType = Config.YOUTUBE_PLAYLIST;
case Config.YOUTUBE_MEDIA_TYPE_PLAYLIST: //new playlist playback request
mediaType = Config.YOUTUBE_MEDIA_TYPE_PLAYLIST;
youTubeVideos = (ArrayList<YouTubeVideo>) intent.getSerializableExtra(Config.YOUTUBE_TYPE_PLAYLIST);
iterator = youTubeVideos.listIterator();
int startPosition = intent.getIntExtra(Config.YOUTUBE_TYPE_PLAYLIST_VIDEO_POS, 0);
iterator = youTubeVideos.listIterator(startPosition);
playNext();
break;
default:
Expand Down Expand Up @@ -265,42 +267,45 @@ private void buildNotification(NotificationCompat.Action action) {
builder.setContentIntent(clickPendingIntent);
builder.setDeleteIntent(stopPendingIntent);
builder.setOngoing(false);
builder.setSubText(videoItem.getViewCount());
builder.setStyle(style);

//load bitmap for largeScreen
if (videoItem.getThumbnailURL() != null && !videoItem.getThumbnailURL().isEmpty()) {
Picasso.with(this)
.load(videoItem.getThumbnailURL())
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
updateNotificationLargeIcon(bitmap);
}

@Override
public void onBitmapFailed(Drawable drawable) {
}

@Override
public void onPrepareLoad(Drawable drawable) {
}
});
.into(target);
}

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

style.setShowActionsInCompactView(0, 1, 2);

//Notification notification = builder.build();
//startForeground(R.string.app_name, notification);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());

}

/**
* Field which handles image loading
*/
private Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
updateNotificationLargeIcon(bitmap);
}

@Override
public void onBitmapFailed(Drawable drawable) {
Log.d(TAG, "Load bitmap... failed");
}

@Override
public void onPrepareLoad(Drawable drawable) {
}
};

/**
* Updates only large icon in notification panel when bitmap is decoded
*
Expand Down Expand Up @@ -328,9 +333,16 @@ private NotificationCompat.Action generateAction(int icon, String title, String
}

/**
* Plays next video
* Plays next video in playlist
*/
private void playNext() {
//if media type is video not playlist, just loop it
if (mediaType == Config.YOUTUBE_MEDIA_TYPE_VIDEO) {
seekVideo(0);
restartVideo();
return;
}

if (previousWasCalled) {
previousWasCalled = false;
iterator.next();
Expand All @@ -346,9 +358,15 @@ private void playNext() {
}

/**
* Plays previous video
* Plays previous video in playlist
*/
private void playPrevious() {
//if media type is video not playlist, just loop it
if (mediaType == Config.YOUTUBE_MEDIA_TYPE_VIDEO) {
restartVideo();
return;
}

if (nextWasCalled) {
iterator.previous();
nextWasCalled = false;
Expand Down Expand Up @@ -387,6 +405,15 @@ private void restartVideo() {
mMediaPlayer.start();
}

/**
* Seeks to specific time
*
* @param seekTo
*/
private void seekVideo(int seekTo) {
mMediaPlayer.seekTo(seekTo);
}

/**
* Stops video
*/
Expand All @@ -399,18 +426,20 @@ 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();
final String youtubeLink = "http://youtube.com/watch?v=" + videoItem.getId();
YouTubeUriExtractor ytEx = new YouTubeUriExtractor(this) {
@Override
public void onUrisAvailable(String videoId, String videoTitle, SparseArray<YtFile> ytFiles) {
if (ytFiles != null) {
String downloadUrl = ytFiles.get(YOUTUBE_ITAG).getUrl();
YtFile ytFile = ytFiles.get(YOUTUBE_ITAG_140);
if (ytFile == null) {
ytFile = ytFiles.get(YOUTUBE_ITAG_18);
}
try {
Log.d(TAG, "Start playback");
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(downloadUrl);
mMediaPlayer.setDataSource(ytFile.getUrl());
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.prepare();
mMediaPlayer.start();
Expand All @@ -432,7 +461,7 @@ public void onTaskRemoved(Intent rootIntent) {

@Override
public void onCompletion(MediaPlayer _mediaPlayer) {
if (mediaType == Config.YOUTUBE_PLAYLIST) {
if (mediaType == Config.YOUTUBE_MEDIA_TYPE_PLAYLIST) {
playNext();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public void onItemClick(AdapterView<?> av, View v, final int pos,

Intent serviceIntent = new Intent(getContext(), BackgroundAudioService.class);
serviceIntent.setAction(BackgroundAudioService.ACTION_PLAY);
serviceIntent.putExtra(Config.YOUTUBE_MEDIA_TYPE, Config.YOUTUBE_PLAYLIST);
serviceIntent.putExtra(Config.YOUTUBE_TYPE, Config.YOUTUBE_MEDIA_TYPE_PLAYLIST);
serviceIntent.putExtra(Config.YOUTUBE_TYPE_PLAYLIST, favoriteVideos);
serviceIntent.putExtra(Config.YOUTUBE_TYPE_PLAYLIST_VIDEO_POS, pos);
getActivity().startService(serviceIntent);
} else {
conf.createNetErrorDialog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void run() {
} else {
Intent serviceIntent = new Intent(getContext(), BackgroundAudioService.class);
serviceIntent.setAction(BackgroundAudioService.ACTION_PLAY);
serviceIntent.putExtra(Config.YOUTUBE_MEDIA_TYPE, Config.YOUTUBE_PLAYLIST);
serviceIntent.putExtra(Config.YOUTUBE_TYPE, Config.YOUTUBE_MEDIA_TYPE_PLAYLIST);
serviceIntent.putExtra(Config.YOUTUBE_TYPE_PLAYLIST, youTubeVideos);
getActivity().startService(serviceIntent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void onItemClick(AdapterView<?> av, View v, final int pos,

Intent serviceIntent = new Intent(getContext(), BackgroundAudioService.class);
serviceIntent.setAction(BackgroundAudioService.ACTION_PLAY);
serviceIntent.putExtra(Config.YOUTUBE_MEDIA_TYPE, Config.YOUTUBE_VIDEO);
serviceIntent.putExtra(Config.YOUTUBE_TYPE, Config.YOUTUBE_MEDIA_TYPE_VIDEO);
serviceIntent.putExtra(Config.YOUTUBE_TYPE_VIDEO, recentlyPlayedVideos.get(pos));
getActivity().startService(serviceIntent);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void onItemClick(AdapterView<?> av, View v, int pos,

Intent serviceIntent = new Intent(getContext(), BackgroundAudioService.class);
serviceIntent.setAction(BackgroundAudioService.ACTION_PLAY);
serviceIntent.putExtra(Config.YOUTUBE_MEDIA_TYPE, Config.YOUTUBE_VIDEO);
serviceIntent.putExtra(Config.YOUTUBE_TYPE, Config.YOUTUBE_MEDIA_TYPE_VIDEO);
serviceIntent.putExtra(Config.YOUTUBE_TYPE_VIDEO, searchResultsList.get(pos));
getActivity().startService(serviceIntent);
}
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/smedic/tubtub/utils/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ public final class Config {

public static final boolean DEBUG = false;

public static final int YOUTUBE_NO_NEW_REQUEST = -1;
public static final int YOUTUBE_VIDEO = 0;
public static final int YOUTUBE_PLAYLIST = 1;
public static final int YOUTUBE_MEDIA_NO_NEW_REQUEST = -1;
public static final int YOUTUBE_MEDIA_TYPE_VIDEO = 0;
public static final int YOUTUBE_MEDIA_TYPE_PLAYLIST = 1;

public static final String YOUTUBE_MEDIA_TYPE = "YT_MEDIA_TYPE";
public static final String YOUTUBE_TYPE = "YT_MEDIA_TYPE";
public static final String YOUTUBE_TYPE_VIDEO = "YT_VIDEO";
public static final String YOUTUBE_TYPE_PLAYLIST= "YT_PLAYLIST";
public static final String YOUTUBE_TYPE_PLAYLIST_VIDEO_POS = "YT_PLAYLIST_VIDEO_POS";

public static final String YOUTUBE_API_KEY = "AIzaSyAR3lyb-ucc8JYrSHw0rfCaXCYHveGy6U8";

Expand Down

0 comments on commit 2429238

Please sign in to comment.