智能车从入坑到弃坑1 移动控制端 远程视频打开rtsp码流

build.gradle(app)

implementation 'com.github.NodeMedia:NodeMediaClient-Android:2.9.23'

myvideo.java

package com.example.myapp.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.fragment.app.Fragment;

import com.example.myapp.R;

import cn.nodemedia.NodePlayer;
import cn.nodemedia.NodePlayerView;

public class myvideo extends Fragment implements View.OnClickListener{
    private View root;
    private Button btn_openorclose;
    private Spinner spinner_rtsp;
    private NodePlayerView nodePlayerView;
    public static NodePlayer nodePlayer;
    private Context mContext;
    private String address = "rtsp://192.168.1.25:8554/rtsp001";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }


    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,
                             Bundle savedInstanceState) {
        if(root == null){
            root = inflater.inflate(R.layout.my_video,container,false);
        }
        btn_openorclose = root.findViewById(R.id.button_openorclose);
        btn_openorclose.setOnClickListener(this);
        btn_openorclose.setText("打开相机");
        initSpinnerForDropdown();

        spinner_rtsp = root.findViewById(R.id.rtspspinner);
        nodePlayerView = root.findViewById(R.id.play_surface);
        //设置渲染器类型
        nodePlayerView.setRenderType(NodePlayerView.RenderType.SURFACEVIEW);
        //设置视频画面缩放模式
        nodePlayer = new NodePlayer(mContext);
        //设置播放视图
        nodePlayer.setPlayerView(nodePlayerView);
        nodePlayer.setRtspTransport(NodePlayer.RTSP_TRANSPORT_UDP);

        nodePlayer.setBufferTime(1000);//设置缓冲时间
        nodePlayer.setMaxBufferTime(1000);//设置最大缓冲时间
        nodePlayer.setAutoReconnectWaitTimeout(10000);
        nodePlayer.setConnectWaitTimeout(10000);
        nodePlayer.setBufferTime(0);
        nodePlayer.setMaxBufferTime(0);
        nodePlayer.setVideoEnable(true);//设置视频启用
        nodePlayer.setInputUrl(address);
        // 返回碎片视图
        return root;
    }

    // 初始化下拉模式的列表框
    private void initSpinnerForDropdown() {
        SharedPreferences configdata = mContext.getSharedPreferences("MyAppConfig", 0);
        /*分别调用get+数据类型,去获取前面所存储的姓名等等,如果没有找到相对应的值,则会用方法中的默认值来代替。*/
        String str_rtsp = configdata.getString("rtsp","");
        String[] rtspArray = {str_rtsp};
        //rtspArray[0] = str0;
        // 声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(mContext,
                R.layout.item_select, rtspArray);
        // 从布局文件中获取名叫sp_dropdown的下拉框
        Spinner sp_dropdown = root.findViewById(R.id.rtspspinner);
        // 设置下拉框的标题。对话框模式才显示标题,下拉模式不显示标题
        //sp_dropdown.setPrompt("请选择行星");
        sp_dropdown.setAdapter(starAdapter); // 设置下拉框的数组适配器
        sp_dropdown.setSelection(0); // 设置下拉框默认显示第一项

        // 给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
        //sp_dropdown.setOnItemSelectedListener(new MySelectedListener());
    }


    public void onClick(View v) {
        if (v.getId() == R.id.button_openorclose) {
            if(btn_openorclose.getText().toString()=="打开相机"){
                toast("正在打开相机");
                btn_openorclose.setText("关闭相机");
                openvideo(spinner_rtsp.getSelectedItem().toString());
            }else {
                btn_openorclose.setText("打开相机");
                toast("正在关闭相机");
                closevideo();
            }
        }
    }

    public void openvideo(String straddr){
        nodePlayer.setInputUrl(straddr);
        nodePlayer.start();
    }

    static public void closevideo(){
        nodePlayer.stop();
        nodePlayer.release();
    }

    private void toast(String msg) {
        Toast.makeText(this.mContext, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        nodePlayer.stop();
        nodePlayer.release();
    }

}

my_video.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <cn.nodemedia.NodePlayerView
            android:id="@+id/play_surface"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="MissingClass" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Spinner
                    android:id="@+id/rtspspinner"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="3"
                    android:background="#33FFFFFF"
                    android:spinnerMode="dropdown"
                    android:textColor="#00000000" />

                <android.widget.Button
                    android:id="@+id/button_openorclose"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#33FFFFFF"
                    android:text=""
                    android:textColor="#2196F3" />

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="8" />
            </LinearLayout>
            <Space
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="15" />
        </LinearLayout>
    </FrameLayout>
    <!---android:alpha="0.5"-->
</LinearLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值