使用android ffmpeg android混合2个音频文件

时间:2014-06-04 09:48:40

标签: android audio ffmpeg sox

我正在开发一个用于混合2个音频文件的Android应用程序。我使用android ffmpeg。我使用下面的lib。来自GitHub        https://github.com/guardianproject/android-ffmpeg-java

我使用以下代码混合活动中的2个音频文件。

try {
             File fileAppRoot = new File(getApplicationInfo().dataDir);
             SoxController sxCon = new SoxController(fileAppRoot, new ShellUtils.ShellCallback() {
                    @Override
                    public void shellOut(String shellLine) {

                        System.out.println(shellLine);
                    }

                    @Override
                    public void processComplete(int exitValue) {

                    System.out.println("hello");
                    }
                });
             List<String> files=new ArrayList<String>();
             files.add(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Testing/me.mp3");
             files.add(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Testing/il.mp3");
             sxCon.combineMix(files,Environment.getExternalStorageDirectory().getAbsolutePath()+"/Testing/ial.mp3");

但是这会在processComplete上返回退出值2,并且没有为混合音频生成新文件。 这将在日志中返回以下问题 没有文件扩展名的处理程序`mp3&#39;

感谢您提供任何帮助..

2 个答案:

答案 0 :(得分:0)

你不能将文件mp3与这个库混合。

它只能混合文件“.wave”。

让我们将您的mp3文件转换为wave文件,然后使用此lib来混合文件wave。

我希望这个回复对你有好处。

谢谢,

答案 1 :(得分:0)

https://github.com/bravobit/FFmpeg-Android

实现' nl.bravobit:android-ffmpeg:1.1.7 '

布尔布尔mergeAudio(最终上下文上下文,File [] voiceFile,字符串file_name){

    final ProgressDialog asyncDialog = new ProgressDialog(context);
    asyncDialog.setMessage("Audio Merging Start..");
    asyncDialog.setCancelable(false);

    final boolean[] isSuccess = {false};
    if (file_name != null) {
        file_name = Environment.getExternalStorageDirectory() + "/podmod/" + file_name + "_.mp3";

    } else {
        file_name = getMusicFilename();
    }
    File ffmpegFile = new File(file_name);
    if (ffmpegFile.exists()) {
        ffmpegFile.delete();
    }

    for (File f : voiceFile) {
        if (!f.exists()) {
            Log.d("AudioMergingFailure", "File ot Exist");
            return isSuccess[0];
        }
    }
    String s = "";
    String s_index = "";
    String fileSize = "n=" + voiceFile.length;
    for (int i = 0; i < voiceFile.length; i++) {
        s = s + "-i@" + voiceFile[i].getPath() + "@";
        s_index = s_index + "[" + i + ":0]";

    }

    String str_cmd = s + "-filter_complex@" + s_index + "concat=" + fileSize + ":v=0:a=1[out]@-map@[out]@" + file_name;
    Log.d("str_cmd", str_cmd);

    String[] cmd = str_cmd.split("@");
    final String finalFile_name = file_name;
    try {
        if (FFmpeg.getInstance(context).isSupported()) {
            FFmpeg ffmpeg = FFmpeg.getInstance(context);
            // to execute "ffmpeg -version" command you just need to pass "-version"
            ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
                @Override
                public void onStart() {
                    asyncDialog.show();
                }

                @Override
                public void onProgress(String message) {
                }

                @Override
                public void onFailure(String message) {
                    Log.d("AudioMergingFailure", message);
                    asyncDialog.dismiss();
                    Toast.makeText(context, "Audio Merging Failed", 
                    Toast.LENGTH_LONG).show();

                }

                @Override
                public void onSuccess(String message) {
                    asyncDialog.dismiss();


                    Log.v("onSuccess", message);

                    File ffmpegFile_ = new File(finalFile_name);
                    Toast.makeText(context, "Audio onSuccess", 
                    Toast.LENGTH_LONG).show();


                    isSuccess[0] = true;
                }

                @Override
                public void onFinish() {

                    asyncDialog.dismiss();

                }

            });


        } else {

            asyncDialog.dismiss();

        }


    } catch (Exception e) {
        asyncDialog.dismiss();
        Log.d("NotException_", e.getMessage());
    }
    return isSuccess[0];
}

public static String getMusicFilename() {
    return Environment.getExternalStorageDirectory() + "/podmod/Merged_Audio_" + getRandomNumber(0, 100000) + ".mp3";
}