Skip to content

Commit

Permalink
Merge pull request #7 from arafsheikh/bestaudio
Browse files Browse the repository at this point in the history
Use the best available audio stream
  • Loading branch information
smedic authored Sep 12, 2016
2 parents 3a4314d + 2f9e30d commit d4f9ac9
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ public class BackgroundAudioService extends Service implements MediaPlayer.OnCom

private static final String TAG = "SMEDIC service";

private static final int YOUTUBE_ITAG_140 = 140; //mp4a - stereo, 44.1 KHz 128 Kbps
private static final int YOUTUBE_ITAG_17 = 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";
public static final String ACTION_NEXT = "action_next";
Expand Down Expand Up @@ -457,6 +454,23 @@ private void stopPlayer() {
mMediaPlayer.release();
}

/**
* Get the best available audio stream
*
* @param ytFiles Array of available streams
* @return Audio stream with highest bitrate
*/
private YtFile getBestStream(SparseArray<YtFile> ytFiles) {
if (ytFiles.get(141) != null) {
return ytFiles.get(141); //mp4a - stereo, 44.1 KHz 256 Kbps
} else if (ytFiles.get(251) != null) {
return ytFiles.get(251); //webm - stereo, 48 KHz 160 Kbps
} else if (ytFiles.get(140) != null) {
return ytFiles.get(140); //mp4a - stereo, 44.1 KHz 128 Kbps
}
return ytFiles.get(17); //mp4 - stereo, 44.1 KHz 96-100 Kbps
}

/**
* Extracts link from youtube video ID, so mediaPlayer can play it
*/
Expand All @@ -467,10 +481,7 @@ private void extractUrlAndPlay() {
@Override
public void onUrisAvailable(String videoId, String videoTitle, SparseArray<YtFile> ytFiles) {
if (ytFiles != null) {
YtFile ytFile = ytFiles.get(YOUTUBE_ITAG_140);
if (ytFile == null) {
ytFile = ytFiles.get(YOUTUBE_ITAG_17);
}
YtFile ytFile = getBestStream(ytFiles);
try {
Log.d(TAG, "Start playback");
if (mMediaPlayer != null) {
Expand Down

0 comments on commit d4f9ac9

Please sign in to comment.