1
0
mirror of https://github.com/ultrasonic/ultrasonic synced 2025-02-19 21:20:52 +01:00

Make doPlay method a private method, and play the only public entry point.

This commit is contained in:
tzugen 2021-03-24 13:45:46 +01:00
parent d017ca9fb2
commit 09fb6aa487
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 31 additions and 11 deletions

View File

@ -131,7 +131,7 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
{ {
if (localMediaPlayer.currentPlaying.isCompleteFileAvailable()) if (localMediaPlayer.currentPlaying.isCompleteFileAvailable())
{ {
localMediaPlayer.doPlay(localMediaPlayer.currentPlaying, currentPlayingPosition, autoPlay); localMediaPlayer.play(localMediaPlayer.currentPlaying, currentPlayingPosition, autoPlay);
} }
} }
} }

View File

@ -172,7 +172,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
} }
/* /*
* Set the next playing file. * Set the next playing file. nextToPlay cannot be null
*/ */
@Synchronized @Synchronized
fun setNextPlaying(nextToPlay: DownloadFile) { fun setNextPlaying(nextToPlay: DownloadFile) {
@ -181,6 +181,9 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
nextPlayingTask?.start() nextPlayingTask?.start()
} }
/*
* Clear the next playing file. setIdle controls whether the playerState is affected as well
*/
@Synchronized @Synchronized
fun clearNextPlaying(setIdle: Boolean) { fun clearNextPlaying(setIdle: Boolean) {
nextSetup = false nextSetup = false
@ -202,26 +205,35 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
} }
@Synchronized @Synchronized
fun bufferAndPlay() { private fun bufferAndPlay(fileToPlay: DownloadFile, position: Int, autoStart: Boolean) {
if (playerState !== PlayerState.PREPARED) { if (playerState !== PlayerState.PREPARED) {
reset() reset()
bufferTask = BufferTask(currentPlaying, 0) bufferTask = BufferTask(fileToPlay, position)
bufferTask!!.start() bufferTask!!.start()
} else { } else {
doPlay(currentPlaying, 0, true) doPlay(fileToPlay, position, autoStart)
} }
} }
/*
* Public method to play a given file.
* Optionally specify a position to start at.
*/
@Synchronized @Synchronized
fun play(fileToPlay: DownloadFile?) { @JvmOverloads
fun play(fileToPlay: DownloadFile?, position: Int = 0, autoStart: Boolean = true) {
if (nextPlayingTask != null) { if (nextPlayingTask != null) {
nextPlayingTask!!.cancel() nextPlayingTask!!.cancel()
nextPlayingTask = null nextPlayingTask = null
} }
setCurrentPlaying(fileToPlay) setCurrentPlaying(fileToPlay)
bufferAndPlay()
if (fileToPlay != null) {
bufferAndPlay(fileToPlay, position, autoStart)
}
} }
@Synchronized @Synchronized
fun playNext() { fun playNext() {
if (nextMediaPlayer == null || currentPlaying == null) return if (nextMediaPlayer == null || currentPlaying == null) return
@ -388,18 +400,20 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
} }
@Synchronized @Synchronized
fun doPlay(downloadFile: DownloadFile?, position: Int, start: Boolean) { private fun doPlay(downloadFile: DownloadFile, position: Int, start: Boolean) {
try { try {
downloadFile!!.setPlaying(false) downloadFile.setPlaying(false)
//downloadFile.setPlaying(true);
val file = if (downloadFile.isCompleteFileAvailable) downloadFile.completeFile else downloadFile.partialFile val file = if (downloadFile.isCompleteFileAvailable) downloadFile.completeFile else downloadFile.partialFile
val partial = file == downloadFile.partialFile val partial = file == downloadFile.partialFile
downloadFile.updateModificationDate() downloadFile.updateModificationDate()
mediaPlayer.setOnCompletionListener(null) mediaPlayer.setOnCompletionListener(null)
secondaryProgress = -1 // Ensure seeking in non StreamProxy playback works secondaryProgress = -1 // Ensure seeking in non StreamProxy playback works
mediaPlayer.reset() mediaPlayer.reset()
setPlayerState(PlayerState.IDLE) setPlayerState(PlayerState.IDLE)
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC) mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
var dataSource = file.path var dataSource = file.path
if (partial) { if (partial) {
if (proxy == null) { if (proxy == null) {
@ -417,9 +431,12 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
proxy!!.stop() proxy!!.stop()
proxy = null proxy = null
} }
Timber.i("Preparing media player") Timber.i("Preparing media player")
mediaPlayer.setDataSource(dataSource) mediaPlayer.setDataSource(dataSource)
setPlayerState(PlayerState.PREPARING) setPlayerState(PlayerState.PREPARING)
mediaPlayer.setOnBufferingUpdateListener { mp, percent -> mediaPlayer.setOnBufferingUpdateListener { mp, percent ->
val progressBar = PlayerFragment.getProgressBar() val progressBar = PlayerFragment.getProgressBar()
val song = downloadFile.song val song = downloadFile.song
@ -433,6 +450,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
progressBar.secondaryProgress = secondaryProgress progressBar.secondaryProgress = secondaryProgress
} }
} }
mediaPlayer.setOnPreparedListener { mediaPlayer.setOnPreparedListener {
Timber.i("Media player prepared") Timber.i("Media player prepared")
setPlayerState(PlayerState.PREPARED) setPlayerState(PlayerState.PREPARED)
@ -588,7 +606,9 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
return return
} }
} }
doPlay(downloadFile, position, true) if (downloadFile != null) {
doPlay(downloadFile, position, true)
}
} }
private fun bufferComplete(): Boolean { private fun bufferComplete(): Boolean {