val ijkMediaPlayer = IjkMediaPlayer()
fun initIJKPlayer(context: Context, surface: Surface){
val uri = "rtsp://192.168.137.1:8554/camera"
ijkMediaPlayer.setOnPreparedListener(object : IMediaPlayer.OnPreparedListener{
override fun onPrepared(mp: IMediaPlayer?) {
mp?.start()
}
})
ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "protocol_whitelist", "rtp,rtmp,rtsp,tcp,udp")
try {
ijkMediaPlayer.setDataSource(context, Uri.parse(uri))
} catch (e:IOException){
e.printStackTrace()
}
ijkMediaPlayer.setSurface(surface)
try {
ijkMediaPlayer.prepareAsync()
}catch (e:java.lang.IllegalStateException){
e.printStackTrace()
}
}
fun stopPlayer(){
mediaPlayer.release()
}
Composable中使用:
@SuppressLint("InflateParams")
@Composable
fun RobotControlPage( navController: NavHostController, myViewModel: MyViewModel, activity: MainActivity){
var surface by myViewModel.surface
AndroidView(factory = {
LayoutInflater.from(activity).inflate(R.layout.surfaceview,null,false).apply {
surface = findViewById(R.id.surface_view)
}
}){
surface?.holder?.addCallback(object: SurfaceHolder.Callback{
override fun surfaceCreated(p0: SurfaceHolder) {
myViewModel.initIJKPlayer(activity, p0.surface)
}
override fun surfaceChanged(p0: SurfaceHolder, p1: Int, p2: Int, p3: Int) {}
override fun surfaceDestroyed(p0: SurfaceHolder) {
myViewModel.stopPlayer()
}
})
}
}
以下是log
IJKMEDIA D IjkMediaPlayer_setOption
tv.danmaku....MediaPlayer D Couldn't open file on client side, trying server side
IJKMEDIA D IjkMediaPlayer_setDataSourceAndHeaders
IJKMEDIA V setDataSource: path rtsp://192.168.137.1:8554/camera
IJKMEDIA D ijkmp_set_data_source(url="rtsp://192.168.137.1:8554/camera")
IJKMEDIA D ijkmp_set_data_source(url="rtsp://192.168.137.1:8554/camera")=0
IJKMEDIA D IjkMediaPlayer_setVideoSurface
IJKMEDIA D ijkmp_set_android_surface(surface=0x7fc3a3b3c8)
IJKMEDIA D ffpipeline_set_surface()
IJKMEDIA D ijkmp_set_android_surface(surface=0x7fc3a3b3c8)=void
IJKMEDIA D IjkMediaPlayer_prepareAsync
IJKMEDIA D ijkmp_prepare_async()
IJKMEDIA W remove 'timeout' option for rtmp.
IJKMEDIA I ===== versions =====
IJKMEDIA I ijkplayer : k0.8.8
IJKMEDIA I FFmpeg : ff3.4--ijk0.8.7--20180103--001
IJKMEDIA I libavutil : 55.78.100
IJKMEDIA I libavcodec : 57.107.100
IJKMEDIA I libavformat : 57.83.100
IJKMEDIA I libswscale : 4.8.100
IJKMEDIA I libswresample: 2.9.100
IJKMEDIA I ===== options =====
IJKMEDIA I format-opts : ijkapplication = 493597805280
IJKMEDIA I format-opts : ijkiomanager = 493635880448
IJKMEDIA I format-opts : protocol_whitelist = rtp,rtmp,rtsp,tcp,udp
IJKMEDIA I ===================
IJKMEDIA D ijkmp_prepare_async()=0
IJKMEDIA I SDL_RunThread: [11158] ff_read
IJKMEDIA W remove 'timeout' option for rtmp.
IJKMEDIA I SDL_RunThread: [11156] ff_msg_loop
IJKMEDIA D message_loop
IJKMEDIA I SDL_RunThread: [11157] ff_vout
IJKMEDIA D FFP_MSG_FLUSH:
IJKMEDIA I Add dns cache hostname = 192.168.137.1, ip =
IJKMEDIA W 'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)
chatty I uid=10971(com.nrec.robotcontroller) ff_read identical 2 lines
IJKMEDIA W 'circular_buffer_size' option was set but it is not supported on this build (pthread support is required)
IJKMEDIA E Option ijkapplication not found.
IJKMEDIA D FFP_MSG_OPEN_INPUT:
OpenGLRenderer I Davey! duration=996ms; Flags=1, IntendedVsync=697187155982, Vsync=697187155982, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=697187200826, AnimationStart=697187285826, PerformTraversalsStart=697756646034, DrawStart=698036614888, SyncQueued=698107514628, SyncStart=698107627492, IssueDrawCommandsStart=698107920044, SwapBuffers=698181935409, FrameCompleted=698183639263, DequeueBufferDuration=4840000, QueueBufferDuration=1092000,
robotcontrolle W Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (light greylist, reflection)
robotcontrolle W Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (light greylist, reflection)
robotcontrolle W Accessing hidden method Landroid/os/Trace;->traceCounter(JLjava/lang/String;I)V (light greylist, reflection)
IJKMEDIA I max_frame_duration: 10.000
IJKMEDIA D FFP_MSG_FIND_STREAM_INFO:
IJKMEDIA I Input #0, rtsp, from 'rtsp://192.168.137.1:8554/camera':
IJKMEDIA I Metadata:
IJKMEDIA I title :
IJKMEDIA I No Name
IJKMEDIA I
IJKMEDIA I Duration:
IJKMEDIA I N/A
IJKMEDIA I , start:
IJKMEDIA I 0.588646
IJKMEDIA I , bitrate:
IJKMEDIA I N/A
IJKMEDIA I
IJKMEDIA I Stream #0:0
IJKMEDIA I : Video: h264, yuvj420p(pc, progressive), 480x360 [SAR 4:3 DAR 16:9]
IJKMEDIA I ,
IJKMEDIA I 25 fps,
IJKMEDIA I 25 tbr,
IJKMEDIA I 90k tbn,
IJKMEDIA I 50 tbc
IJKMEDIA I
IJKMEDIA I Stream #0:1
IJKMEDIA I : Audio: aac, 48000 Hz, stereo, fltp
IJKMEDIA I
IJKMEDIA I SDL_Android_AudioTrack: CHANNEL_OUT_STEREO
IJKMEDIA I SDL_Android_AudioTrack: ENCODING_PCM_16BIT
AudioTrack V getMinFrameCount=3848: afFrameCount=960, afSampleRate=48000, afLatency=80
AudioTrack D set(): streamType -1, sampleRate 48000, format 0x1, channelMask 0x3, frameCount 7696, flags #0, notificationFrames 0, sessionId 0, transferType 3, uid -1, pid -1
AudioTrack V set() streamType -1 frameCount 7696 flags 0000
AudioTrack V Building AudioTrack with attributes: usage=1 content=2 flags=0x200 tags=[]
AudioTrack W Use of stream types is deprecated for operations other than volume control
AudioTrack W See the documentation of AudioTrack() for what to use instead with android.media.AudioAttributes to qualify your playback use case
IJKMEDIA I SDL_Android_AudioTrack_new_from_spec: init volume as 1.000000/(0.000000,1.000000)
IJKMEDIA I audio_session_id = 49
IJKMEDIA I AudioCodec: avcodec, aac
IJKMEDIA I SDL_RunThread: [11171] ff_aout_android
IJKMEDIA I SDL_RunThread: [11172] ff_audio_dec
PlayerBase V baseStart() piid=71
IJKMEDIA I VideoCodec: avcodec, h264
IJKMEDIA W fps: 25.000000 (normal)
IJKMEDIA W fps: 25.000000 (normal)
IJKMEDIA D FFP_MSG_COMPONENT_OPEN:
IJKMEDIA D FFP_MSG_VIDEO_SIZE_CHANGED: 480, 360
IJKMEDIA D FFP_MSG_SAR_CHANGED: 4, 3
IJKMEDIA D ijkmp_get_msg: FFP_MSG_PREPARED
IJKMEDIA D FFP_MSG_PREPARED:
IJKMEDIA D IjkMediaPlayer_start
IJKMEDIA D ijkmp_start()
IJKMEDIA D ijkmp_start()=0
IJKMEDIA D ijkmp_get_msg: FFP_REQ_START
IJKMEDIA D FFP_MSG_AUDIO_RENDERING_START:
IJKMEDIA I SDL_RunThread: [11182] ff_video_dec
IJKMEDIA D FFP_MSG_VIDEO_ROTATION_CHANGED: 0
IJKMEDIA D SDL_VoutFFmpeg_CreateOverlay(w=480, h=360, fmt=RV32(0x32335652, dp=0x72d87deec0)
IJKMEDIA D FFP_MSG_VIDEO_SIZE_CHANGED: 480, 360
IJKMEDIA D Video: first frame decoded
IJKMEDIA D FFP_MSG_VIDEO_DECODED_START:
IJKMEDIA D SDL_VoutFFmpeg_CreateOverlay(w=480, h=360, fmt=RV32(0x32335652, dp=0x72d87deec0)
IJKMEDIA D FFP_MSG_VIDEO_SIZE_CHANGED: 480, 360
IJKMEDIA D SDL_VoutFFmpeg_CreateOverlay(w=480, h=360, fmt=RV32(0x32335652, dp=0x72d87deec0)
IJKMEDIA D FFP_MSG_VIDEO_SIZE_CHANGED: 480, 360
IJKMEDIA D ANativeWindow_setBuffersGeometry: w=2207, h=1080, f=(0x4) => w=480, h=360, f=RV32(0x32335652)
IJKMEDIA D FFP_MSG_VIDEO_RENDERING_START:
tv.danmaku....MediaPlayer I Info: MEDIA_INFO_VIDEO_RENDERING_START
IJKMEDIA D avcodec/Audio: first frame decoded
IJKMEDIA D FFP_MSG_AUDIO_DECODED_START:
ProfileInstaller D Installing profile for com.example.myapplication
使用0.8.8版一直无法播放RTSP视频,surfaceView一直黑屏,不明白是什么原因 myViewModel中初始化:
Composable中使用:
以下是log