2

我有一个应用程序,可以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按时间拆分并将我需要的部分保存在单独的文件中?

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

提前致谢

4

1 回答 1

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();
            }
        }
    }
于 2016-04-25T07:00:43.760 回答