Kotlin - Cannot stream online radio links using MediaPlayer

Viewed 366

I decided to learn Kotlin by developing a simple online radio streaming app. I've learnt how to implement MediaPlayer into my app, and if I pass a link for an .mp3 file, it works and successfully streams the file. However, nothing happens when I pass a link to an online radio. Here's an example of my code:

class RadioFragment : Fragment() {

private lateinit var mp: MediaPlayer

val url = "http://listen.shoutcast.com/freshsnd"


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view =inflater.inflate(R.layout.fragment_radio, container, false)

    view.audioStreamBtn.setOnClickListener{

        val mediaPlayer: MediaPlayer? = MediaPlayer().apply {
            setAudioStreamType(AudioManager.STREAM_MUSIC)
            setDataSource(url)
            prepareAsync() // might take long! (for buffering, etc)
            setOnPreparedListener{ mp -> mp.start()}
        }

    }

    return view
}

And I get this in the logcat:

2020-05-11 11:56:01.622 27441-27441/com.android.slowfy W/MediaPlayer: Use of stream types is deprecated for operations other than volume control
2020-05-11 11:56:01.623 27441-27441/com.android.slowfy W/MediaPlayer: See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case
2020-05-11 11:56:01.628 27441-27441/com.android.slowfy V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@df75339): Cookies: null
2020-05-11 11:56:01.641 27441-27464/com.android.slowfy V/MediaHTTPService: makeHTTPConnection: CookieManager created: java.net.CookieManager@4bffd7e
2020-05-11 11:56:01.642 27441-27464/com.android.slowfy V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@df75339): cookieHandler: java.net.CookieManager@4bffd7e Cookies: null
2020-05-11 11:56:01.657 27441-27459/com.android.slowfy I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
2020-05-11 11:56:01.669 27441-27459/com.android.slowfy D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-11 11:56:03.531 27441-27510/com.android.slowfy I/.android.slowf: ProcessProfilingInfo new_methods=1484 is saved saved_to_disk=1 resolve_classes_delay=8000
2020-05-11 11:56:31.883 27441-27459/com.android.slowfy E/MediaPlayerNative: error (1, -2147483648)
2020-05-11 11:56:31.884 27441-27441/com.android.slowfy E/MediaPlayer: Error (1,-2147483648)
2020-05-11 11:57:06.693 27441-27455/com.android.slowfy W/MediaPlayer-JNI: MediaPlayer finalized without being released
2020-05-11 11:58:03.990 27441-27724/com.android.slowfy W/DpmTcmClient: Couldn't find 'tcm' socket after 120times. quit trying

I've looked through some errors on the E/MediaPlayer: Error (1,-2147483648) error but I've got no idea on how to implement a solution on my code. It's weird because about one year ago I did the same exact app on Java and it worked great.

Any thoughts on this?

0 Answers
Related