将流媒体的URL链接播放到媒体播放器?

huangapple go评论75阅读模式
英文:

How to play streamed URL link to mediaPlayer?

问题

mediaPlayer 播放不了流媒体收音机链接。

我的主要目标是使我的 Android 应用能够播放来自 radio-browser.info 的收音机 URL 链接。我找到的大多数答案只是使用常规的 .mp3 链接,而不是流媒体音频。此外,我相信我找到的大多数答案已过时,所以代码无法正常工作。

这是我的代码:

class MainActivity : AppCompatActivity() {
    private lateinit var testingButton: Button
    var mediaPlayer: MediaPlayer? = null

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

        testingButton = findViewById(R.id.testingButton)

        testingButton.setOnClickListener {
            playAudio()
        }
    }

    private fun playAudio() {
        mediaPlayer = MediaPlayer()
        try {
            mediaPlayer!!.setDataSource("http://play.sas-media.ru/play_256")
            mediaPlayer!!.prepareAsync()
            mediaPlayer!!.start()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        Toast.makeText(this, "音频开始播放", Toast.LENGTH_LONG).show()
    }
}

在按下按钮后,应用程序不会崩溃,它会显示“音频已开始播放”,但实际上没有播放任何内容。我觉得我漏掉了什么。

编辑:
按下按钮后的 Logcat 错误:
MediaPlayerNative 错误 (1, -2147483648)
MediaPlayer 错误 (1, -2147483648)

如果在出现错误后再次按下按钮:
MediaPlayer 错误 (-38,0)

英文:

mediaPlayer won't play streamed radio link

my main goal is to make it so my android app can play a radio url link from radio-browser.info.
Most of the answers i find are just using a regular .mp3 link, not a streamed audio. On top of that, i believe most answers i find are outdated so the code doesn't quite work.

This is what i have:

class MainActivity : AppCompatActivity() {
    private lateinit var testingButton : Button
    var mediaPlayer : MediaPlayer? = null

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

        testingButton = findViewById(R.id.testingButton)

        testingButton.setOnClickListener{
            playAudio()
        }
    }

    private fun playAudio() {
        mediaPlayer = MediaPlayer()
        try {
            mediaPlayer!!.setDataSource("http://play.sas-media.ru/play_256")
            mediaPlayer!!.prepareAsync()
            mediaPlayer!!.start()
        }catch (e: IOException) {
            e.printStackTrace()
        }
        Toast.makeText(this,"Audio started playing",Toast.LENGTH_LONG).show()
    }

The app doesn't crash upon button press, it says "Audio Started playing" but actually plays nothing.
I feel like i'm missing something.

Edit:
Logcat errors upon button press:
MediaPlayerNative error (1, -2147483648)
MediaPlayer error (1, -2147483648)

and if i press the button again after that error

MediaPlayer error (-38,0)

答案1

得分: 0

我认为你的playAudio()方法中缺少下面的代码:

    mediaPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)

setAudioStreamType已经被弃用,更新的代码如下:

        private fun playAudio() {
        val audioAttributes = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build()
        GlobalScope.launch(Dispatchers.Main) {
            try {
                val mediaPlayer = MediaPlayer()
                withContext(Dispatchers.IO) {
                    mediaPlayer.setDataSource("https://play.sas-media.ru/play_256")
                    mediaPlayer.setAudioAttributes(audioAttributes)
                    mediaPlayer.prepare()
                }
                mediaPlayer.start()
                Toast.makeText(this@MainActivity, "Audio started playing", Toast.LENGTH_LONG).show()
            } catch (e: IOException) {
                e.printStackTrace()
                Toast.makeText(this@MainActivity, "Failed to play audio", Toast.LENGTH_LONG).show()
            }
        }
    }
英文:

I think you are missing the below code in your playAudio() method

mediaPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)

setAudioStreamType is deprecated updated code-

    private fun playAudio() {
    val audioAttributes = AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        .build()
    GlobalScope.launch(Dispatchers.Main) {
        try {
            val mediaPlayer = MediaPlayer()
            withContext(Dispatchers.IO) {
                mediaPlayer.setDataSource("https://play.sas-media.ru/play_256")
                mediaPlayer.setAudioAttributes(audioAttributes)
                mediaPlayer.prepare()
            }
            mediaPlayer.start()
            Toast.makeText(this@MainActivity, "Audio started playing", Toast.LENGTH_LONG).show()
        } catch (e: IOException) {
            e.printStackTrace()
            Toast.makeText(this@MainActivity, "Failed to play audio", Toast.LENGTH_LONG).show()
        }
    }
}

huangapple
  • 本文由 发表于 2023年5月25日 22:22:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定