我有一堂课,它在ExoPLayer的帮助下观看rtmp流:
player = ExoPlayerFactory.newSimpleInstance(context)
val rtmpDataSourceFactory = RtmpDataSourceFactory()
val videoSource = ProgressiveMediaSource.Factory(rtmpDataSourceFactory)
.createMediaSource(Uri.parse(streamURL))
player.prepare(videoSource)
player.setVideoTextureView(playerView)
player.playWhenReady = true
playerView
是TextureView,而不是SurfaceView,因为我还需要从流中获取屏幕截图。
据我所知,ExoPlayer没有用于流记录的方法,只能下载,所以问题是-我如何记录rtmp流?我搜索了很多库,并提出了堆栈问题,但仍然找不到干净,正常的解决方案。
[目前,我正在尝试通过基本的MediaRecorder记录流,并获得Android开发人员的帮助,但我仍然不明白MediaRecorder如何获取流数据或至少获取表面数据。
val path = "${Environment.getExternalStorageDirectory()}${File.separator}${Environment.DIRECTORY_DCIM}${File.separator}${"FILE_NAME"}"
recorder = MediaRecorder().apply {
setVideoSource(MediaRecorder.VideoSource.SURFACE)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setOutputFile(path)
start()
}
implementation 'com.writingminds:FFmpegAndroid:0.3.2
这是代码:
// Build path for recorded video
val title = "/" + System.currentTimeMillis().toString() + ".mp4"
val targetFile = File(getExternalStoragePublicDirectory(DIRECTORY_DCIM).toString() + title)
// FFMpeg command for stream recording
val command = arrayOf("-i", streamURL, "-acodec", "copy", "-vcodec", "copy", targetFile.toString())
try {
// Load the binary
ffmpeg.loadBinary(object : LoadBinaryResponseHandler() {})
} catch (e: FFmpegNotSupportedException) {
e.printStackTrace()
}
try {
// Execute command
ffmpeg.execute(command, object : ExecuteBinaryResponseHandler() {})
} catch (e: FFmpegCommandAlreadyRunningException) {
e.printStackTrace()
}