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

View File

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

View File

@ -170,7 +170,7 @@ public class StreamProxy implements Runnable
public void run()
{
Log.i(TAG, "Streaming song in background");
DownloadFile downloadFile = currentPlaying.get();
DownloadFile downloadFile = currentPlaying == null? null : currentPlaying.get();
MusicDirectory.Entry song = downloadFile.getSong();
long fileSize = downloadFile.getBitRate() * ((song.getDuration() != null) ? song.getDuration() : 0) * 1000 / 8;
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 org.koin.android.ext.android.startKoin
import org.moire.ultrasonic.di.DiProperties
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
import org.moire.ultrasonic.di.*
class UApp : MultiDexApplication() {
override fun onCreate() {
@ -20,7 +15,8 @@ class UApp : MultiDexApplication() {
appPermanentStorage,
baseNetworkModule,
featureFlagsModule,
musicServiceModule
musicServiceModule,
mediaPlayerModule
),
extraProperties = mapOf(
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<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()) }
}