在android中合并音频文件和捕获的视频

问题描述:

是否有可能将音频和视频文件合并到一个文件中。我的 需求是:一个可以录制视频(mp4)的android应用程序,然后我的本地文件夹中有一些音频文件(mp3),我想将它添加到通过camcoder捕获的视频中。所以最终的效果就像播放视频一样,并且将会听到要在内部合并的添加音频。我该怎么做? P.S.合并必须在内部完成.Camcorder不会录制任何音频。在android中合并音频文件和捕获的视频

请帮

+0

我使用MP4解析器的例子,但它不是帮助很大, – user2724486

我不能使用MP3 files.Instead成功我使用的MP4文件的背景音乐。如果你有兴趣来处理MP4文件,下面的代码片段可以帮助你

String root = Environment.getExternalStorageDirectory().toString(); 
      String audio = root +"/test.mp4"; 
      String video = root +"/myvideo.mp4"; 
      String output = root+ "/ouputVideo.mp4"; 
      Log.e("FILE", "audio:"+audio + " video:"+video+ " out:"+output); 
      mux(video, audio, output); 



public boolean mux(String videoFile, String audioFile, String outputFile) { 
    Movie video; 
    try { 
     video = new MovieCreator().build(videoFile); 
    } catch (RuntimeException e) { 
     e.printStackTrace(); 
     return false; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    Movie audio; 

    try { 
     audio = new MovieCreator().build(audioFile); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } catch (NullPointerException e) { 

     e.printStackTrace(); 
     return false; 
    } 

    Track audioTrack = audio.getTracks().get(1); 

    //video.getTracks().remove(1); 
    video.addTrack(audioTrack); 

    Container out = new DefaultMp4Builder().build(video); 

    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(outputFile); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos); 
    try { 
     out.writeContainer(byteBufferByteChannel); 
     byteBufferByteChannel.close(); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 
+0

感谢,所有的MP3文件不会在这里工作。只有少数是工作任何想法? – Sandy09

+0

MovieCreator类在哪里? –