AudioHandleActivity接入基类Activity

AudioHandleActivity接入基类Activity
pull/107/head
xufulong 5 years ago
parent 545fcb6182
commit b9e149ba28
  1. 137
      app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java
  2. 6
      app/src/main/java/com/frank/ffmpeg/activity/BaseActivity.java
  3. 53
      app/src/main/java/com/frank/ffmpeg/util/FileUtil.java
  4. 125
      app/src/main/res/layout/activity_audio_handle.xml
  5. 2
      app/src/main/res/layout/activity_main.xml
  6. 2
      app/src/main/res/values/strings.xml

@ -4,10 +4,10 @@ import android.annotation.SuppressLint;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import java.io.File; import java.io.File;
@ -23,29 +23,30 @@ import com.frank.ffmpeg.util.FileUtil;
* Created by frank on 2018/1/23. * Created by frank on 2018/1/23.
*/ */
public class AudioHandleActivity extends AppCompatActivity implements View.OnClickListener{ public class AudioHandleActivity extends BaseActivity {
private final static String TAG = AudioHandleActivity.class.getSimpleName(); private final static String TAG = AudioHandleActivity.class.getSimpleName();
private final static String PATH = Environment.getExternalStorageDirectory().getPath(); private final static String PATH = Environment.getExternalStorageDirectory().getPath();
private String srcFile = PATH + File.separator + "tiger.mp3";
private String appendFile = PATH + File.separator + "test.mp3"; private String appendFile = PATH + File.separator + "test.mp3";
private final static int MSG_BEGIN = 11; private final static int MSG_BEGIN = 311;
private final static int MSG_FINISH = 12; private final static int MSG_FINISH = 312;
private ProgressBar progress_audio; private ProgressBar progress_audio;
private LinearLayout layoutAudioHandle;
private int viewId;
@SuppressLint("HandlerLeak") @SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){ private Handler mHandler = new Handler(){
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
super.handleMessage(msg); super.handleMessage(msg);
switch (msg.what){ switch (msg.what) {
case MSG_BEGIN: case MSG_BEGIN:
progress_audio.setVisibility(View.VISIBLE); progress_audio.setVisibility(View.VISIBLE);
setGone(); layoutAudioHandle.setVisibility(View.GONE);
break; break;
case MSG_FINISH: case MSG_FINISH:
progress_audio.setVisibility(View.GONE); progress_audio.setVisibility(View.GONE);
setVisible(); layoutAudioHandle.setVisibility(View.VISIBLE);
break; break;
default: default:
break; break;
@ -53,94 +54,58 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
} }
}; };
@Override
int getLayoutId() {
return R.layout.activity_audio_handle;
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_handle);
initView(); initView();
} }
private void initView() { private void initView() {
progress_audio = (ProgressBar) findViewById(R.id.progress_audio); progress_audio = getView(R.id.progress_audio);
findViewById(R.id.btn_transform).setOnClickListener(this); layoutAudioHandle = getView(R.id.layout_audio_handle);
findViewById(R.id.btn_cut).setOnClickListener(this); initViewsWithClick(
findViewById(R.id.btn_concat).setOnClickListener(this); R.id.btn_transform,
findViewById(R.id.btn_mix).setOnClickListener(this); R.id.btn_cut,
findViewById(R.id.btn_play_audio).setOnClickListener(this); R.id.btn_concat,
findViewById(R.id.btn_play_opensl).setOnClickListener(this); R.id.btn_mix,
findViewById(R.id.btn_audio_encode).setOnClickListener(this); R.id.btn_play_audio,
findViewById(R.id.btn_pcm_concat).setOnClickListener(this); R.id.btn_play_opensl,
} R.id.btn_audio_encode,
R.id.btn_pcm_concat);
private void setVisible() {
findViewById(R.id.btn_transform).setVisibility(View.VISIBLE);
findViewById(R.id.btn_cut).setVisibility(View.VISIBLE);
findViewById(R.id.btn_concat).setVisibility(View.VISIBLE);
findViewById(R.id.btn_mix).setVisibility(View.VISIBLE);
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() { @Override
findViewById(R.id.btn_transform).setVisibility(View.GONE); public void onViewClick(View view) {
findViewById(R.id.btn_cut).setVisibility(View.GONE); viewId = view.getId();
findViewById(R.id.btn_concat).setVisibility(View.GONE); selectFile();
findViewById(R.id.btn_mix).setVisibility(View.GONE);
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 @Override
public void onClick(View v) { void onSelectedFile(String filePath) {
int handleType; doHandleAudio(filePath);
switch (v.getId()){
case R.id.btn_transform:
handleType = 0;
break;
case R.id.btn_cut:
handleType = 1;
break;
case R.id.btn_concat:
handleType = 2;
break;
case R.id.btn_mix:
handleType = 3;
break;
case R.id.btn_play_audio:
handleType = 4;
break;
case R.id.btn_play_opensl:
handleType = 5;
break;
case R.id.btn_audio_encode:
handleType = 6;
break;
case R.id.btn_pcm_concat:
handleType = 7;
break;
default:
handleType = 0;
break;
}
doHandleAudio(handleType);
} }
/** /**
* 调用ffmpeg处理音频 * 调用ffmpeg处理音频
* @param handleType handleType * @param srcFile srcFile
*/ */
private void doHandleAudio(int handleType){ private void doHandleAudio(final String srcFile) {
String[] commandLine = null; String[] commandLine = null;
if (!FileUtil.checkFileExist(srcFile)){ if (!FileUtil.checkFileExist(srcFile)) {
return;
}
if (!FileUtil.isAudio(srcFile)) {
showToast(getString(R.string.wrong_audio_format));
return; return;
} }
switch (handleType){ switch (viewId) {
case 0://转码 case R.id.btn_transform://转码
// String transformFile = PATH + File.separator + "transform.aac"; // String transformFile = PATH + File.separator + "transform.aac";
// commandLine = FFmpegUtil.transformAudio(srcFile, transformFile); // commandLine = FFmpegUtil.transformAudio(srcFile, transformFile);
//使用mp3lame进行转码 //使用mp3lame进行转码
@ -149,25 +114,25 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
Mp3Converter mp3Converter = new Mp3Converter(); Mp3Converter mp3Converter = new Mp3Converter();
mp3Converter.convertToMp3(inputFile, transformFile); mp3Converter.convertToMp3(inputFile, transformFile);
break; break;
case 1://剪切 case R.id.btn_cut://剪切(注意原文件与剪切文件格式一致)
String cutFile = PATH + File.separator + "cut.mp3"; String cutFile = PATH + File.separator + "cut.aac";
commandLine = FFmpegUtil.cutAudio(srcFile, 10, 15, cutFile); commandLine = FFmpegUtil.cutAudio(srcFile, 10, 15, cutFile);
break; break;
case 2://合并,支持MP3、AAC、AMR等,不支持PCM裸流,不支持WAV(PCM裸流加音频头) case R.id.btn_concat://合并,支持MP3、AAC、AMR等,不支持PCM裸流,不支持WAV(PCM裸流加音频头)
if (!FileUtil.checkFileExist(appendFile)){ if (!FileUtil.checkFileExist(appendFile)){
return; return;
} }
String concatFile = PATH + File.separator + "concat.mp3"; String concatFile = PATH + File.separator + "concat.mp3";
commandLine = FFmpegUtil.concatAudio(srcFile, appendFile, concatFile); commandLine = FFmpegUtil.concatAudio(srcFile, appendFile, concatFile);
break; break;
case 3://混合 case R.id.btn_mix://混合
if (!FileUtil.checkFileExist(appendFile)){ if (!FileUtil.checkFileExist(appendFile)){
return; return;
} }
String mixFile = PATH + File.separator + "mix.aac"; String mixFile = PATH + File.separator + "mix.aac";
commandLine = FFmpegUtil.mixAudio(srcFile, appendFile, mixFile); commandLine = FFmpegUtil.mixAudio(srcFile, appendFile, mixFile);
break; break;
case 4://解码播放(AudioTrack) case R.id.btn_play_audio://解码播放(AudioTrack)
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -175,7 +140,7 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
} }
}).start(); }).start();
return; return;
case 5://解码播放(OpenSL ES) case R.id.btn_play_opensl://解码播放(OpenSL ES)
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -183,8 +148,8 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
} }
}).start(); }).start();
return; return;
case 6://音频编码 case R.id.btn_audio_encode://音频编码
//可编码成WAV、AAC。如果需要编码成MP3、AMR,ffmpeg需要重新编译,把MP3、AMR库enable //可编码成WAV、AAC。如果需要编码成MP3,ffmpeg需要重新编译,把MP3库enable
String pcmFile = PATH + File.separator + "concat.pcm"; String pcmFile = PATH + File.separator + "concat.pcm";
String wavFile = PATH + File.separator + "new.wav"; String wavFile = PATH + File.separator + "new.wav";
//pcm数据的采样率,一般采样率为8000、16000、44100 //pcm数据的采样率,一般采样率为8000、16000、44100
@ -193,7 +158,7 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
int channel = 1; int channel = 1;
commandLine = FFmpegUtil.encodeAudio(pcmFile, wavFile, sampleRate, channel); commandLine = FFmpegUtil.encodeAudio(pcmFile, wavFile, sampleRate, channel);
break; break;
case 7://PCM裸流音频文件合并 case R.id.btn_pcm_concat://PCM裸流音频文件合并
String srcPCM = PATH + File.separator + "audio.pcm";//第一个pcm文件 String srcPCM = PATH + File.separator + "audio.pcm";//第一个pcm文件
String appendPCM = PATH + File.separator + "audio.pcm";//第二个pcm文件 String appendPCM = PATH + File.separator + "audio.pcm";//第二个pcm文件
String concatPCM = PATH + File.separator + "concat.pcm";//合并后的文件 String concatPCM = PATH + File.separator + "concat.pcm";//合并后的文件
@ -215,8 +180,8 @@ public class AudioHandleActivity extends AppCompatActivity implements View.OnCli
* 执行ffmpeg命令行 * 执行ffmpeg命令行
* @param commandLine commandLine * @param commandLine commandLine
*/ */
private void executeFFmpegCmd(final String[] commandLine){ private void executeFFmpegCmd(final String[] commandLine) {
if(commandLine == null){ if(commandLine == null) {
return; return;
} }
FFmpegCmd.execute(commandLine, new FFmpegCmd.OnHandleListener() { FFmpegCmd.execute(commandLine, new FFmpegCmd.OnHandleListener() {

@ -54,12 +54,6 @@ public abstract class BaseActivity extends AppCompatActivity implements View.OnC
} }
} }
protected void initViews(int... viewIds) {
for (int viewId : viewIds) {
getView(viewId);
}
}
protected void initViewsWithClick(int... viewIds) { protected void initViewsWithClick(int... viewIds) {
for (int viewId : viewIds) { for (int viewId : viewIds) {
getView(viewId).setOnClickListener(this); getView(viewId).setOnClickListener(this);

@ -16,6 +16,27 @@ import java.io.IOException;
public class FileUtil { public class FileUtil {
private final static String TYPE_MP3 = "mp3";
private final static String TYPE_AAC = "aac";
private final static String TYPE_AMR = "amr";
private final static String TYPE_FLAC = "flac";
private final static String TYPE_M4A = "m4a";
private final static String TYPE_WMA = "wma";
private final static String TYPE_WAV = "wav";
private final static String TYPE_OGG = "ogg";
private final static String TYPE_AC3 = "ac3";
private final static String TYPE_MP4 = "mp4";
private final static String TYPE_MKV = "mkv";
private final static String TYPE_WEBM = "webm";
private final static String TYPE_AVI = "avi";
private final static String TYPE_WMV = "wmv";
private final static String TYPE_FLV = "flv";
private final static String TYPE_TS = "ts";
private final static String TYPE_M3U8 = "m3u8";
private final static String TYPE_3GP = "3gp";
private final static String TYPE_MOV = "mov";
public static boolean concatFile(String srcFilePath, String appendFilePath, String concatFilePath){ public static boolean concatFile(String srcFilePath, String appendFilePath, String concatFilePath){
if(TextUtils.isEmpty(srcFilePath) if(TextUtils.isEmpty(srcFilePath)
|| TextUtils.isEmpty(appendFilePath) || TextUtils.isEmpty(appendFilePath)
@ -75,7 +96,6 @@ public class FileUtil {
*/ */
public static boolean checkFileExist(String path){ public static boolean checkFileExist(String path){
if (TextUtils.isEmpty(path)) { if (TextUtils.isEmpty(path)) {
Log.e("FileUtil", path + "is null!");
return false; return false;
} }
File file = new File(path); File file = new File(path);
@ -86,4 +106,35 @@ public class FileUtil {
return true; return true;
} }
public static boolean isAudio(String path) {
if (TextUtils.isEmpty(path)) {
return false;
}
return path.endsWith(TYPE_MP3)
|| path.endsWith(TYPE_AAC)
|| path.endsWith(TYPE_AMR)
|| path.endsWith(TYPE_FLAC)
|| path.endsWith(TYPE_M4A)
|| path.endsWith(TYPE_WMA)
|| path.endsWith(TYPE_WAV)
|| path.endsWith(TYPE_OGG)
|| path.endsWith(TYPE_AC3);
}
public static boolean isVideo(String path) {
if (TextUtils.isEmpty(path)) {
return false;
}
return path.endsWith(TYPE_MP4)
|| path.endsWith(TYPE_MKV)
|| path.endsWith(TYPE_WEBM)
|| path.endsWith(TYPE_WMV)
|| path.endsWith(TYPE_AVI)
|| path.endsWith(TYPE_FLV)
|| path.endsWith(TYPE_3GP)
|| path.endsWith(TYPE_TS)
|| path.endsWith(TYPE_M3U8)
|| path.endsWith(TYPE_MOV);
}
} }

@ -3,85 +3,76 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".activity.AudioHandleActivity"> tools:context=".activity.AudioHandleActivity">
<Button <LinearLayout
android:id="@+id/btn_transform" android:id="@+id/layout_audio_handle"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/audio_transform" android:orientation="vertical"
android:layout_marginTop="40dp" android:gravity="center_horizontal">
android:layout_centerHorizontal="true"/> <Button
android:id="@+id/btn_transform"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/audio_transform"
android:layout_marginTop="40dp" />
<Button <Button
android:id="@+id/btn_cut" android:id="@+id/btn_cut"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/audio_cut" android:text="@string/audio_cut"
android:layout_below="@+id/btn_transform" android:layout_marginTop="10dp" />
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
<Button <Button
android:id="@+id/btn_concat" android:id="@+id/btn_concat"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/audio_concat" android:text="@string/audio_concat"
android:layout_below="@+id/btn_cut" android:layout_marginTop="10dp" />
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
<Button <Button
android:id="@+id/btn_mix" android:id="@+id/btn_mix"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/audio_mix" android:text="@string/audio_mix"
android:layout_below="@+id/btn_concat" android:layout_marginTop="10dp" />
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
<ProgressBar <Button
android:id="@+id/progress_audio" android:id="@+id/btn_play_audio"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:text="@string/audio_play"
android:visibility="gone"/> android:layout_marginTop="10dp" />
<Button <Button
android:id="@+id/btn_play_audio" android:id="@+id/btn_play_opensl"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/audio_play" android:text="@string/audio_opensl"
android:layout_below="@+id/btn_mix" android:layout_marginTop="10dp" />
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
<Button <Button
android:id="@+id/btn_play_opensl" android:id="@+id/btn_audio_encode"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/audio_opensl" android:text="@string/audio_encode"
android:layout_below="@+id/btn_play_audio" android:layout_marginTop="10dp" />
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
<Button <Button
android:id="@+id/btn_audio_encode" android:id="@+id/btn_pcm_concat"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/audio_encode" android:text="@string/pcm_concat"
android:layout_below="@+id/btn_play_opensl" android:layout_marginTop="10dp" />
android:layout_marginTop="10dp" </LinearLayout>
android:layout_centerHorizontal="true"/>
<Button <ProgressBar
android:id="@+id/btn_pcm_concat" android:id="@+id/progress_audio"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/pcm_concat" android:layout_centerInParent="true"
android:layout_below="@+id/btn_audio_encode" android:visibility="gone"/>
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout> </RelativeLayout>

@ -7,7 +7,7 @@
android:id="@+id/btn_audio" android:id="@+id/btn_audio"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:text="@string/audio_handle"/> android:text="@string/audio_handle"/>

@ -66,5 +66,7 @@
<string name="please_select">请选择正确文件</string> <string name="please_select">请选择正确文件</string>
<string name="file_not_found">文件不存在</string> <string name="file_not_found">文件不存在</string>
<string name="please_click_select">请点击右上角菜单选择文件</string> <string name="please_click_select">请点击右上角菜单选择文件</string>
<string name="wrong_audio_format">非音频文件</string>
<string name="wrong_video_format">非视频文件</string>
</resources> </resources>

Loading…
Cancel
Save