我正在尝试使用RTMP协议在应用中流式传输相机。从一周开始,我正在调试ExoPlayer和Librtmp客户端,但不知何故,Librtmp客户端无法建立连接并触发RtmpIOException,内部出现ECONNREFUSED
错误。试过Socket编程但得到了同样的错误。 IP ping工作正常。我的同事最近就Librtmp GitHub回购提出了一个问题。自1月份以来,Devs没有回复。
我们尝试过其他lib,例如IJKPlayer(内部使用FFmpeg),它能够预览流,但它在流预览之前有更多的延迟,而且还有摄像头wifi断开问题。由于使用JNI构建,因此无法进一步调试内部。
代码:
private val bandwidthMeater = DefaultBandwidthMeter()
private val adaptiveTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeater)
private val trackSelector = DefaultTrackSelector(adaptiveTrackSelectionFactory)
private val player = ExoPlayerFactory.newSimpleInstance(context, trackSelector)
fun init() {
player.setVideoSurface(holder.surface)
}
fun startStream(streamUrl: String, startListener: () -> Unit) {
//e.g. streamUrl = rtmp://192.168.42.5:1936/live/myStream (IP is Android device IP provide by camera when connected to its wifi)
val rtmpDatasourceFactory = RtmpDatasource.RtmpDataSourceFactory()
val factory = ExtractorMediaSource.Factory(rtmpDatasourceFactory)
val extractorMediaSource =factory.createMediaSource(Uri.parse(streamUrl))
player.prepare(extractorMediaSource)
player.playWhenReady = true
}
ExoPlayer错误:
E/ExoPlayerImplInternal: Source error.
net.butterflytv.rtmp_client.RtmpClient$RtmpIOException
at net.butterflytv.rtmp_client.RtmpClient.open(RtmpClient.java:56)
at com.google.android.exoplayer2.ext.rtmp.RtmpDataSource.open(RtmpDataSource.java:57)
at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:841)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:308)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
连接错误:
java.net.ConnectException: failed to connect to /192.168.42.5 (port 1936): connect failed: ECONNREFUSED (Connection refused)
答案 0 :(得分:-1)
您是否在运行时请求许可?
每次遇到这种类型的错误,这就是我忘记做的事情。
https://developer.android.com/training/permissions/requesting#java