How to video record with specific sound programmatically in android?

后端 未结 4 1380
终归单人心
终归单人心 2021-01-03 00:13

I have created functionality to record video in my app.

When I play a song, that song is recorded with video and a video file is created, similar to a dubshmash app

相关标签:
4条回答
  • 2021-01-03 00:43

    You can record video without audio and merge audio later on using mp4 parser like this:

    /*
     * @param videoFile path to video file
     * @param audioFile path to audiofile
    */
        public String mux(String videoFile, String audioFile) {
            Movie video = null;
            try {
                video = new MovieCreator().build(videoFile);
            } catch (RuntimeException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        Movie audio = null;
        try {
            audio = new MovieCreator().build(audioFile);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (NullPointerException e) {
    
            e.printStackTrace();
            return null;
        }
        int size = audio.getTracks().size();
        Track audioTrack = audio.getTracks().get((size - 1));
        video.addTrack(audioTrack);
    
        Container out = new DefaultMp4Builder().build(video);
    
        File myDirectory = new File(Environment.getExternalStorageDirectory(), "/Folder Name");
        if (!myDirectory.exists()) {
            myDirectory.mkdirs();
        }
        filePath = myDirectory + "/video" + System.currentTimeMillis() + ".mp4";
        try {
            RandomAccessFile ram = new RandomAccessFile(String.format(filePath), "rw");
            FileChannel fc = ram.getChannel();
            out.writeContainer(fc);
            ram.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    return filePath;
    }
    

    In build.gradle add following dependency

    compile 'com.googlecode.mp4parser:isoparser:1.0.5.4'
    
    0 讨论(0)
  • 2021-01-03 00:44

    You can use a MediaRecorder without calling setAudio* on it. remove this line

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    

    see this link

    0 讨论(0)
  • 2021-01-03 00:47

    There is currently no way to directly record android output without "background noise".

    Note that this is a security concern to restrict access to other apps audio output, therefore it is very unlikely that it could be achieved directly.

    See this answer

    0 讨论(0)
  • 2021-01-03 01:03

    If you want to working with video then you have to use FFMPEG library

    That can be you can work with Video.

    That for i have already give answer to How to use ffmpeg in android studio? see this LINK. Go step by step and import in your project

    0 讨论(0)
提交回复
热议问题