Split audio file in android

99封情书 提交于 2019-12-10 23:10:01

问题


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.

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.

This is my code:

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

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.

Thanks in advance


回答1:


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 .

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


来源:https://stackoverflow.com/questions/32462384/split-audio-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!