简体   繁体   中英

Android: cannot play a song - java.lang.UnsupportedOperationException: Unknown or unsupported URL

I'm trying to play a song which I put in Music folder on SD card. But when I call mediaPlayer?.setDataSource(this@MusicPlayerActivity, uri) I get the exception: "java.lang.UnsupportedOperationException: Unknown or unsupported URL: content://media/external/audio/media/samurai.mp3"

I get an URI in the readSongs() function. Is there any trouble with the URI? If there is, how to get the right URI?

Thanks.

Code snippet

class MusicPlayerActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMusicPlayerBinding
    private lateinit var musicList: MutableList<Music>
    private lateinit var linearLayoutManager: LinearLayoutManager
    private lateinit var adapter: MusicAdapter
    private var mediaPlayer: MediaPlayer? = null
    private var currentTrackPosition: Int = 0
    private var isPlaying: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMusicPlayerBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        musicList = mutableListOf()

        if (Build.VERSION.SDK_INT >= 23) {
            checkPermissions()
        }

        binding.playFab.setOnClickListener {
            play(currentTrackPosition)
        }
    }

    private fun play(currentTrackPosition: Int) {
        if (!isPlaying) {
            isPlaying = true
            mediaPlayer = MediaPlayer()
            mediaPlayer?.setAudioStreamType(AudioManager.STREAM_MUSIC)
            try {
                val uri = Uri.parse(musicList[currentTrackPosition].songUri)
                mediaPlayer?.setDataSource(this@MusicPlayerActivity, uri)
                mediaPlayer?.prepare()
                mediaPlayer?.start()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    private fun readSongs() {
        val selection = MediaStore.Audio.Media.IS_MUSIC
        val projection = arrayOf(
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DISPLAY_NAME
        )
        val uriExternal = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

        val cursor: Cursor? = contentResolver.query(
            uriExternal,
            projection,
            selection,
            null, null
        )

        while (cursor!!.moveToNext()) {
            musicList.add(
                Music(
                    cursor.getString(0),
                    cursor.getString(1),
                    Uri.withAppendedPath(uriExternal, cursor.getString(2)).toString()
                )
            )
        }

        cursor.close()

        linearLayoutManager = LinearLayoutManager(this)
        adapter = MusicAdapter(musicList)

        binding.recyclerView.layoutManager = linearLayoutManager
        binding.recyclerView.adapter = adapter
    }

I replaced MediaStore.Audio.Media.DISPLAY_NAME with MediaStore.Audio.Media._ID so URI becomes "content://media/external/audio/media/42" and now it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM