Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

android - MediaRecorder setProfile() throwing IllegalStateException without setting audio source

I am creating a Screen recording app and using MediaRecorder for recording video. The screen recording works fine when I set the audio source for media recorder instance through setAudioSource(...). Since this approach also lets me record audio along with video, I also want to record video without audio. So, for that i removed the setAudioSource(...) function call (as stated by the documentation):-

Sets the audio source to be used for recording. If this method is not called, the output file will not contain an audio track.

Code:-

private fun createMediaRecorder(mScreenWidth: Int, mScreenHeight: Int): MediaRecorder {
    val mMediaRecorder = MediaRecorder()
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT)   //code doesn't work without this line of code
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE)
    val profile: CamcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)
    profile.videoFrameWidth = mScreenWidth
    profile.videoFrameHeight = mScreenHeight
    mMediaRecorder.setProfile(profile)
    setOutputFile(mMediaRecorder)
    mMediaRecorder.prepare()
    return mMediaRecorder
}


private fun setOutputFile(mediaRecorder: MediaRecorder) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        val contentVals = ContentValues()
        contentVals.put(MediaStore.MediaColumns.DISPLAY_NAME, "some")
        contentVals.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")
        contentVals.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MOVIES)
        val uri =
            contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentVals)
        val fileDescriptor = contentResolver.openFileDescriptor(uri!!, "w")
        mediaRecorder.setOutputFile(fileDescriptor!!.fileDescriptor)
    } else {
        val fileDir =
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
        val file = File(fileDir, "some.mp4")
        mediaRecorder.setOutputFile(file.path)
    }
}

Exception:-

Caused by: java.lang.IllegalStateException
    at android.media.MediaRecorder.setAudioEncoder(Native Method)
    at android.media.MediaRecorder.setProfile(MediaRecorder.java:559)
    at com.abdulwahabfaiz.myapplication.MediaService.createMediaRecorder(MediaService.kt:120)
    at com.abdulwahabfaiz.myapplication.MediaService.onStartCommand(MediaService.kt:56)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...