Android 海康视频监控预览实现

信息发布系统中需要支持海康视频监控预览功能,下面将列出功能实现相关流程

文末附带demo

准备工作(省略步骤,module文件和so文件见demo)

1.引用module

//组件依赖
implementation project(path: ':hatom-video-player')

2.创建jnilib,复制粘贴so库文件

 3.build.gradle文件

defaultConfig {
...
	 ndk{
            abiFilters 'arm64-v8a','armeabi-v7a'
        }
...
}

正式开始:

1.manifest文件,申请权限,开启硬件加速(SurfaceView需要)

<uses-permission android:name="android.permission.INTERNET" />
<!-- 存储   -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
        android:name=".MyApp"
        android:allowBackup="true"
        android:hardwareAccelerated="true"

...

</application>

2.自定义Application,初始化sdk

public class BaseApp extends Application {
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        HatomPlayerSDK.init(this, "", isAppDebug());
    }

    public static Context getContext() {
        return context;
    }

    public static boolean isAppDebug() {
        if ("".equals(context.getPackageName())) return false;
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);
            return ai != null && (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }
}

3.MainActivity中调用

//监控视频短链接地址
public static final String playUrl = "rtsp://61.53.68.15:554/openUrl/NCfAEgM";
//创建HatomPlayer实例
    HatomPlayer hatomPlayer = new DefaultHatomPlayer();

//onCreate中增加回调
binding.texture.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                //设置播放画面显示surface,支持TextureView和SurfaceView
                hatomPlayer.setSurfaceTexture(binding.texture.getSurfaceTexture());
                //播放前设置播放配置(可选)
                PlayConfig playConfig = new PlayConfig();
                //使用硬解码
                playConfig.hardDecode = false;
                //开启智能信息
                playConfig.privateData = false;
                hatomPlayer.setPlayConfig(playConfig);
                //设置播放参数
                //realPlayUrl为预览短链接,需要通过调用openApi获取
                hatomPlayer.setDataSource(playUrl, null);
                //设置播放回调
                hatomPlayer.setPlayStatusCallback((status, s) -> {
                    switch (status) {
                        case SUCCESS:
                            Log.i("swyLog", "播放成功");
                            break;
                        case FAILED:
                        case EXCEPTION:
                        case FINISH:
                            hatomPlayer.stop();
                            Log.i("swyLog", "error is : " + MessageFormat.format("播放失败,错误码为:{0}", convertToHexString(s)));
                            break;
                        default:
                            break;
                    }
                });
                hatomPlayer.start();
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
            }
        });

//格式化错误码方法
public static String convertToHexString(String errorCode) {
        if (errorCode.startsWith("0x")) {
            return errorCode;
        }
        if (TextUtils.isEmpty(errorCode)) {
            return "";
        }
        int parseInt = Integer.parseInt(errorCode);
        StringBuilder hexCode = new StringBuilder(Integer.toHexString(parseInt));
        if (hexCode.length() < 8) {
            int count = 8 - hexCode.length();
            for (int i = 0; i < count; i++) {
                hexCode.insert(0, "0");
            }
        }
        return MessageFormat.format("{0}{1}", "0x", hexCode.toString());
    }

重要说明:

binding.texture.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                //设置播放画面显示surface,支持TextureView和SurfaceView
                hatomPlayer.setSurfaceTexture(binding.texture.getSurfaceTexture());

这里使用的TextureView.getSurfaceTexture,如果不增加SurfaceTextureListener监听,则会无法正常显示监控预览,另外,根据自己在设备上实际测试发现,从设置该回调,到进入onSurfaceTextureAvailable回调,中间会有一段比较长的时间(秒级)

实际业务场景遇到的问题分享:

