在android中拆分音频文件

时间:2015-09-08 16:01:31

标签: java android audio filesplitting

我有一个应用程序,只要有电话呼叫就使用MediaRecorder from MIC录制音频,我需要能够在通话完成时保存此录音的最后x分钟 - 例如将录制创建的音频文件拆分。

我搜索了这个,我找到的是如何通过直接从文件中删除字节来拆分.wav文件。但我将文件保存在: MediaRecorder.OutputFormat.THREE_GPP带编码: MediaRecorder.OutputFormat.AMR_NB我没有找到分割此类文件的方法。

这是我的代码:

    public class Recorder
    {
    private MediaRecorder myRecorder;
    public String outputFile = null;
    private Context context = null;

    public Recorder(Context context_app)
    {
        context = context_app;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss",      Locale.getDefault());
        df.setTimeZone(TimeZone.getDefault());
        String time = df.format(new Date());
        outputFile = Environment.getExternalStorageDirectory().
                getAbsolutePath() + "/"+time+".3gpp"; //this is the folder in which your Audio file willl save
        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myRecorder.setOutputFile(outputFile);
    }


    public void start() {
        try {
            myRecorder.prepare();
            myRecorder.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Toast.makeText(context, e.getMessage(),
                    Toast.LENGTH_SHORT).show();
            // prepare() fails
            e.printStackTrace();
        }

        Toast.makeText(context, "Start recording...",
                Toast.LENGTH_SHORT).show();
    }

    public void stop() {
        try {
            myRecorder.stop();
            myRecorder.reset();
            myRecorder.release();
            myRecorder = null;

            Toast.makeText(context, "Stop recording...",
                    Toast.LENGTH_SHORT).show();
        } catch (RuntimeException e) {
            // no valid audio/video data has been received
            e.printStackTrace();
        }
    }  
}

如何按时间拆分THREE_GPP并将我需要的部分保存在单独的文件中?

另外,我对直接操作字节和文件一无所知,所以请详细说明是否是你解决它的方式。

提前致谢

1 个答案:

答案 0 :(得分:2)

使用FFMPEG库剪切/拆分任何媒体文件的一部分。顺便说一下,编译FFMPEG是另一个难点,但你可以使用预编译的库,如https://github.com/WritingMinds/ffmpeg-android-java

以下是有关如何编译&使用FFMPEG命令:https://github.com/JesusMartinAlonso/Video4Share。从示例中,我可以按以下方式分割3gp文件:

// ffmpeg command sample: "-y -i inputPath -ss 00:00:02 -codec copy -t 00:00:06 outputPath"
    public String getSplitCommand(String inputFileUrl, String outputFileUrl,
                                       String start, String end) {
        if ((TextUtils.isEmpty(inputFileUrl) && (TextUtils.isEmpty(outputFileUrl)))) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("-y ")
                .append("-i ")
                .append(inputFileUrl).append(" ")
                .append("-ss ")
                .append(start).append(" ")
                .append("-codec ")
                .append("copy ")
                .append("-t ")
                .append(end).append(" ")
                .append(outputFileUrl);
        return stringBuilder.toString();
    }

public void executeBinaryCommand(FFmpeg ffmpeg, ProgressDialog progressDialog, final String command) {
        if (!TextUtils.isEmpty(command)) {
            try {
                ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                    @Override
                    public void onFailure(String response) {
                        progressDialog.setMessage(response);
                    }

                    @Override
                    public void onSuccess(String response) {
                        progressDialog.setMessage(response);

                    }

                    @Override
                    public void onProgress(String response) {
                        progressDialog.setMessage(response);
                    }

                    @Override
                    public void onStart() {
                        progressDialog.show();
                    }

                    @Override
                    public void onFinish() {
                        progressDialog.dismiss();
                    }
                });
            } catch (FFmpegCommandAlreadyRunningException exception) {
                exception.printStackTrace();
            }
        }
    }