Fix Audio Crossfade

This commit is contained in:
Prathamesh More 2022-05-02 09:09:00 +05:30
parent 608c265390
commit 806b3beaa7
4 changed files with 15 additions and 25 deletions

View file

@ -120,20 +120,9 @@ class CrossFadePlayer(val context: Context) : Playback, MediaPlayer.OnCompletion
override val isPlaying: Boolean
get() = mIsInitialized && getCurrentPlayer()?.isPlaying == true
// This has to run when queue is changed or song is changed manually by user
fun sourceChangedByUser() {
hasDataSource = false
cancelFade()
getCurrentPlayer()?.apply {
if (isPlaying) stop()
}
getNextPlayer()?.apply {
if (isPlaying) stop()
}
}
override fun setDataSource(path: String): Boolean {
override fun setDataSource(path: String, force: Boolean): Boolean {
cancelFade()
if (force) hasDataSource = false
mIsInitialized = false
/* We've already set DataSource if initialized is true in setNextDataSource */
if (!hasDataSource) {
@ -154,7 +143,7 @@ class CrossFadePlayer(val context: Context) : Playback, MediaPlayer.OnCompletion
*/
private fun setDataSourceImpl(
player: MediaPlayer,
path: String
path: String,
): Boolean {
player.reset()
player.setOnPreparedListener(null)

View file

@ -30,6 +30,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull;
import code.name.monkey.appthemehelper.util.VersionUtils;
import code.name.monkey.retromusic.ConstantsKt;
import code.name.monkey.retromusic.R;
@ -66,7 +68,7 @@ public class MultiPlayer
* @return True if the <code>player</code> has been prepared and is ready to play, false otherwise
*/
@Override
public boolean setDataSource(@NonNull final String path) {
public boolean setDataSource(@NotNull final String path, boolean force) {
mIsInitialized = false;
mIsInitialized = setDataSourceImpl(mCurrentMediaPlayer, path);
if (mIsInitialized) {

View file

@ -92,6 +92,7 @@ import code.name.monkey.retromusic.util.PreferenceUtil.unregisterOnSharedPrefere
import code.name.monkey.retromusic.volume.AudioVolumeObserver
import code.name.monkey.retromusic.volume.OnAudioVolumeChangedListener
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import org.koin.java.KoinJavaComponent.get
import java.util.*
@ -910,14 +911,6 @@ class MusicService : MediaBrowserServiceCompat(),
}
fun playSongAtImpl(position: Int) {
if (!trackEndedByCrossfade) {
// This is only imp if we are using crossfade
if (playback is CrossFadePlayer) {
(playback as CrossFadePlayer).sourceChangedByUser()
}
} else {
trackEndedByCrossfade = false
}
if (openTrackAndPrepareNextAt(position)) {
play()
} else {
@ -1285,9 +1278,15 @@ class MusicService : MediaBrowserServiceCompat(),
@Synchronized
private fun openCurrent(): Boolean {
val force = if (!trackEndedByCrossfade) {
true
} else {
trackEndedByCrossfade = false
false
}
return try {
if (playback != null) {
return playback!!.setDataSource(getTrackUri(currentSong))
return playback!!.setDataSource(getTrackUri(currentSong), force)
} else false
} catch (e: Exception) {
e.printStackTrace()

View file

@ -23,7 +23,7 @@ interface Playback {
val audioSessionId: Int
fun setDataSource(path: String): Boolean
fun setDataSource(path: String, force: Boolean): Boolean
fun setNextDataSource(path: String?)