N
O
D
E
M
E
D
I
A
Thinking
首页
产品
文档
博客
订单
文档
快速接入-JAVA
2026年 5月 20日 下午3:52
# 简介 NodeMediaClient-Android 支持原生java接入,对于现有应用,仅需几行代码便可实现超低延迟RTMP、RTSP、HTTP-FLV播放接入。RTMP直播推流,3行代码开启美颜滤镜。高性能硬件编解码,标准H264/H265 编码支持。 ## 接入jitpack.io,修改settings.gradle ``` dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url 'https://jitpack.io' } } } ``` ## 加入依赖,修改build.gradle ``` dependencies { implementation 'com.github.NodeMedia:NodeMediaClient-Android:4.0.11' } ``` ## 播放接入 ### 视图, 定义一个全屏FrameLayout,id为videoView ``` <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LivePlayerActivity"> <FrameLayout android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout> ``` ### Activity类 ``` package cn.nodemedia.javademo; import android.os.Bundle; import android.util.Log; import android.widget.FrameLayout; import androidx.appcompat.app.AppCompatActivity; import cn.nodemedia.NodePlayer; public class LivePlayerActivity extends AppCompatActivity { private NodePlayer np; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_live_player); FrameLayout vv = findViewById(R.id.videoView); np = new NodePlayer(this, ""); np.setOnNodePlayerEventListener((player, event, msg) -> { Log.d("NodePlayer", "event:" + event + " msg:" + msg); }); // 设置事件监听器 np.attachView(vv); // 附加到视图 np.setBufferTime(1000); // 设置缓冲时间 单位 ms, 可随时修改 np.setScaleMode(1); // 设置缩放模式 可随时修改 np.setVolume(1.0f);// 设置音量, 可随时修改 np.setHWAccelEnable(true); // 启用硬件加速 // np.setRTSPTransport("tcp");// 设置 RTSP 传输模式 // np.setHTTPReferer("custom http referer"); // np.setHTTPUserAgent("custom http user agent"); np.start("rtmp://live.nodemedia.cn/live/bbb"); // 开始播放 } @Override protected void onDestroy() { super.onDestroy(); np.stop(); // 停止播放 np.detachView(); // 移除视图 } } ``` ## 推流接入 ### 视图, 定义一个全屏FrameLayout,id为cameraView, 并定义一个按钮来开始或停止推流 ``` <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LiveStreamActivity"> <FrameLayout android:id="@+id/cameraView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> </FrameLayout> <Button android:id="@+id/streamButton" android:layout_width="0dp" android:layout_height="60dp" android:layout_marginHorizontal="32dp" android:layout_marginBottom="32dp" android:text="开始推流" android:textSize="18sp" android:background="@drawable/stream_button_bg" android:textColor="@android:color/white" android:elevation="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` ### Activity 类 ``` package cn.nodemedia.javademo; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.FrameLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import cn.nodemedia.NodePublisher; public class LiveStreamActivity extends AppCompatActivity { private NodePublisher np; private Button streamButton; private boolean isStreaming = false; private final String rtmpUrl = "rtmp://192.168.0.2/live/stream"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_live_stream); FrameLayout cv = findViewById(R.id.cameraView); streamButton = findViewById(R.id.streamButton); np = new NodePublisher(this,""); np.setOnNodePublisherEventListener((publisher, event, msg) -> { Log.d("NodePublisher", "event: " + event + ", msg: " + msg); }); np.attachView(cv); // 绑定视图 np.setCryptoKey(""); // 设置加密密钥 16 位 np.setAudioCodecParam(NodePublisher.NMC_CODEC_ID_AAC, NodePublisher.NMC_PROFILE_AAC_LC, 44100, 2, 64 * 1024); // 设置音频参数 np.setVideoCodecParam(NodePublisher.NMC_CODEC_ID_H264, NodePublisher.NMC_PROFILE_H264_MAIN, 720, 1280, 30, 2000 * 1024); // 设置视频参数 np.setKeyFrameInterval(2); // 设置关键帧间隔, 单位 s np.setVideoOrientation(NodePublisher.VIDEO_ORIENTATION_PORTRAIT);// 设置视频方向 np.setHWAccelEnable(true); // 启用硬件加速 // 这两项需要同时设置 np.setEffectStyle(NodePublisher.EFFECTOR_STYLE_ID_FAIRSKIN); // 设置画面风格特效 np.setEffectParameter(NodePublisher.EFFECTOR_STYLE, 1.0f); // 设置画面风格特效强度, 取值范围 0.0 ~ 1.0 np.setEffectParameter(NodePublisher.EFFECTOR_SMOOTHSKIN, 1.0f); // 设置磨皮特效的强度, 取值范围 0.0 ~ 1.0 // 特效风格和特效等级可随时设置 np.openCamera(NodePublisher.NMC_CAMERA_FRONT); // 打开前置摄像头 streamButton.setOnClickListener(v -> { if (isStreaming) { stopStreaming(); } else { startStreaming(); } }); } private void startStreaming() { int result = np.start(rtmpUrl); if (result == 0) { isStreaming = true; streamButton.setText("停止推流"); Toast.makeText(this, "推流已开始", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "推流启动失败,错误码: " + result, Toast.LENGTH_SHORT).show(); } } private void stopStreaming() { np.stop(); isStreaming = false; streamButton.setText("开始推流"); Toast.makeText(this, "推流已停止", Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); if (isStreaming) { np.stop(); // 停止推流 } np.detachView();// 销毁视图 np.closeCamera(); // 关闭摄像头 } } ``` ## JAVA Demo 源码 [JavaDemo.zip](https://www.nodemedia.cn/doc/server/index.php?s=/api/attachment/visitFile&sign=71869f03891b2c909cbde0a666ec2506 "[JavaDemo.zip")
嘿,我是小R,需要帮助随时找我哦
QQ客服:281269007
邮件支持
扫码加微信
回到顶部