信息发布系统的业务场景里,因为要单独定义一个组件PreviewWidget,然后父布局动态的addView加载该组件,因为MainActivity中需要执行业务,然后组件中只给外部返回一个hatomPlayer对象,但是把后续代码拿出来之后,就发现总是白屏,显示不出来,打断点发现,texture.getSurfaceTexture总是拿到空,想了很多办法都无法解决。最终发现的问题所在,就是上面说的进入回调会有一个秒级的延时,所以,最终的解决方法就是PreviewWidget中的onSurfaceTextureAvailable响应之后,给MainActivity发event事件通知,然后MainActivity再去执行后续逻辑,至此问题就解决了,代码如下,可以参考一下便于理解本段表述的意思

PreviewWidget

texture_view.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        hatomPlayer.setSurfaceTexture(texture_view.getSurfaceTexture());
        LiveDataBus.getInstance()
                .with(EventBus.PREVIEW_AVAILABLE, PreviewAvailableEvent.class)
                .postValue(new PreviewAvailableEvent());
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
});

MainActivity

LiveDataBus.getInstance()
        .with(EventBus.PREVIEW_AVAILABLE, PreviewAvailableEvent.class)
        .observe(this, event -> {
            showTextures();
        });

public void showTextures() {
        if (TextUtils.isEmpty(previewUrl)) {
            Log.i("swyLog", "监控预览地址为空");
            return;
        }
        //播放前设置播放配置(可选)
        PlayConfig playConfig = new PlayConfig();
        //使用硬解码
        playConfig.hardDecode = true;
        //开启智能信息
        playConfig.privateData = true;
        hatomPlayer.setPlayConfig(playConfig);
        //设置播放参数
        //预览短链接,需要通过调用openApi获取
//        String url = "rtsp://61.53.68.15:554/openUrl/e9KXHVu";
        hatomPlayer.setDataSource(previewUrl, null);
        //设置播放回调
        hatomPlayer.setPlayStatusCallback((status, s) -> {
            switch (status) {
                case SUCCESS:
                    runOnUiThread(() -> {
                        previewWidget.setError("");
                        Log.i("swyLog", "播放成功");
                    });
                    break;
                case FAILED:
                case EXCEPTION:
                case FINISH:
                    runOnUiThread(() -> {
                        hatomPlayer.stop();
                        previewWidget.setError(MessageFormat.format("播放失败,错误码为:{0}", convertToHexString(s)));
                        Log.i("swyLog", "error is : " + MessageFormat.format("播放失败,错误码为:{0}", convertToHexString(s)));
                    });
                    break;
                default:
                    break;
            }
        });
        hatomPlayer.start();
    }

至此完结

demo源码

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java对接大华海康视频监控是指通过Java编程语言实现与大华和海康视频监控设备的交互和通讯。Java是一种跨平台的高级编程语言,其强大的网络编程能力和丰富的第三方库使得与视频监控设备的对接变得更加简单和灵活。 对接大华海康视频监控可以利用Java提供的网络编程功能,使用Socket或HTTP协议与视频监控设备进行通信。首先,需要通过设备的IP地址和端口号建立与设备的连接。然后,通过发送指令和接收设备的响应来实现视频监控设备的控制和操作。 在Java中,可以使用第三方库来简化与大华海康视频监控设备的对接过程。例如,对于大华视频监控设备,可以使用Java SDK提供的相关接口,通过调用SDK中的方法实现设备的登录、预览、录像回放和控制等功能。 对于海康视频监控设备,可以使用Java SDK提供的海康芯片平台开发包(SDK)来实现对接。通过该SDK,可以获取设备的基本信息、实时视频流、录像文件等,并进行远程控制和操作。 在对接大华海康视频监控时,还可以利用Java提供的图形用户界面(GUI)开发工具包,如JavaFX或Swing,将视频监控画面显示在程序界面上,以便用户实时查看监控画面,同时结合图像处理和分析算法,实现实时监控、报警和数据分析等功能。 总之,Java对接大华海康视频监控是一种灵活、高效的方式,能够通过Java编程语言实现视频监控设备的交互和通讯,满足不同应用场景下的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间如潮水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值