You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中实现城市公共IP摄像头实时视频播放?

How to Play Live Public IP Camera Streams in Android (Instead of Just MP4 Files)

Hey John, the core issue here is that Android's native MediaPlayer has very limited support for live streaming protocols—most public IP cameras don’t serve static MP4 files, but instead use streaming standards like RTSP, HLS, or RTMP. Let’s walk through the best solutions to get your app working with these live feeds.

First: Identify the Camera's Streaming Protocol

Before writing code, you need to confirm what protocol the public camera uses:

  • Many city public cameras use RTSP (starts with rtsp://)
  • Some use HLS (ends with .m3u8)
  • A smaller number use RTMP

You can usually find this info by searching for the camera’s public stream URL, or test the URL in VLC first to confirm it plays live video.

ExoPlayer is built specifically for streaming media and supports all the common live protocols that MediaPlayer struggles with. Here’s how to implement it:

Step 1: Add Dependencies to build.gradle (Module level)

dependencies {
    // ExoPlayer core library
    implementation "com.google.android.exoplayer:exoplayer-core:2.19.1"
    // Extensions for specific protocols (RTSP, HLS)
    implementation "com.google.android.exoplayer:exoplayer-rtsp:2.19.1"
    implementation "com.google.android.exoplayer:exoplayer-hls:2.19.1"
}

Step 2: Add a Player View to Your Layout

In your activity_main.xml:

<com.google.android.exoplayer2.ui.StyledPlayerView
    android:id="@+id/player_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Step 3: Implement the Player Logic in MainActivity

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.ui.StyledPlayerView;

public class MainActivity extends AppCompatActivity {

    private ExoPlayer player;
    private StyledPlayerView playerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        playerView = findViewById(R.id.player_view);

        // Initialize ExoPlayer
        player = new ExoPlayer.Builder(this).build();
        playerView.setPlayer(player);

        // Replace this with your target camera's live stream URL
        String cameraStreamUrl = "rtsp://example.com/rome-public-camera-feed";
        MediaItem mediaItem = MediaItem.fromUri(cameraStreamUrl);
        
        // Load and start playback
        player.setMediaItem(mediaItem);
        player.prepare();
        player.play();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Release player resources to avoid memory leaks
        player.release();
    }
}

Step 4: Add Required Permissions

Add these to your AndroidManifest.xml:

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

Solution 2: Handle Edge Cases & Additional Tips

  • Camera Authentication: Some public cameras require username/password access. For RTSP streams, add credentials directly to the URL like rtsp://user:pass@camera-url.com, or use ExoPlayer’s DefaultHttpDataSource.Factory to set authentication headers.
  • Error Handling: Live streams can be unstable—add a listener to catch playback errors and implement retry logic:
    player.addListener(new Player.Listener() {
        @Override
        public void onPlayerError(PlaybackException error) {
            // Handle error (e.g., show retry button)
        }
    });
    
  • Older Android Versions: ExoPlayer supports devices back to API 16, but some older hardware may have decoding limitations for certain protocols. Test on target devices if possible.

内容的提问来源于stack exchange,提问作者John Born

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:49:24