gpt4 book ai didi

android - RTSP 实时视频流

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:03 26 4
gpt4 key购买 nike

我正在构建一个应用程序,用于将视频从树莓派流式传输到我的 Android 设备。我有三种播放此流的功能方式:

rtsp://media.smart-streaming.com/mytest/mp4:sample_phone_150k.mp4

  1. 媒体播放器 + SurfaceView
  2. 视频 View
  3. Intent.ACTION_VIEW

问题是应用程序播放上述 ^ 流但不播放我的 (rtsp://192.168.1.143:8554/vid.mp4) ... 但 VLC 播放

我尝试过使用 VLC 和 LIVE555 以多种视频格式进行流式传输,我还尝试播放手机上录制的视频。

这是我的代码:

//Stream methods 0 = MediaPlayer & SurfaceView, 1 = VideoView, 2 = Native Video Player
final int STREAM_USING = 0;

//MediaPlayer on surfaceView
String streamPath = "rtsp://192.168.1.143:8554/vid.mp4";//"rtsp://media.smart-streaming.com/mytest/mp4:sample_phone_150k.mp4";//"rtsp://192.168.1.143:8554/vid.mp4";//;"rtp://239.255.0.1:5004/";
Uri streamUri;
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;

//VideoView
VideoView videoView;

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

//MediaPlayer
switch (STREAM_USING) {
case 0: {
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFixedSize(800, 480);
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
mediaPlayer.release();
mediaPlayer = new MediaPlayer();
//mediaPlayer.stop();
play();
}
return false;
}
});

mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Toast.makeText(getApplicationContext(), "BUFF : " + percent, Toast.LENGTH_SHORT).show();
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
});

//Intent i = new Intent(Intent.ACTION_GET_CONTENT);
//i.setType("video/*");
//startActivityForResult(i, 1234);
streamUri = Uri.parse(streamPath);
play();
break;
}
case 1: {
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse(streamPath));
MediaController mediaController = new MediaController(this);
//mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
try {
videoView.start();
}
catch (SecurityException se) {
Log.e("SE", se.getMessage());
se.printStackTrace();
}
break;
}
case 2: {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(streamPath));
startActivity(intent);
break;
}
default: {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(streamPath));
startActivity(intent);
}
}
}

private void play() {
try {
//final FileInputStream fis = new FileInputStream(streamPath);
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.setDataSource(MainActivity.this, streamUri);
//mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//mediaPlayer.reset();
mediaPlayer.start();
}
});


} catch (SecurityException se) {
Log.e("SE", se.getMessage());
se.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}

感谢任何帮助,我已经尝试了一个星期但没有成功:(

最佳答案

您已经添加了两种方法,只是我想添加位修改。我已经完成了流式 ralalted 任务。我已经将 rtsp 与 wowza 一起使用。我会让你知道的几种方法

尝试一次,如果您遇到问题请告诉我。如果您在 vlc 中的流式传输工作正常,那么应用端有问题。如果这些方法不起作用,请尝试使用其他手机。它还可以帮助您。

<强>1。试试 videoview

vvVideoPlay = (VideoView) findViewById(R.id.vvVideoPlay);
MediaController mediaController = new MediaController(this);

String videoUrl = "rtsp://192.168.1.143:8554/vid.mp4";

mediaController.setAnchorView(vvVideoPlay);
Uri uri = Uri.parse(videoUrl);
vvVideoPlay.setVideoURI(uri);
vvVideoPlay.setMediaController(mediaController);
vvVideoPlay.requestFocus();

vvVideoPlay.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {

mp.start();
pdialog.dismiss();
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {

@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

mp.start();
}
});
}
});

<强>2。直接用手机播放器试试

startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("rtsp://192.168.1.143:8554/vid.mp4")));

<强>3。第三种方法尝试在您的应用中使用此库和自定义播放器。

Step1.将它添加到你的gradle

compile "fm.jiecao:jiecaovideoplayer:4.7.0"

第 2 步。将其添加为 xml 布局中的视频播放。

<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="@+id/videoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent" />

第 3 步。从这里检查如何在您的类里面使用这个库,

public class PlayVideoActivity extends BaseActivity {

@BindView(R.id.videoPlayer)
JCVideoPlayerStandard mVideoPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
restoreFromIntent(getIntent());
}

@Override
public int getLayout() {
return R.layout.activity_play_video;
}


// get video path from intent and play the video here
private void restoreFromIntent(Intent intent) {

mVideoPlayer.setUp("rtsp://192.168.1.143:8554/vid.mp4"
, JCVideoPlayerStandard.SCREEN_LAYOUT_LIST, "");
}

@Override
public void onBackPressed() {
if (JCVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}

@Override
protected void onPause() {
super.onPause();
JCVideoPlayer.releaseAllVideos();
}
}

希望这能帮助您解决问题。谢谢

关于android - RTSP 实时视频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com