Added null checks, moved MediaPlayer to its own Koin module

This commit is contained in:
Nite 2020-06-26 16:33:27 +02:00
parent bbe9f39300
commit bd77c2a851
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
6 changed files with 80 additions and 61 deletions

View File

@ -47,7 +47,6 @@ public class Downloader
private ScheduledExecutorService executorService; private ScheduledExecutorService executorService;
private long revision; private long revision;
public Downloader(Context context, ShufflePlayBuffer shufflePlayBuffer, ExternalStorageMonitor externalStorageMonitor, public Downloader(Context context, ShufflePlayBuffer shufflePlayBuffer, ExternalStorageMonitor externalStorageMonitor,
LocalMediaPlayer localMediaPlayer) LocalMediaPlayer localMediaPlayer)
{ {
@ -150,6 +149,7 @@ public class Downloader
if (start == -1) start = 0; if (start == -1) start = 0;
int i = start; int i = start;
// Check all DownloadFiles on the playlist
do do
{ {
DownloadFile downloadFile = downloadList.get(i); DownloadFile downloadFile = downloadList.get(i);
@ -162,6 +162,7 @@ public class Downloader
cleanupCandidates.add(currentDownloading); cleanupCandidates.add(currentDownloading);
if (i == (start + 1)) if (i == (start + 1))
{ {
// The next file on the playlist is currently downloading
localMediaPlayer.setNextPlayerState(DOWNLOADING); localMediaPlayer.setNextPlayerState(DOWNLOADING);
} }
break; break;
@ -176,6 +177,7 @@ public class Downloader
} while (i != start); } while (i != start);
} }
// If the downloadList contains no work, check the backgroundDownloadList
if ((preloaded + 1 == n || preloaded >= Util.getPreloadCount(context) || downloadList.isEmpty()) && !backgroundDownloadList.isEmpty()) if ((preloaded + 1 == n || preloaded >= Util.getPreloadCount(context) || downloadList.isEmpty()) && !backgroundDownloadList.isEmpty())
{ {
for (int i = 0; i < backgroundDownloadList.size(); i++) for (int i = 0; i < backgroundDownloadList.size(); i++)

View File

@ -293,14 +293,17 @@ public class LocalMediaPlayer
updateRemoteControl(); updateRemoteControl();
} }
Handler mainHandler = new Handler(context.getMainLooper()); if (onPlayerStateChanged != null)
Runnable myRunnable = new Runnable() { {
@Override Handler mainHandler = new Handler(context.getMainLooper());
public void run() { Runnable myRunnable = new Runnable() {
onPlayerStateChanged.accept(playerState, currentPlaying); @Override
} public void run() {
}; onPlayerStateChanged.accept(playerState, currentPlaying);
mainHandler.post(myRunnable); }
};
mainHandler.post(myRunnable);
}
if (playerState == STARTED && positionCache == null) if (playerState == STARTED && positionCache == null)
{ {
@ -321,14 +324,17 @@ public class LocalMediaPlayer
this.currentPlaying = currentPlaying; this.currentPlaying = currentPlaying;
updateRemoteControl(); updateRemoteControl();
Handler mainHandler = new Handler(context.getMainLooper()); if (onCurrentPlayingChanged != null)
Runnable myRunnable = new Runnable() { {
@Override Handler mainHandler = new Handler(context.getMainLooper());
public void run() { Runnable myRunnable = new Runnable() {
onCurrentPlayingChanged.accept(currentPlaying); @Override
} public void run() {
}; onCurrentPlayingChanged.accept(currentPlaying);
mainHandler.post(myRunnable); }
};
mainHandler.post(myRunnable);
}
} }
public synchronized void setNextPlaying(DownloadFile nextToPlay) public synchronized void setNextPlaying(DownloadFile nextToPlay)
@ -398,14 +404,16 @@ public class LocalMediaPlayer
setPlayerState(PlayerState.STARTED); setPlayerState(PlayerState.STARTED);
setupHandlers(currentPlaying, false); setupHandlers(currentPlaying, false);
Handler mainHandler = new Handler(context.getMainLooper()); if (onNextSongRequested != null) {
Runnable myRunnable = new Runnable() { Handler mainHandler = new Handler(context.getMainLooper());
@Override Runnable myRunnable = new Runnable() {
public void run() { @Override
onNextSongRequested.run(); public void run() {
} onNextSongRequested.run();
}; }
mainHandler.post(myRunnable); };
mainHandler.post(myRunnable);
}
// Proxy should not be being used here since the next player was already setup to play // Proxy should not be being used here since the next player was already setup to play
if (proxy != null) if (proxy != null)
@ -726,14 +734,16 @@ public class LocalMediaPlayer
} }
} }
Handler mainHandler = new Handler(context.getMainLooper()); if (onPrepared != null) {
Runnable myRunnable = new Runnable() { Handler mainHandler = new Handler(context.getMainLooper());
@Override Runnable myRunnable = new Runnable() {
public void run() { @Override
onPrepared.run(); public void run() {
} onPrepared.run();
}; }
mainHandler.post(myRunnable); };
mainHandler.post(myRunnable);
}
} }
}); });
@ -862,14 +872,17 @@ public class LocalMediaPlayer
} }
else else
{ {
Handler mainHandler = new Handler(context.getMainLooper()); if (onSongCompleted != null)
Runnable myRunnable = new Runnable() { {
@Override Handler mainHandler = new Handler(context.getMainLooper());
public void run() { Runnable myRunnable = new Runnable() {
onSongCompleted.accept(currentPlaying); @Override
} public void run() {
}; onSongCompleted.accept(currentPlaying);
mainHandler.post(myRunnable); }
};
mainHandler.post(myRunnable);
}
} }
return; return;

