如何在Android中设置纵向rtsp流视频的方向?

时间:2017-04-04 10:11:20

标签: android rtsp

我正试图以纵向方式播放rtsp视频流,任何人都可以帮我吗?

我做过类似的事情 videoView.setVideoPath("rtsp://172.18.24.105:1234"); videoView.requestFocus(); videoView.start();

Video is playing in this way,Click to see 下面是我已经开始rtsp流的活动代码,它也可以从rtsp url播放rtsp流,问题是rtsp视频流正在播放其他方向,如上图链接。 有人可以帮我这个吗?我的活动代码中是否存在启动rtsp流可能影响视频流方向的问题

public class Example1Activity扩展AppCompatActivity {

private final static String TAG = "MainActivity";
private SurfaceView mSurfaceView;
private RtspServer mRtspServer;
private ActivityExampleBinding binding;
private SharedPreferences prefs;
private VideoView videoView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_example);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mSurfaceView = (SurfaceView) findViewById(R.id.surface);
    // Sets the port of the RTSP server to 1234
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(RtspServer.KEY_PORT, String.valueOf(1234));
    editor.apply();

    SessionBuilder.getInstance()
            .setSurfaceView(mSurfaceView)
            .setPreviewOrientation(90)
            .setContext(getApplicationContext())
            .setAudioEncoder(SessionBuilder.AUDIO_AAC)
            .setAudioQuality(new AudioQuality(16000, 24000))
            .setVideoEncoder(SessionBuilder.VIDEO_H264)
            .setVideoQuality(new VideoQuality(176, 144, 12, 56000))
            .setCamera(1)
            .build();

    // Starts the RTSP server
    this.startService(new Intent(this, CustomRtspServer.class));

    binding.btView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (binding.etUrl.getText().toString().startsWith("rtsp://")) {
                prefs.edit().putString("rtspUrl", binding.etUrl.getText().toString()).apply();
                binding.video.setVideoPath(binding.etUrl.getText().toString());
                binding.video.requestFocus();
                binding.progressBar.setVisibility(View.VISIBLE);
                binding.video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        binding.progressBar.setVisibility(View.GONE);
                    }
                });
                binding.video.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                        binding.progressBar.setVisibility(View.GONE);
                        return false;
                    }
                });
                binding.video.start();
            } else {
                Toast.makeText(Example1Activity.this, "Please enter valid rtsp URL", Toast.LENGTH_SHORT).show();
            }

        }
    });


}

@Override
protected void onResume() {
    super.onResume();
    bindService(new Intent(this, CustomRtspServer.class), mRtspServiceConnection, Context.BIND_AUTO_CREATE);

    String url = prefs.getString("rtspUrl", null);
    if (!TextUtils.isEmpty(url)) {
        binding.etUrl.setText(url);
    }
}

private ServiceConnection mRtspServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mRtspServer = (CustomRtspServer) ((RtspServer.LocalBinder) service).getService();
        mRtspServer.addCallbackListener(mRtspCallbackListener);
        mRtspServer.start();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    }

};

private RtspServer.CallbackListener mRtspCallbackListener = new RtspServer.CallbackListener() {

    @Override
    public void onError(RtspServer server, Exception e, int error) {
        // We alert the user that the port is already used by another app.
        if (error == RtspServer.ERROR_BIND_FAILED) {
        }
    }

    @Override
    public void onMessage(RtspServer server, int message) {

    }

};

@Override
protected void onStop() {
    super.onStop();
    if (mRtspServer != null) mRtspServer.removeCallbackListener(mRtspCallbackListener);
    unbindService(mRtspServiceConnection);
}

}`

activity_example

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:color="@android:color/background_light"
    tools:context=".MainActivity">

    <net.majorkernelpanic.streaming.gl.SurfaceView
        android:id="@+id/surface"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="8dp" />

    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll"
        android:layout_alignParentTop="true"
        android:visibility="visible" />


    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/etUrl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textColor="#ffffff"
            app:backgroundTint="#ffffff" />

        <Button
            android:id="@+id/btView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Play" />
    </LinearLayout>

</RelativeLayout>

0 个答案:

没有答案