增加PCM裸流音频文件的合并

pull/107/head
frank 7 years ago
parent 5fe02096fa
commit bc864c5e2f
  1. 21
      app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java
  2. 69
      app/src/main/java/com/frank/ffmpeg/util/FileUtil.java
  3. 11
      app/src/main/res/layout/activity_audio_handle.xml
  4. 1
      app/src/main/res/values/strings.xml

@ -15,6 +15,7 @@ import com.frank.ffmpeg.AudioPlayer;
import com.frank.ffmpeg.FFmpegCmd;
import com.frank.ffmpeg.R;
import com.frank.ffmpeg.util.FFmpegUtil;
import com.frank.ffmpeg.util.FileUtil;
/**
* 使用ffmpeg处理音频
@ -68,6 +69,7 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
findViewById(R.id.btn_play_audio).setOnClickListener(this);
findViewById(R.id.btn_play_opensl).setOnClickListener(this);
findViewById(R.id.btn_audio_encode).setOnClickListener(this);
findViewById(R.id.btn_pcm_concat).setOnClickListener(this);
}
private void setVisible() {
@ -78,6 +80,7 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
findViewById(R.id.btn_play_audio).setVisibility(View.VISIBLE);
findViewById(R.id.btn_play_opensl).setVisibility(View.VISIBLE);
findViewById(R.id.btn_audio_encode).setVisibility(View.VISIBLE);
findViewById(R.id.btn_pcm_concat).setVisibility(View.VISIBLE);
}
private void setGone() {
@ -88,6 +91,7 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
findViewById(R.id.btn_play_audio).setVisibility(View.GONE);
findViewById(R.id.btn_play_opensl).setVisibility(View.GONE);
findViewById(R.id.btn_audio_encode).setVisibility(View.GONE);
findViewById(R.id.btn_pcm_concat).setVisibility(View.GONE);
}
@Override
@ -115,6 +119,9 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
case R.id.btn_audio_encode:
handleType = 6;
break;
case R.id.btn_pcm_concat:
handleType = 7;
break;
default:
handleType = 0;
break;
@ -137,7 +144,7 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
String cutFile = PATH + File.separator + "cut.mp3";
commandLine = FFmpegUtil.cutAudio(srcFile, 10, 15, cutFile);
break;
case 2://合并
case 2://合并,支持MP3、AAC、AMR等,不支持PCM裸流,不支持WAV(PCM裸流加音频头)
String concatFile = PATH + File.separator + "concat.mp3";
commandLine = FFmpegUtil.concatAudio(srcFile, appendFile, concatFile);
break;
@ -163,14 +170,22 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
return;
case 6://音频编码
//可编码成WAV、AAC。如果需要编码成MP3、AMR,ffmpeg需要重新编译,把MP3、AMR库enable
String pcmFile = PATH + File.separator + "audio.pcm";
String wavFile = PATH + File.separator + "output.wav";
String pcmFile = PATH + File.separator + "concat.pcm";
String wavFile = PATH + File.separator + "new.wav";
//pcm数据的采样率,一般采样率为8000、16000、44100
int sampleRate = 8000;
//pcm数据的声道,单声道为1,立体声道为2
int channel = 1;
commandLine = FFmpegUtil.encodeAudio(pcmFile, wavFile, sampleRate, channel);
break;
case 7://PCM裸流音频文件合并
String srcPCM = PATH + File.separator + "audio.pcm";//第一个pcm文件
String appendPCM = PATH + File.separator + "audio.pcm";//第二个pcm文件
String concatPCM = PATH + File.separator + "concat.pcm";//合并后的文件
mHandler.obtainMessage(MSG_BEGIN).sendToTarget();
FileUtil.concatFile(srcPCM, appendPCM, concatPCM);
mHandler.obtainMessage(MSG_FINISH).sendToTarget();
return;
default:
break;
}

@ -0,0 +1,69 @@
package com.frank.ffmpeg.util;
import android.text.TextUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件工具类
* Created by frank on 2018/5/9.
*/
public class FileUtil {
public static boolean concatFile(String srcFilePath, String appendFilePath, String concatFilePath){
if(TextUtils.isEmpty(srcFilePath)
|| TextUtils.isEmpty(appendFilePath)
|| TextUtils.isEmpty(concatFilePath)){
return false;
}
File srcFile = new File(srcFilePath);
if(!srcFile.exists()){
return false;
}
File appendFile = new File(appendFilePath);
if(!appendFile.exists()){
return false;
}
FileOutputStream outputStream = null;
FileInputStream inputStream1 = null, inputStream2 = null;
try {
inputStream1 = new FileInputStream(srcFile);
inputStream2 = new FileInputStream(appendFile);
outputStream = new FileOutputStream(new File(concatFilePath));
byte[] data = new byte[1024];
int len;
while ((len = inputStream1.read(data)) > 0){
outputStream.write(data, 0, len);
}
outputStream.flush();
while ((len = inputStream2.read(data)) > 0){
outputStream.write(data, 0, len);
}
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(inputStream1 != null){
inputStream1.close();
}
if(inputStream2 != null){
inputStream2.close();
}
if(outputStream != null){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}

@ -11,7 +11,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/audio_transform"
android:layout_marginTop="80dp"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"/>
<Button
@ -75,4 +75,13 @@
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/btn_pcm_concat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pcm_concat"
android:layout_below="@+id/btn_audio_encode"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

@ -7,6 +7,7 @@
<string name="audio_play">音频解码AudioTrack播放</string>
<string name="audio_opensl">音频解码OpenSL播放</string>
<string name="audio_encode">音频编码</string>
<string name="pcm_concat">PCM合并</string>
<string name="audio_handle">音频处理</string>
<string name="media_handle">音视频处理</string>

Loading…
Cancel
Save