Skip to content

Commit

Permalink
fixed playFrom() crash, support queueing entire row (eg: last played) (
Browse files Browse the repository at this point in the history
…#1568)

* fixed playFrom() crash, support queueing entire row (eg: last played)

* store MediaManager as a variable for readability
  • Loading branch information
mueslimak3r authored Mar 27, 2022
1 parent df01514 commit 7491ed1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.jellyfin.androidtv.ui.playback.AudioNowPlayingActivity;
import org.jellyfin.androidtv.ui.playback.MediaManager;
import org.jellyfin.androidtv.ui.playback.PlaybackLauncher;
import org.jellyfin.androidtv.util.KeyProcessor;
import org.jellyfin.androidtv.util.Utils;
import org.jellyfin.androidtv.util.apiclient.PlaybackHelper;
import org.jellyfin.apiclient.interaction.ApiClient;
Expand Down Expand Up @@ -139,17 +138,30 @@ public static void launch(final BaseRowItem rowItem, ItemRowAdapter adapter, int

case Audio:
Timber.d("got pos %s", pos);
// if a song isn't the first in the queue, play it
if (rowItem.getIndex() > KoinJavaComponent.<MediaManager>get(MediaManager.class).getCurrentAudioQueuePosition() && rowItem.getIndex() < KoinJavaComponent.<MediaManager>get(MediaManager.class).getCurrentAudioQueueSize()) {
KoinJavaComponent.<MediaManager>get(MediaManager.class).playFrom(rowItem.getIndex());
} else {
if (rowItem.getBaseItem() == null)
return;
MediaManager mediaManager = KoinJavaComponent.<MediaManager>get(MediaManager.class);

// if the song currently playing is selected (and is the exact item - this only happens in the nowPlayingRow), open AudioNowPlayingActivity
if (mediaManager.hasAudioQueueItems() && rowItem.getBaseItem() == mediaManager.getCurrentAudioItem()) {
// otherwise, open AudioNowPlayingActivity
Intent nowPlaying = new Intent(activity, AudioNowPlayingActivity.class);
activity.startActivity(nowPlaying);
} else if (mediaManager.hasAudioQueueItems() && rowItem instanceof AudioQueueItem && pos < mediaManager.getCurrentAudioQueueSize()) {
Timber.d("playing audio queue item");
mediaManager.playFrom(pos);
} else {
Timber.d("playing audio item");
List<BaseItemDto> audioItemsAsList = new ArrayList<>();

for (Object item : adapter.unmodifiableList()) {
if (item instanceof BaseRowItem && ((BaseRowItem) item).getBaseItem() != null)
audioItemsAsList.add(((BaseRowItem) item).getBaseItem());
}
mediaManager.playNow(activity, audioItemsAsList, pos, false);
}

return;

case Season:
case RecordingGroup:
//Start activity for enhanced browse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.jellyfin.apiclient.interaction.Response;
import org.jellyfin.apiclient.model.dlna.DeviceProfile;
import org.jellyfin.apiclient.model.dto.BaseItemDto;
import org.jellyfin.apiclient.model.dto.BaseItemType;
import org.jellyfin.apiclient.model.playlists.PlaylistCreationRequest;
import org.jellyfin.apiclient.model.playlists.PlaylistCreationResult;
import org.jellyfin.sdk.model.DeviceInfo;
Expand Down Expand Up @@ -242,6 +243,7 @@ private void reportProgress() {
}

private void onComplete() {
Timber.d("item complete");
stopAudio(hasNextAudioItem());

if (hasNextAudioItem()) {
Expand All @@ -252,6 +254,7 @@ private void onComplete() {
}

private void releasePlayer() {
Timber.d("releasing audio player");
if (mVlcPlayer != null) {
mVlcPlayer.setEventListener(null);
mVlcPlayer.release();
Expand Down Expand Up @@ -514,7 +517,12 @@ public int addToVideoQueue(BaseItemDto item) {
}

public void clearAudioQueue() {
stopAudio(true);
clearAudioQueue(false);
}

public void clearAudioQueue(boolean releasePlayer) {
Timber.d("clearing the audio queue");
stopAudio(releasePlayer);
clearUnShuffledQueue();
if (mCurrentAudioQueue == null) {
createAudioQueue(new ArrayList<BaseItemDto>());
Expand Down Expand Up @@ -602,7 +610,18 @@ public void playNow(Context context, final List<BaseItemDto> items, int position

boolean fireQueueReplaceEvent = hasAudioQueueItems();

playNowInternal(context, items, position, shuffle);
List<BaseItemDto> list = new ArrayList<BaseItemDto>();
for (int i = 0; i < items.size(); i++){
if (items.get(i).getBaseItemType() == BaseItemType.Audio) {
list.add(items.get(i));
} else if (i < position) {
position--;
}
}
if (position < 0)
position = 0;

playNowInternal(context, list, position, shuffle);

if (fireQueueReplaceEvent)
fireQueueReplaced();
Expand Down Expand Up @@ -644,10 +663,14 @@ private void playNowInternal(Context context, List<BaseItemDto> items, int posit
}

public boolean playFrom(int ndx) {
if (ndx >= mCurrentAudioQueue.size()) return false;
if (mCurrentAudioQueue == null || ndx >= mCurrentAudioQueue.size()) return false;

if (!ensureInitialized()) return false;

if (isPlayingAudio()) stopAudio(false);

Timber.d("playing audio queue from pos %s", ndx);

mCurrentAudioQueuePosition = ndx < 0 || ndx >= mCurrentAudioQueue.size() ? -1 : ndx - 1;
createManagedAudioQueue();
nextAudioItem();
Expand Down Expand Up @@ -886,7 +909,7 @@ public int prevAudioItem() {

private void stop() {
if (!getIsAudioPlayerInitialized()) return ;
if (nativeMode) mExoPlayer.stop(true);
if (nativeMode) mExoPlayer.stop();
else mVlcPlayer.stop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public static PopupMenu createItemMenu(BaseRowItem rowItem, UserItemDataDto user

if (rowItem instanceof AudioQueueItem) {
if (activity instanceof AudioNowPlayingActivity) {
if (rowItem.getIndex() > KoinJavaComponent.<MediaManager>get(MediaManager.class).getCurrentAudioQueuePosition() && rowItem.getIndex() < KoinJavaComponent.<MediaManager>get(MediaManager.class).getCurrentAudioQueueSize())
menu.getMenu().add(0, MENU_ADVANCE_QUEUE, order++, R.string.lbl_play_from_here);
if (rowItem.getBaseItem() != KoinJavaComponent.<MediaManager>get(MediaManager.class).getCurrentAudioItem())
menu.getMenu().add(0, MENU_ADVANCE_QUEUE, order++, R.string.lbl_play_from_here);
} else {
menu.getMenu().add(0, MENU_GOTO_NOW_PLAYING, order++, R.string.lbl_goto_now_playing);

Expand Down Expand Up @@ -422,8 +422,9 @@ public void onError(Exception exception) {
return true;
case MENU_ADVANCE_QUEUE:
KoinJavaComponent.<MediaManager>get(MediaManager.class).playFrom(mCurrentRowItemNdx);
return true;
case MENU_CLEAR_QUEUE:
KoinJavaComponent.<MediaManager>get(MediaManager.class).clearAudioQueue();
KoinJavaComponent.<MediaManager>get(MediaManager.class).clearAudioQueue(true);
return true;
case MENU_INSTANT_MIX:
PlaybackHelper.playInstantMix(mCurrentActivity, mCurrentItem);
Expand Down

0 comments on commit 7491ed1

Please sign in to comment.