Я не могу одновременно воспроизводить потоковое видео по ссылке rtsp

#android #video

#Android #Видео

Вопрос:

Я хочу одновременно воспроизводить 4 элемента видеообзора по URL потока rtsp, чтобы он воспроизводил 1 элемент, оставшийся не работающим, но если запустить в sumsung node2, можно воспроизвести 3 элемента, оставшиеся не работающими. Как сделать, чтобы для просмотра видео все 4 элемента работали одновременно.

Мой код

      import android.app.Activity;
     import android.media.MediaPlayer;
     import android.media.MediaPlayer.OnPreparedListener;
     import android.net.Uri;
     import android.os.Bundle;
     import android.view.Window;
     import android.widget.MediaController;
     import android.widget.VideoView;

     public class MainActivity extends Activity {

String SrcPath = "rtsp://monitor:monitor@192.168.60.101/MPEG4/ch1/sub/av_stream";
String SrcPath1 = "rtsp://monitor:monitor@192.168.60.102/MPEG4/ch2/sub/av_stream";
String SrcPath2 = "rtsp://monitor:monitor@192.168.60.101/MPEG4/ch3/sub/av_stream";
String SrcPath3 = "rtsp://monitor:monitor@192.168.60.101/MPEG4/ch4/sub/av_stream";

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

    VideoView myVideoView1 = (VideoView) findViewById(R.id.videoView1);
    VideoView myVideoView2 = (VideoView) findViewById(R.id.videoView2);
    VideoView myVideoView3 = (VideoView) findViewById(R.id.videoView3);
    VideoView myVideoView4 = (VideoView) findViewById(R.id.videoView4);


    prepareVideo(myVideoView1, SrcPath);
    prepareVideo(myVideoView2, SrcPath1);
    prepareVideo(myVideoView3, SrcPath2);
    prepareVideo(myVideoView4, SrcPath3);

}

public void prepareVideo(final VideoView videoView, String url) {
    try {


        MediaController mediacontroller = new MediaController(this);
        mediacontroller.setAnchorView(videoView);
        videoView.setMediaController(mediacontroller);
        videoView.setVideoURI(Uri.parse(url));


        videoView.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer player) {

                videoView.requestFocus();
                videoView.start();

            }
        });

        //

    } catch (Exception e) {
        System.out.println("Exception caught: "   e.getMessage());
    }
   }

 }
  

XML

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.androidvideoview.MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <RelativeLayout
        android:id="@ id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <VideoView
            android:id="@ id/videoView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@ id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <VideoView
            android:id="@ id/videoView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <RelativeLayout
        android:id="@ id/RelativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <VideoView
            android:id="@ id/videoView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@ id/RelativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <VideoView
            android:id="@ id/videoView4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1" />
    </RelativeLayout>
</LinearLayout>
  

Ответ №1:

Просто введите этот код и вызовите метод showVideos:

 private static final int[] SURFACE_RES_IDS = { R.id.videoView1,
        R.id.videoView2, R.id.videoView3, R.id.videoView4 };

private SurfaceHolder[] mSurfaceHolders = new SurfaceHolder[SURFACE_RES_IDS.length];    

private MediaPlayer[] mMediaPlayers = new MediaPlayer[SURFACE_RES_IDS.length];

private SurfaceView[] mSurfaceViews = new SurfaceView[SURFACE_RES_IDS.length];
public void showVideos(){
for(int i=0;i<4;i  ){
    mSurfaceViews[i] = (SurfaceView) findViewById(SURFACE_RES_IDS[i]);
    mSurfaceViews[i].setId(i);
    mSurfaceHolders[i] = mSurfaceViews[i].getHolder();
    mSurfaceHolders[i].addCallback(this);
    mSurfaceViews[i].setOnTouchListener(this);
    }
}
public void onBufferingUpdate(MediaPlayer player, int percent) {
    Log.d(TAG, "MediaPlayer("   indexOf(player)
              "): onBufferingUpdate percent: "   percent);
}

public void onCompletion(MediaPlayer player) {
    Log.d(TAG, "MediaPlayer("   indexOf(player)   "): onCompletion called");
}

public void onVideoSizeChanged(MediaPlayer player, int width, int height) {
    Log.v(TAG, "MediaPlayer("   indexOf(player)
              "): onVideoSizeChanged called");
    if (width == 0 || height == 0) {
        Log.e(TAG, "invalid video width("   width   ") or height("   height
                  ")");
        return;
    }

    int index = indexOf(player);
    if (index == -1)
        return; // sanity check; should never happen
    mSizeKnown[index] = true;
    if (mVideoReady[index] amp;amp; mSizeKnown[index]) {
        startVideoPlayback(player);
    }
}

public void onPrepared(MediaPlayer player) {
    Log.d(TAG, "MediaPlayer("   indexOf(player)   "): onPrepared called");

    int index = indexOf(player);
    if (index == -1)
        return; // sanity check; should never happen
    mVideoReady[index] = true;
    if (mVideoReady[index] amp;amp; mSizeKnown[index]) {
        startVideoPlayback(player);
    }
}

public void surfaceChanged(SurfaceHolder holder, int i, int j, int k) {
    Log.d(TAG, "SurfaceHolder("   indexOf(holder)
              "): surfaceChanged called");
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "SurfaceHolder("   indexOf(holder)
              "): surfaceDestroyed called");
}
public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "SurfaceHolder("   indexOf(holder)
              "): surfaceCreated called");
if (index == -1)
        return; // should never happen
    try {
        mMediaPlayers[index] = new MediaPlayer();
Uri videourl  = Uri.parse("rtsp://monitor:monitor@192.168.60.101/MPEG4/ch1/sub/av_stream");
mMediaPlayers[index]
                .setDataSource(this, videourl);
        mMediaPlayers[index].setDisplay(mSurfaceHolders[index]);

        try {
            mMediaPlayers[index].prepare();
        } catch (IOException e) {
            if (mMediaPlayers[index] != null) {
                mMediaPlayers[index].release();
                mMediaPlayers[index] = null;
            }
            // releaseMediaPlayers();
        }

        mMediaPlayers[index].setOnBufferingUpdateListener(this);
        mMediaPlayers[index].setOnCompletionListener(this);

        mMediaPlayers[index]
                .setOnPreparedListener(new OnPreparedListener() {

                    @Override
                    public void onPrepared(MediaPlayer m) {
                        try {
                            if (m.isPlaying()) {
                                m.stop();
                                m.release();
                                m = new MediaPlayer();
                            }
                            m.setVolume(0f, 0f);
                            // m.setLooping(false);
                            m.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        mMediaPlayers[index].setOnVideoSizeChangedListener(this);
        mMediaPlayers[index].setAudioStreamType(AudioManager.STREAM_MUSIC);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayers();
}

private void releaseMediaPlayers() {
    for (int i = 0; i < mMediaPlayers.length; i  ) {
        if (mMediaPlayers[i] != null) {
            mMediaPlayers[i].release();
            mMediaPlayers[i] = null;
        }
    }
}

private void startVideoPlayback(MediaPlayer player) {
    Log.v(TAG, "MediaPlayer("   indexOf(player)   "): startVideoPlayback");
    player.start();
}

private int indexOf(MediaPlayer player) {
    for (int i = 0; i < mMediaPlayers.length; i  )
        if (mMediaPlayers[i] == player)
            return i;
    return -1;
}

private int indexOf(SurfaceHolder holder) {
    for (int i = 0; i < mSurfaceHolders.length; i  )
        if (mSurfaceHolders[i] == holder)
            return i;
    return -1;
}