6

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 application.

Now the problem that I am facing is that other voices such as near by sounds also get recorded. The song file is recorded in the video record screen and I play the song when video recording activity launches.

How can I have my application record only song with video?

 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
  mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

Is there any solution in audio source set as a speaker , because song sound going through a speaker? if is it another possible way please reply me.

Mayank Sugandhi
  • 397
  • 1
  • 7
  • 22

4 Answers4

8

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'
Muhammad Umair Shafique
  • 2,475
  • 1
  • 30
  • 39
  • Muhammad :- if i merge audio and video file, then issue is coming that i want to cut audio track for 10 seconds. because my process is that in video recording screen there will be playing music for full length, and when i click the button then video and audio record as well. – Mayank Sugandhi Mar 08 '16 at 07:57
  • Muhammad :-there are two phase. if i record both then environment noise problem. and if i merge the two files then i have to record internally of audio song for two second during without sound of video recording. then question is arise that how to record or cut audio for 10 seconds internally in android? – Mayank Sugandhi Mar 08 '16 at 07:59
  • The video's default audio and the new recorded audio gets overlapped when I try your solution.Can you help me out – karthik vishnu kumar Oct 14 '17 at 13:16
  • You need to extract video only from original movie and then include new audio. You can do this using FFmpeg Java Wrapper "Javacv". Please check my new answer. – Muhammad Umair Shafique Oct 16 '17 at 04:03
  • will this parser work if the audio file is of 'mp3' format? – Janaaaa May 31 '18 at 06:45
  • What if the one video has 2 files like video and audio and I want to overlay/mix one more audio on it how can we do it with mp4parser? – Ahmad Arslan Sep 18 '18 at 08:10
  • You might play around with it. Not tested though but this should work. `Track audioTrack1 = audio.getTracks().get(0); Track audioTrack2 = audio.getTracks().get(1); video.addTrack(audioTrack1); video.addTrack(audioTrack2);` Something like this. – Muhammad Umair Shafique Sep 18 '18 at 08:38
3

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

Ravi Vaghela
  • 3,420
  • 2
  • 23
  • 51
2

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

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);

see this link

Community
  • 1
  • 1
Haniyeh Khaksar
  • 774
  • 4
  • 20
  • Any other way to merge video without sound and record audio file and at last merge audio and video file. i want to make file which play only 10 second like dubshmash. have you seen dubshmash functionality? – Mayank Sugandhi Mar 01 '16 at 09:18
1

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

Community
  • 1
  • 1
archived
  • 647
  • 8
  • 24
  • :- Any other way to merge video without sound and record audio file and at last merge audio and video file. i want to make file which play only 10 second like dubshmash. have you seen dubshmash functionality? – Mayank Sugandhi Mar 01 '16 at 09:18
  • @MayankSugandhi sure, and it has nothing to do with MediaRecorder. [Take a look](http://stackoverflow.com/a/30136355/5908572) – archived Mar 01 '16 at 10:04
  • :- i will try .. but what about audio recording? is it possible record a song till 10 second internally , no other sound record and make file in our sd Card. – Mayank Sugandhi Mar 01 '16 at 10:18
  • @MayankSugandhi what is the source of the audio? – archived Mar 01 '16 at 10:19
  • Okay, so I looked up dubsmash. What they do is muxing prerecorded audio tracks with your video capture. So if you do have an audio to mux with video, go with MediaMuxer, just as it is suggested in the answer I gave a link to – archived Mar 01 '16 at 10:31
  • :- Please listen my requirement, first i choose song and i get file path then i open video recorder activity. in this activity there is player which process by seek bar. and when i open this activity i play song and when i click the button of video recording then video record and as well as audio. i manage 10 sec recording and it is running and recording successfully and save file in sd card. but problem is that if i cry during this video recording, my sound is also record with song. – Mayank Sugandhi Mar 01 '16 at 12:18
  • i am not able to filter my sound and audio song. second way is that i can mute audio when video recording and record the audio and video separate and merge. – Mayank Sugandhi Mar 01 '16 at 12:18
  • how can i record or cut audio song for 10 sec and make file in sd card? – Mayank Sugandhi Mar 01 '16 at 12:19
  • You can record video without sound and merge audio later without showing user that audio is being merged later. – Muhammad Umair Shafique Mar 07 '16 at 09:47