Fix errors and warnings

This commit is contained in:
tzugen 2021-03-24 12:59:07 +01:00
parent 2260cc311f
commit a467abf10b
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
1 changed files with 13 additions and 10 deletions

View File

@ -26,6 +26,8 @@ import timber.log.Timber
import java.io.File import java.io.File
import java.net.URLEncoder import java.net.URLEncoder
import java.util.* import java.util.*
import kotlin.math.abs
import kotlin.math.max
/** /**
* Represents a Media Player which uses the mobile's resources for playback * Represents a Media Player which uses the mobile's resources for playback
@ -50,7 +52,8 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
private var nextPlayerState = PlayerState.IDLE private var nextPlayerState = PlayerState.IDLE
private var nextSetup = false private var nextSetup = false
private var nextPlayingTask: CancellableTask? = null private var nextPlayingTask: CancellableTask? = null
private var wakeLock: WakeLock? = null private val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
private val wakeLock: WakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.javaClass.name)
private var mediaPlayer: MediaPlayer? = null private var mediaPlayer: MediaPlayer? = null
private var nextMediaPlayer: MediaPlayer? = null private var nextMediaPlayer: MediaPlayer? = null
private var mediaPlayerLooper: Looper? = null private var mediaPlayerLooper: Looper? = null
@ -62,6 +65,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
private var bufferTask: CancellableTask? = null private var bufferTask: CancellableTask? = null
private var positionCache: PositionCache? = null private var positionCache: PositionCache? = null
private var secondaryProgress = -1 private var secondaryProgress = -1
fun onCreate() { fun onCreate() {
if (mediaPlayer != null) { if (mediaPlayer != null) {
mediaPlayer!!.release() mediaPlayer!!.release()
@ -93,8 +97,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
EqualizerController.create(context, mediaPlayer) EqualizerController.create(context, mediaPlayer)
VisualizerController.create(mediaPlayer) VisualizerController.create(mediaPlayer)
}.start() }.start()
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.javaClass.name)
wakeLock.setReferenceCounted(false) wakeLock.setReferenceCounted(false)
audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
Util.registerMediaButtonEventReceiver(context, true) Util.registerMediaButtonEventReceiver(context, true)
@ -125,7 +128,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
audioManager!!.unregisterRemoteControlClient(remoteControlClient) audioManager!!.unregisterRemoteControlClient(remoteControlClient)
clearRemoteControl() clearRemoteControl()
Util.unregisterMediaButtonEventReceiver(context, true) Util.unregisterMediaButtonEventReceiver(context, true)
wakeLock!!.release() wakeLock.release()
} catch (exception: Throwable) { } catch (exception: Throwable) {
Timber.w(exception, "LocalMediaPlayer onDestroy exception: ") Timber.w(exception, "LocalMediaPlayer onDestroy exception: ")
} }
@ -178,7 +181,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
} }
nextPlaying = nextToPlay nextPlaying = nextToPlay
nextPlayingTask = CheckCompletionTask(nextPlaying) nextPlayingTask = CheckCompletionTask(nextPlaying)
nextPlayingTask.start() nextPlayingTask?.start()
} }
@Synchronized @Synchronized
@ -202,7 +205,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
if (playerState !== PlayerState.PREPARED) { if (playerState !== PlayerState.PREPARED) {
reset() reset()
bufferTask = BufferTask(currentPlaying, 0) bufferTask = BufferTask(currentPlaying, 0)
bufferTask.start() bufferTask!!.start()
} else { } else {
doPlay(currentPlaying, 0, true) doPlay(currentPlaying, 0, true)
} }
@ -518,10 +521,10 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
// Acquire a temporary wakelock, since when we return from // Acquire a temporary wakelock, since when we return from
// this callback the MediaPlayer will release its wakelock // this callback the MediaPlayer will release its wakelock
// and allow the device to go to sleep. // and allow the device to go to sleep.
wakeLock!!.acquire(60000) wakeLock.acquire(60000)
val pos = cachedPosition val pos = cachedPosition
Timber.i("Ending position %d of %d", pos, duration) Timber.i("Ending position %d of %d", pos, duration)
if (!isPartial || downloadFile.isWorkDone && Math.abs(duration - pos) < 1000) { if (!isPartial || downloadFile.isWorkDone && abs(duration - pos) < 1000) {
setPlayerState(PlayerState.COMPLETED) setPlayerState(PlayerState.COMPLETED)
if (Util.getGaplessPlaybackPreference(context) && nextPlaying != null && nextPlayerState === PlayerState.PREPARED) { if (Util.getGaplessPlaybackPreference(context) && nextPlaying != null && nextPlayerState === PlayerState.PREPARED) {
if (nextSetup) { if (nextSetup) {
@ -549,7 +552,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
Timber.i("Requesting restart from %d of %d", pos, duration) Timber.i("Requesting restart from %d of %d", pos, duration)
reset() reset()
bufferTask = BufferTask(downloadFile, pos) bufferTask = BufferTask(downloadFile, pos)
bufferTask.start() bufferTask!!.start()
} }
} }
} }
@ -606,7 +609,7 @@ class LocalMediaPlayer(private val audioFocusHandler: AudioFocusHandler, private
// Calculate roughly how many bytes BUFFER_LENGTH_SECONDS corresponds to. // Calculate roughly how many bytes BUFFER_LENGTH_SECONDS corresponds to.
val bitRate = downloadFile.bitRate val bitRate = downloadFile.bitRate
val byteCount = Math.max(100000, bitRate * 1024L / 8L * bufferLength) val byteCount = max(100000, bitRate * 1024L / 8L * bufferLength)
// Find out how large the file should grow before resuming playback. // Find out how large the file should grow before resuming playback.
Timber.i("Buffering from position %d and bitrate %d", position, bitRate) Timber.i("Buffering from position %d and bitrate %d", position, bitRate)