0

I'm currently working on an Android app using the Media3 (ExoPlayer) library to stream video content. I've successfully implemented playback for HTTP streams, but I'm having trouble playing a UDP stream. I am using the following code to set up my ExoPlayer:

package com.itsthe1.onetv

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player

class MainActivity : AppCompatActivity() {

    private lateinit var player: ExoPlayer
    private var playerView: PlayerView? = null
    private val TAG = "MainActivity"

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

        playerView = findViewById(R.id.player_view)
        setupExoPlayer()
    }

    private fun setupExoPlayer() {
        player = ExoPlayer.Builder(this).build().apply {
            playerView?.player = this
            
            // Replace with your UDP stream URI
            val udpUri = Uri.parse("udp://@239.255.0.1:1234") 
            setMediaItem(MediaItem.fromUri(udpUri))

            addListener(object : Player.Listener {
                override fun onPlayerError(error: PlaybackException) {
                    Toast.makeText(this@MainActivity, "Playback error: ${error.message}", Toast.LENGTH_SHORT).show()
                    Log.e(TAG, "Playback error: ${error.message}")
                }
            })

            prepare()
            playWhenReady = true
        }
    }

    override fun onStop() {
        super.onStop()
        player.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        player.release()
    }
}

androidx.media3.exoplayer.ExoPlaybackException: Source error Caused by: androidx.media3.datasource.UdpDataSource$UdpDataSourceException: java.net.SocketTimeoutException: Poll timed out

What I've Tried: Verified the UDP stream URI and tested it with VLC Media Player. Ensured that my Android device has network access to the multicast address. Checked network settings to confirm multicast traffic is allowed. Added necessary permissions in the

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Are there specific configurations or settings needed in ExoPlayer for playing UDP streams?

1 Answer 1

0

I'm facing the same problem,

Probably your stream is MPEG-TS over UDP. Based on this code (for ExoPlayer media2) : ExoPlayer2UdpDemo

I have tried to convert the code to Media3. This is what I have done so far without success:

player = ExoPlayer.Builder(this)
.build()
.apply {

    val factory =
        DataSource.Factory { UdpDataSource(3000, 100000) }
        
    val tsExtractorFactory = ExtractorsFactory {
        arrayOf(
            TsExtractor(TsExtractor.MODE_SINGLE_PMT,
                TimestampAdjuster(0), DefaultTsPayloadReaderFactory()
            )
        )
    }

    val mediaSource: MediaSource = ProgressiveMediaSource.Factory(factory,tsExtractorFactory)
        .createMediaSource(MediaItem.fromUri(Uri.parse("udp://@192.168.11.111:1261")))

    setMediaSource(mediaSource)
    prepare()
    playWhenReady = true
}

val playerView = findViewById<View>(R.id.player_view) as PlayerView
playerView.player = player

I'm not sure if ProgressiveMediaSource is the correct way for this type of stream

Not the answer you're looking for? Browse other questions tagged or ask your own question.