在Android中分割音频文件 [英] Split audio file in android

查看:523
本文介绍了在Android中分割音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,只要有电话出现,它就会使用MediaRecorder from MIC记录音频,通话结束后,我需要能够保存此记录的最后x分钟-例如分割录制的音频文件.

I have an app that records audio using MediaRecorder from MIC whenever a phone call is present, i need to be able to save the last x minutes from this recording when the call is finished - e.g. split the audio file the recording created.

我进行了搜索,我所能找到的就是如何通过直接从文件中删除字节来拆分.wav文件.但我将文件保存在: MediaRecorder.OutputFormat.THREE_GPP编码: MediaRecorder.OutputFormat.AMR_NB,但我找不到拆分此类文件的方法.

I searched for this and all i could find is how to split a .wav file by directly deleting bytes from the file. But i am saving the file in: MediaRecorder.OutputFormat.THREE_GPP with encoding: MediaRecorder.OutputFormat.AMR_NB and I didn't find a way to split this type of file.

这是我的代码:

    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并将所需的零件保存在单独的文件中?

How can i split the THREE_GPP by time and save the part I need in a separate file?

此外,我对直接操作字节和文件一无所知,因此请详细说明这是否是解决问题的方法.

Also, i know nothing about manipulating bytes and files directly,so please elaborate if it is the way you solved it.

预先感谢

推荐答案

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

Use FFMPEG library to cut/split the part of any media file. By the way, compiling FFMPEG is another hard part but you can use pre-compiled library like https://github.com/WritingMinds/ffmpeg-android-java .

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

Here is a sample about how to compile & use FFMPEG command: https://github.com/JesusMartinAlonso/Video4Share . From the sample, I could able to split 3gp files as the following way:

// 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();
            }
        }
    }

这篇关于在Android中分割音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