View File

@ -170,7 +170,7 @@ public class StreamProxy implements Runnable
public void run() public void run()
{ {
Log.i(TAG, "Streaming song in background"); Log.i(TAG, "Streaming song in background");
DownloadFile downloadFile = currentPlaying.get(); DownloadFile downloadFile = currentPlaying == null? null : currentPlaying.get();
MusicDirectory.Entry song = downloadFile.getSong(); MusicDirectory.Entry song = downloadFile.getSong();
long fileSize = downloadFile.getBitRate() * ((song.getDuration() != null) ? song.getDuration() : 0) * 1000 / 8; long fileSize = downloadFile.getBitRate() * ((song.getDuration() != null) ? song.getDuration() : 0) * 1000 / 8;
Log.i(TAG, String.format("Streaming fileSize: %d", fileSize)); Log.i(TAG, String.format("Streaming fileSize: %d", fileSize));

View File

@ -2,12 +2,7 @@ package org.moire.ultrasonic.app
import androidx.multidex.MultiDexApplication import androidx.multidex.MultiDexApplication
import org.koin.android.ext.android.startKoin import org.koin.android.ext.android.startKoin
import org.moire.ultrasonic.di.DiProperties import org.moire.ultrasonic.di.*
import org.moire.ultrasonic.di.appPermanentStorage
import org.moire.ultrasonic.di.baseNetworkModule
import org.moire.ultrasonic.di.directoriesModule
import org.moire.ultrasonic.di.featureFlagsModule
import org.moire.ultrasonic.di.musicServiceModule
class UApp : MultiDexApplication() { class UApp : MultiDexApplication() {
override fun onCreate() { override fun onCreate() {
@ -20,7 +15,8 @@ class UApp : MultiDexApplication() {
appPermanentStorage, appPermanentStorage,
baseNetworkModule, baseNetworkModule,
featureFlagsModule, featureFlagsModule,
musicServiceModule musicServiceModule,
mediaPlayerModule
), ),
extraProperties = mapOf( extraProperties = mapOf(
DiProperties.APP_CONTEXT to applicationContext DiProperties.APP_CONTEXT to applicationContext

View File

@ -0,0 +1,20 @@
package org.moire.ultrasonic.di
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module.module
import org.moire.ultrasonic.service.*
import org.moire.ultrasonic.util.ShufflePlayBuffer
val mediaPlayerModule = module {
single<MediaPlayerController> { MediaPlayerControllerImpl(androidContext(), get(), get(), get(), get(), get()) }
single { JukeboxMediaPlayer(androidContext(), get()) }
single { MediaPlayerLifecycleSupport(androidContext(), get(), get(), get()) }
single { DownloadQueueSerializer(androidContext()) }
single { ExternalStorageMonitor(androidContext()) }
single { ShufflePlayBuffer(androidContext()) }
single { Downloader(androidContext(), get(), get(), get()) }
single { LocalMediaPlayer(androidContext()) }
// TODO: Ideally this can be cleaned up when all circular references are removed.
single { MediaPlayerControllerImpl(androidContext(), get(), get(), get(), get(), get()) }
}

View File

@ -112,16 +112,4 @@ val musicServiceModule = module(MUSIC_SERVICE_CONTEXT) {
} }
single { SubsonicImageLoader(getProperty(DiProperties.APP_CONTEXT), get()) } single { SubsonicImageLoader(getProperty(DiProperties.APP_CONTEXT), get()) }
single<MediaPlayerController> { MediaPlayerControllerImpl(androidContext(), get(), get(), get(), get(), get()) }
single { JukeboxMediaPlayer(androidContext(), get()) }
single { MediaPlayerLifecycleSupport(androidContext(), get(), get(), get()) }
single { DownloadQueueSerializer(androidContext()) }
single { ExternalStorageMonitor(androidContext()) }
single { ShufflePlayBuffer(androidContext()) }
single { Downloader(androidContext(), get(), get(), get()) }
single { LocalMediaPlayer(androidContext()) }
// TODO: Ideally this can be cleaned up when all circular references are removed.
single { MediaPlayerControllerImpl(androidContext(), get(), get(), get(), get(), get()) }
} }