gpt4 book ai didi

android - 媒体播放器在后台

转载 作者:行者123 更新时间:2023-12-02 13:31:37 26 4
gpt4 key购买 nike

这是我拥有的创建音频播放器并允许我的应用程序用户听音乐的代码。当手机进入休眠状态时,如何让音频在后台运行?我想尽可能对下面的代码进行最小的更改。那可能吗?

class AudioActivity : BaseActivity() {

private lateinit var mediaPlayer: MediaPlayer
private lateinit var runnable: Runnable
private var handler: Handler = Handler()
private var pause: Boolean = false
private var working: Boolean = false
private var removeFromLibrary: Boolean = false;


private fun stop () {
try {
if ((mediaPlayer.isPlaying || pause.equals(true))) {
pause = false
seek_bar.setProgress(0)
mediaPlayer.stop()
mediaPlayer.reset()
mediaPlayer.release()
handler.removeCallbacks(runnable)

tv_pass.text = ""
tv_due.text = ""
}
} catch(e: UninitializedPropertyAccessException) {
// media player is not initialized
}
}

override fun onStop() {
super.onStop()

stop()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_audio)

setProgressBar(R.id.progressBar)


// Start the media player
playBtn.setOnClickListener {
if (!working) {
working = true

if (pause && mediaPlayer.isPlaying) {
playBtn.setImageResource(R.drawable.ic_play_circle_filled);
mediaPlayer.pause()
} else {
playBtn.setImageResource(R.drawable.pausebtn);

if (pause) {
mediaPlayer.seekTo(mediaPlayer.currentPosition)
mediaPlayer.start()
} else {
mediaPlayer = MediaPlayer.create(applicationContext, Uri.parse(intent.getStringExtra("audio")))
mediaPlayer.start()
}
initializeSeekBar()

mediaPlayer.setOnCompletionListener {
Toast.makeText(this, "end", Toast.LENGTH_SHORT).show()
}
}

pause = !pause;
working = false
}
}

prevBtn.setOnClickListener {
if (!working) {
working = true
try {
mediaPlayer.seekTo(mediaPlayer.currentPosition + (-10*1000))

} catch(e: UninitializedPropertyAccessException) {
// media player is not initialized
}
working = false
}
}


nextBtn.setOnClickListener {
if (!working) {
working = true
try {
mediaPlayer.seekTo(mediaPlayer.currentPosition + (10*1000))
} catch(e: UninitializedPropertyAccessException) {
// media player is not initialized
}
working = false
}

}

// Seek bar change listener
seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
if (b) {
try {
mediaPlayer.seekTo(i * 1000)
} catch(e: UninitializedPropertyAccessException) {
// media player is not initialized
}
}
}

override fun onStartTrackingTouch(seekBar: SeekBar) {
}

override fun onStopTrackingTouch(seekBar: SeekBar) {
}
})
}

// Method to initialize seek bar and audio stats
private fun initializeSeekBar() {
seek_bar.max = mediaPlayer.seconds

runnable = Runnable {
seek_bar.progress = mediaPlayer.currentSeconds

var minutesaddum = "";
var secondsaddum = ""
val minutes = mediaPlayer.currentSeconds / 60
val seconds = mediaPlayer.currentSeconds % 60

if (minutes < 10) {
minutesaddum = "0"
}
if (seconds < 10) {
secondsaddum = "0"
}
tv_pass.text = "${minutesaddum}${minutes}:${secondsaddum}${seconds}"
val diff = mediaPlayer.seconds - mediaPlayer.currentSeconds


var minutesdiffaddum = "";
var secondsdiffaddum = ""
val minutesdiff = diff / 60
val secondsdiff = diff % 60


if (minutesdiff < 10) {
minutesdiffaddum = "0"
}
if (secondsdiff < 10) {
secondsdiffaddum = "0"
}

tv_due.text = "${minutesdiffaddum}${minutesdiff}:${secondsdiffaddum}${secondsdiff}"

handler.postDelayed(runnable, 1000)
}
handler.postDelayed(runnable, 1000)
}

// Creating an extension property to get the media player time duration in seconds
val MediaPlayer.seconds: Int
get() {
return this.duration / 1000
}

// Creating an extension property to get media player current position in seconds
val MediaPlayer.currentSeconds: Int
get() {
return this.currentPosition / 1000
}

最佳答案

如果您需要创建一个在后台播放的媒体播放器。我建议你先创建一个服务。即使您杀死应用程序,此服务也会让媒体播放器一直播放。

记住也要创建通知。此通知将保持服务作为代码运行: startForeground(0, notification);

来自 Android 的示例:https://developer.android.com/guide/components/services

希望这有帮助!

关于android - 媒体播放器在后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61151243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com