// Create a player instance.valplayer=ExoPlayer.Builder(context).build()// Set the media item to be played.player.setMediaItem(MediaItem.fromUri(rtspUri))// Prepare the player.player.prepare()
Java
// Create a player instance.ExoPlayerplayer=newExoPlayer.Builder(context).build();// Set the media item to be played.player.setMediaItem(MediaItem.fromUri(rtspUri));// Prepare the player.player.prepare();
// Create an RTSP media source pointing to an RTSP uri.valmediaSource:MediaSource=RtspMediaSource.Factory().createMediaSource(MediaItem.fromUri(rtspUri))// Create a player instance.valplayer=ExoPlayer.Builder(context).build()// Set the media source to be played.player.setMediaSource(mediaSource)// Prepare the player.player.prepare()
Java
// Create an RTSP media source pointing to an RTSP uri.MediaSourcemediaSource=newRtspMediaSource.Factory().createMediaSource(MediaItem.fromUri(rtspUri));// Create a player instance.ExoPlayerplayer=newExoPlayer.Builder(context).build();// Set the media source to be played.player.setMediaSource(mediaSource);// Prepare the player.player.prepare();
// Create an RTSP media source pointing to an RTSP uri and override the socket// factory.valmediaSource:MediaSource=RtspMediaSource.Factory().setSocketFactory(...).createMediaSource(MediaItem.fromUri(rtspUri))
Java
// Create an RTSP media source pointing to an RTSP uri and override the socket// factory.MediaSourcemediaSource=newRtspMediaSource.Factory().setSocketFactory(...).createMediaSource(MediaItem.fromUri(rtspUri));
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["# RTSP\n\nExoPlayer supports both live and on demand RTSP. Supported sample formats and\nnetwork types are listed below.\n\n### Supported sample formats\n\n- H264 (the SDP media description must include SPS/PPS data in the fmtp attribute for decoder initialization).\n- AAC (with ADTS bitstream).\n- AC3.\n\n| **Note:** Please comment on [this issue](https://github.com/google/ExoPlayer/issues/9210) to request support for additional sample formats.\n\n### Supported network types\n\n- RTP over UDP unicast (multicast is not supported).\n- Interleaved RTSP, RTP over RTSP using TCP.\n\nUsing MediaItem\n---------------\n\nTo play an RTSP stream, you need to depend on the RTSP module. \n\n### Kotlin\n\n```kotlin\nimplementation(\"androidx.media3:media3-exoplayer-rtsp:1.8.0\")\n```\n\n### Groovy\n\n```groovy\nimplementation \"androidx.media3:media3-exoplayer-rtsp:1.8.0\"\n```\n\nYou can then create a `MediaItem` for an RTSP URI and pass it to the player. \n\n### Kotlin\n\n```kotlin\n// Create a player instance.\nval player = ExoPlayer.Builder(context).build()\n// Set the media item to be played.\nplayer.setMediaItem(MediaItem.fromUri(rtspUri))\n// Prepare the player.\nplayer.prepare()\n```\n\n### Java\n\n```java\n// Create a player instance.\nExoPlayer player = new ExoPlayer.Builder(context).build();\n// Set the media item to be played.\nplayer.setMediaItem(MediaItem.fromUri(rtspUri));\n// Prepare the player.\nplayer.prepare();\n```\n\n\u003cbr /\u003e\n\n### Authentication\n\nExoPlayer supports playback with RTSP BASIC and DIGEST authentication. To play\nprotected RTSP content, the `MediaItem`'s URI must be configured with the\nauthentication info. Specifically, the URI should be of the form\n`rtsp://\u003cusername\u003e:\u003cpassword\u003e@\u003chost address\u003e`.\n\nUsing RtspMediaSource\n---------------------\n\nFor more customization options, you can create an `RtspMediaSource` and pass it\ndirectly to the player instead of a `MediaItem`. \n\n### Kotlin\n\n```kotlin\n// Create an RTSP media source pointing to an RTSP uri.\nval mediaSource: MediaSource =\nRtspMediaSource.Factory().createMediaSource(MediaItem.fromUri(rtspUri))\n// Create a player instance.\nval player = ExoPlayer.Builder(context).build()\n// Set the media source to be played.\nplayer.setMediaSource(mediaSource)\n// Prepare the player.\nplayer.prepare()\n```\n\n### Java\n\n```java\n// Create an RTSP media source pointing to an RTSP uri.\nMediaSource mediaSource =\n new RtspMediaSource.Factory().createMediaSource(MediaItem.fromUri(rtspUri));\n// Create a player instance.\nExoPlayer player = new ExoPlayer.Builder(context).build();\n// Set the media source to be played.\nplayer.setMediaSource(mediaSource);\n// Prepare the player.\nplayer.prepare();\n```\n\n\u003cbr /\u003e\n\nUsing RTSP behind a NAT (RTP/TCP support)\n-----------------------------------------\n\nExoPlayer uses UDP as the default protocol for RTP transport.\n\nWhen streaming RTSP behind a NAT layer, the NAT might not be able to forward the\nincoming RTP/UDP packets to the device. This occurs if the NAT lacks the\nnecessary UDP port mapping. If ExoPlayer detects there have not been incoming\nRTP packets for a while and the playback has not started yet, ExoPlayer tears\ndown the current RTSP playback session, and retries playback using RTP-over-RTSP\n(transmitting RTP packets using the TCP connection opened for RTSP).\n\nThe timeout for retrying with TCP can be customized by calling the method\n`RtspMediaSource.Factory.setTimeoutMs()`. For example, if the timeout is set to\nfour seconds, the player will retry with TCP after four seconds of UDP\ninactivity.\n\nSetting the timeout also affects the end-of-stream detection logic. That is,\nExoPlayer will report the playback has ended if nothing is received for the\nduration of the set timeout. Setting this value too small may lead to an early\nend-of-stream signal under poor network conditions.\n\nRTP/TCP offers better compatibility under some network setups. You can configure\nExoPlayer to use RTP/TCP by default with\n`RtspMediaSource.Factory.setForceUseRtpTcp()`.\n\n### Passing a custom SocketFactory\n\nCustom `SocketFactory` instances can be useful when particular routing is\nrequired (for example, when RTSP traffic needs to pass a specific interface, or the\nsocket needs additional connectivity flags).\n\nBy default, `RtspMediaSource` will use Java's standard socket factory\n(`SocketFactory.getDefault()`) to create connections to the remote endpoints.\nThis behavior can be overridden using\n`RtspMediaSource.Factory.setSocketFactory()`. \n\n### Kotlin\n\n```kotlin\n// Create an RTSP media source pointing to an RTSP uri and override the socket\n// factory.\nval mediaSource: MediaSource =\nRtspMediaSource.Factory()\n .setSocketFactory(...)\n .createMediaSource(MediaItem.fromUri(rtspUri))\n```\n\n### Java\n\n```java\n// Create an RTSP media source pointing to an RTSP uri and override the socket\n// factory.\nMediaSource mediaSource =\n new RtspMediaSource.Factory()\n .setSocketFactory(...)\n .createMediaSource(MediaItem.fromUri(rtspUri));\n```\n\n\u003cbr /\u003e"]]