MediaHandleActivity接入基类Activity

MediaHandleActivity接入基类Activity
pull/107/head
xufulong 5 years ago
parent 8157a2d6e0
commit 2e7d905393
  1. 8
      app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java
  2. 100
      app/src/main/java/com/frank/ffmpeg/activity/MediaHandleActivity.java
  3. 8
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
  4. 16
      app/src/main/res/layout/activity_media_handle.xml

@ -30,7 +30,7 @@ public class AudioHandleActivity extends BaseActivity {
private String appendFile = PATH + File.separator + "test.mp3"; private String appendFile = PATH + File.separator + "test.mp3";
private final static int MSG_BEGIN = 311; private final static int MSG_BEGIN = 311;
private final static int MSG_FINISH = 312; private final static int MSG_FINISH = 312;
private ProgressBar progress_audio; private ProgressBar progressAudio;
private LinearLayout layoutAudioHandle; private LinearLayout layoutAudioHandle;
private int viewId; private int viewId;
@ -41,11 +41,11 @@ public class AudioHandleActivity extends BaseActivity {
super.handleMessage(msg); super.handleMessage(msg);
switch (msg.what) { switch (msg.what) {
case MSG_BEGIN: case MSG_BEGIN:
progress_audio.setVisibility(View.VISIBLE); progressAudio.setVisibility(View.VISIBLE);
layoutAudioHandle.setVisibility(View.GONE); layoutAudioHandle.setVisibility(View.GONE);
break; break;
case MSG_FINISH: case MSG_FINISH:
progress_audio.setVisibility(View.GONE); progressAudio.setVisibility(View.GONE);
layoutAudioHandle.setVisibility(View.VISIBLE); layoutAudioHandle.setVisibility(View.VISIBLE);
break; break;
default: default:
@ -68,7 +68,7 @@ public class AudioHandleActivity extends BaseActivity {
} }
private void initView() { private void initView() {
progress_audio = getView(R.id.progress_audio); progressAudio = getView(R.id.progress_audio);
layoutAudioHandle = getView(R.id.layout_audio_handle); layoutAudioHandle = getView(R.id.layout_audio_handle);
initViewsWithClick( initViewsWithClick(
R.id.btn_transform, R.id.btn_transform,

@ -7,9 +7,9 @@ import android.os.Bundle;
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.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 com.frank.ffmpeg.FFmpegCmd; import com.frank.ffmpeg.FFmpegCmd;
import com.frank.ffmpeg.R; import com.frank.ffmpeg.R;
@ -22,18 +22,19 @@ import java.io.File;
* 使用ffmpeg进行音视频合成与分离 * 使用ffmpeg进行音视频合成与分离
* Created by frank on 2018/1/23. * Created by frank on 2018/1/23.
*/ */
public class MediaHandleActivity extends AppCompatActivity implements View.OnClickListener{ public class MediaHandleActivity extends BaseActivity {
private final static String TAG = MediaHandleActivity.class.getSimpleName(); private final static String TAG = MediaHandleActivity.class.getSimpleName();
private static final String PATH = Environment.getExternalStorageDirectory().getPath(); private static final String PATH = Environment.getExternalStorageDirectory().getPath();
private String srcFile = PATH + File.separator + "hello.mp4"; private String videoFile;
private String videoFile = PATH + File.separator + "flash-tree.mp4";//flash-tree.mp4 private String temp = PATH + File.separator + "temp.mp4";
String temp = PATH + File.separator + "temp.mp4";
private boolean isMux; private boolean isMux;
private final static int MSG_MUX = 100; private final static int MSG_MUX = 100;
private final static int MSG_BEGIN = 101; private final static int MSG_BEGIN = 101;
private final static int MSG_FINISH = 102; private final static int MSG_FINISH = 102;
private ProgressBar progress_media; private ProgressBar progressMedia;
private int viewId;
private LinearLayout layoutMediaHandle;
@SuppressLint("HandlerLeak") @SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){ private Handler mHandler = new Handler(){
@ -73,12 +74,12 @@ public class MediaHandleActivity extends AppCompatActivity implements View.OnCli
} }
break; break;
case MSG_BEGIN: case MSG_BEGIN:
progress_media.setVisibility(View.VISIBLE); progressMedia.setVisibility(View.VISIBLE);
setGone(); layoutMediaHandle.setVisibility(View.GONE);
break; break;
case MSG_FINISH: case MSG_FINISH:
progress_media.setVisibility(View.GONE); progressMedia.setVisibility(View.GONE);
setVisible(); layoutMediaHandle.setVisibility(View.VISIBLE);
break; break;
default: default:
break; break;
@ -86,83 +87,70 @@ public class MediaHandleActivity extends AppCompatActivity implements View.OnCli
} }
}; };
@Override
int getLayoutId() {
return R.layout.activity_media_handle;
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_handle);
hideActionBar();
initView(); initView();
} }
private void initView() { private void initView() {
progress_media = (ProgressBar) findViewById(R.id.progress_media) ; progressMedia = getView(R.id.progress_media);
findViewById(R.id.btn_mux).setOnClickListener(this); layoutMediaHandle = getView(R.id.layout_media_handle);
findViewById(R.id.btn_extract_audio).setOnClickListener(this); initViewsWithClick(
findViewById(R.id.btn_extract_video).setOnClickListener(this); R.id.btn_mux,
R.id.btn_extract_audio,
R.id.btn_extract_video
);
} }
private void setVisible() { @Override
findViewById(R.id.btn_mux).setVisibility(View.VISIBLE); public void onViewClick(View view) {
findViewById(R.id.btn_extract_audio).setVisibility(View.VISIBLE); viewId = view.getId();
findViewById(R.id.btn_extract_video).setVisibility(View.VISIBLE); selectFile();
}
private void setGone() {
findViewById(R.id.btn_mux).setVisibility(View.GONE);
findViewById(R.id.btn_extract_audio).setVisibility(View.GONE);
findViewById(R.id.btn_extract_video).setVisibility(View.GONE);
} }
@Override @Override
public void onClick(View v) { void onSelectedFile(String filePath) {
int handleType; doHandleMedia(filePath);
switch (v.getId()){
case R.id.btn_mux:
handleType = 0;
break;
case R.id.btn_extract_audio:
handleType = 1;
break;
case R.id.btn_extract_video:
handleType = 2;
break;
default:
handleType = 0;
break;
}
doHandleMedia(handleType);
} }
/** /**
* 调用ffmpeg处理音视频 * 调用ffmpeg处理音视频
* @param handleType handleType * @param srcFile srcFile
*/ */
private void doHandleMedia(int handleType){ private void doHandleMedia(String srcFile) {
String[] commandLine = null; String[] commandLine = null;
switch (handleType){ if (!FileUtil.checkFileExist(srcFile)){
case 0://音视频合成 return;
if (!FileUtil.checkFileExist(videoFile)){ }
if (!FileUtil.isVideo(srcFile)) {
showToast(getString(R.string.wrong_video_format));
return; return;
} }
switch (viewId){
case R.id.btn_mux://音视频合成
try { try {
//视频文件有音频,先把纯视频文件抽取出来 //视频文件有音频,先把纯视频文件抽取出来
commandLine = FFmpegUtil.extractVideo(videoFile, temp); videoFile = srcFile;
commandLine = FFmpegUtil.extractVideo(srcFile, temp);
isMux = true; isMux = true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
break; break;
case 1://提取音频 case R.id.btn_extract_audio://提取音频
if (!FileUtil.checkFileExist(srcFile)){
return;
}
String extractAudio = PATH + File.separator + "extractAudio.aac"; String extractAudio = PATH + File.separator + "extractAudio.aac";
commandLine = FFmpegUtil.extractAudio(srcFile, extractAudio); commandLine = FFmpegUtil.extractAudio(srcFile, extractAudio);
break; break;
case 2://提取视频 case R.id.btn_extract_video://提取视频
if (!FileUtil.checkFileExist(srcFile)){
return;
}
String extractVideo = PATH + File.separator + "extractVideo.mp4"; String extractVideo = PATH + File.separator + "extractVideo.mp4";
commandLine = FFmpegUtil.extractVideo(srcFile, extractVideo); commandLine = FFmpegUtil.extractVideo(srcFile, extractVideo);
break; break;

@ -25,7 +25,7 @@ public class VideoHandleActivity extends BaseActivity {
private static final String PATH = Environment.getExternalStorageDirectory().getPath(); private static final String PATH = Environment.getExternalStorageDirectory().getPath();
private ProgressBar progress_video; private ProgressBar progressVideo;
private LinearLayout layoutVideoHandle; private LinearLayout layoutVideoHandle;
private int viewId; private int viewId;
@ -36,11 +36,11 @@ public class VideoHandleActivity extends BaseActivity {
super.handleMessage(msg); super.handleMessage(msg);
switch (msg.what){ switch (msg.what){
case MSG_BEGIN: case MSG_BEGIN:
progress_video.setVisibility(View.VISIBLE); progressVideo.setVisibility(View.VISIBLE);
layoutVideoHandle.setVisibility(View.GONE); layoutVideoHandle.setVisibility(View.GONE);
break; break;
case MSG_FINISH: case MSG_FINISH:
progress_video.setVisibility(View.GONE); progressVideo.setVisibility(View.GONE);
layoutVideoHandle.setVisibility(View.VISIBLE); layoutVideoHandle.setVisibility(View.VISIBLE);
break; break;
default: default:
@ -63,7 +63,7 @@ public class VideoHandleActivity extends BaseActivity {
} }
private void intView() { private void intView() {
progress_video = getView(R.id.progress_video); progressVideo = getView(R.id.progress_video);
layoutVideoHandle = getView(R.id.layout_video_handle); layoutVideoHandle = getView(R.id.layout_video_handle);
initViewsWithClick( initViewsWithClick(
R.id.btn_video_transform, R.id.btn_video_transform,

@ -5,11 +5,16 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="com.frank.ffmpeg.activity.MediaHandleActivity"> tools:context="com.frank.ffmpeg.activity.MediaHandleActivity">
<LinearLayout
android:id="@+id/layout_media_handle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button <Button
android:id="@+id/btn_mux" android:id="@+id/btn_mux"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp" android:layout_marginTop="120dp"
android:text="@string/media_mux"/> android:text="@string/media_mux"/>
@ -18,18 +23,15 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/media_extra_audio" android:text="@string/media_extra_audio"
android:layout_marginTop="10dp" android:layout_marginTop="10dp" />
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_mux"/>
<Button <Button
android:id="@+id/btn_extract_video" android:id="@+id/btn_extract_video"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/media_extract_video" android:text="@string/media_extract_video"
android:layout_marginTop="10dp" android:layout_marginTop="10dp" />
android:layout_centerHorizontal="true" </LinearLayout>
android:layout_below="@+id/btn_extract_audio"/>
<ProgressBar <ProgressBar
android:id="@+id/progress_media" android:id="@+id/progress_media"

Loading…
Cancel
Save