Java android中的音频文件分割
我有一个应用程序,可以从麦克风使用Java android中的音频文件分割,java,android,audio,filesplitting,Java,Android,Audio,Filesplitting,我有一个应用程序,可以从麦克风使用MediaRecorder录制音频每当有电话时,我需要能够在通话结束时保存此录制的最后x分钟-例如,分割创建的音频文件 我搜索了这个,我能找到的就是如何通过直接从文件中删除字节来拆分.wav文件。但我将文件保存在: MediaRecorder.OutputFormat.THREE_GPP带编码: MediaRecorder.OutputFormat.AMR\u NB我没有找到分割这种类型文件的方法 这是我的代码: public class Recorde
MediaRecorder录制音频
每当有电话时,我需要能够在通话结束时保存此录制的最后x分钟-例如,分割创建的音频文件
我搜索了这个,我能找到的就是如何通过直接从文件中删除字节来拆分.wav
文件。但我将文件保存在:
MediaRecorder.OutputFormat.THREE_GPP
带编码:
MediaRecorder.OutputFormat.AMR\u 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
,并将需要的部分保存在单独的文件中
另外,我对直接操作字节和文件一无所知,所以请详细说明这是否是您解决问题的方法
提前感谢使用FFMPEG库剪切/分割任何媒体文件的部分。顺便说一下,编译FFMPEG是另一个困难的部分,但您可以使用预编译库,如 下面是一个关于如何编译和使用FFMPEG命令的示例:。从示例中,我可以按以下方式拆分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();
}
}
}
这不是一个非常简单的操作。音频文件有一个标题,当你试图从一开始就删除某些内容时,你应该注意始终将标题移动到新的位置…这是一个很好的询问方法的理由:)嗨。您应该在此处编写解决方案的最重要部分,因为链接指向的内容可能会在将来消失。i get java.io.IOException:Cannot run program”/data/user/0/sample.user.com.speakerrecognitionsampleapp/files/ffmpeg::error=2,没有这样的文件或目录。知道为什么吗?使用相同的代码分割音频文件,但响应“ffmpeg版本n3.0.1版权(c)2000-2016 ffmpeg开发者”失败。请帮助