RTSP直播视频流

时间:2016-04-30 08:43:04

标签: android raspberry-pi

我正在构建一个应用程序来实现从覆盆子pi到我的Android设备的直播视频。我有三种功能方式来播放这个流:

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

  1. Media Player + SurfaceView
  2. VideoView
  3. Intent.ACTION_VIEW
  4. 问题是应用程序播放上面的^流但不播放我的(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();
        }
    }
    

    任何帮助都表示赞赏,我已经尝试了一个星期才能让这项工作取得成功:(

1 个答案:

答案 0 :(得分:5)

您已经添加了两种方法,我想添加位修改。我已经完成了流媒体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"

Step2。将其添加为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();
}
}

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