How to use ContentResolver while merging Video and Audio file using isoparser library in Android 10

Viewed 122

I am trying to merge a video file (.mp4 format) and an audio file (.aac format) using "com.googlecode.mp4parser:isoparser". The merged file is getting saved properly if I use the deprecated method "Environment.getExternalStorageDirectory()" to create FileOutputStream.

But I don't want to use this deprecated method. So I am using ContentResolver to create FileOutputStream, but then merged file is getting saved without audio. I want the merged file to be saved in external storage under Movies folder so that it will be visible in Gallery app.

Below is my code.

        val mp4File = MovieCreator.build(FileDataSourceImpl(videoFilePath))
        val aacTrack = AACTrackImpl(FileDataSourceImpl(audioFilePath)

        val nuTracks : ArrayList<Track> = arrayListOf()
        for (track in mp4File.tracks){
            if (track.handler.equals("vide")){
                nuTracks.add(track)
            }
        }
        nuTracks.add(aacTrack)

        val movie = Movie()
        movie.tracks = nuTracks

        val mp4file = DefaultMp4Builder().build(movie)
        
        val resolver = context!!.contentResolver
        val contentValues = ContentValues().apply {
            put(MediaStore.MediaColumns.DISPLAY_NAME, "FinalMergedFile")
            put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")
            put(MediaStore.MediaColumns.RELATIVE_PATH, "Movies/${myFolder}/")
        }
        val collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
        val uriSavedVideo = resolver.insert(collection, contentValues)
        val pfd = resolver.openFileDescriptor(uriSavedVideo!!, "rwt")
        val fc = FileOutputStream(pfd!!.fileDescriptor).channel
        mp4file.writeContainer(fc)
        fc.close()
0 Answers
Related