How to record video with a certain sound programmatically in Android?

I created functionality for recording videos in my application.

When I play a song, this song is recorded from the video and a video file is created, similar to the dubshmash application.

Now the problem that I am facing is that other voices, such as next to the sounds, are also recorded. The song file is recorded on the video recording screen, and I will play the song when the video recording action starts.

How can I record a recording of my application with video only?

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

Is there any solution in the sound source set as the speaker because the sound of the song goes through the speaker? if this is another way, answer me.

+5
source share
5 answers

If you want to work with video, you need to use the FFMPEG library

Perhaps you can work with the video.

What I already answered on How to use ffmpeg in android studio? see LINK . Go step by step and import into your peoj

+1
source

You can record silent video and combine audio later using the mp4 parser as follows:

 /* * @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 the following dependency

 compile 'com.googlecode.mp4parser:isoparser:1.0.5.4' 
+6
source

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

 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 

see this link

+2
source

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

Please note that this is a security issue that restricts access to the audio output of other applications, so it is very unlikely that it can be achieved directly.

See this answer

+1
source

You can also use Javacv (FFmpeg java wrapper) to combine audio and video. You will need to complete this task either in async or in a stream.

 FrameGrabber videoGrabber = new FFmpegFrameGrabber(videoPath); Frame videoFrame=null; FrameRecorder mFrameRecorder; videoGrabber.start(); mFrameRecorder = new FFmpegFrameRecorder(OutputPath, videoGrabber .getImageWidth(), videoGrabber.getImageHeight(), 2); mFrameRecorder.setVideoQuality(1); mFrameRecorder.setFormat("mp4"); mFrameRecorder.setFrameRate(videoGrabber.getFrameRate()); mFrameRecorder.start(); while ((videoFrame = videoGrabber.grabFrame())!=null) { videoFrame = videoGrabber.grabFrame(); mFrameRecorder.record(videoFrame); } mFrameRecorder.stop(); mFrameRecorder.release(); videoGrabber.stop(); } catch (FrameGrabber.Exception e) { e.printStackTrace(); } catch (FrameRecorder.Exception e) { e.printStackTrace(); } 

At the application level build.gradle

Add these dependencies

 compile'org.bytedeco:javacv:1.0' compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-arm' compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-arm' 
0
source

Source: https://habr.com/ru/post/1243292/


All Articles