}
private void init(){
// 设置像素绘制格式为 RGBA_8888
SurfaceHolder holder = getHolder();
holder.setFormat(PixelFormat.RGBA_8888);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><com.haohao.ffmpeg.MySurfaceView
android:id="@+id/my_surface_view"
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AVUtils.AVCallback {
private static final String TAG = “MainActivity”;
private static final String BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar;
private String input_video_file_path = BASE_PATH
- “input.mp4”;
private String output_video_file_path = BASE_PATH - “output.yuv”;
private String input_audio_file_path = BASE_PATH - “hello.mp3”;
private String output_audio_file_path = BASE_PATH - “hello.pcm”;
private String video_src = BASE_PATH - “ffmpeg.mp4”;
private Button mDecodeVideoBtn;
private Button mVideoRenderBtn;
private Button mAudioPlayBtn, mAudioDecodeBtn;
private ProgressDialog mProgressDialog;
private ExecutorService mExecutorService;
private MySurfaceView mySurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS}, 0);
}
mDecodeVideoBtn = (Button)findViewById(R.id.video_decode_btn);
mVideoRenderBtn = (Button)findViewById(R.id.video_render_btn);
mAudioDecodeBtn = (Button) findViewById(R.id.audio_decode_btn);
mAudioPlayBtn = (Button)findViewById(R.id.audio_play_btn);
mySurfaceView = (MySurfaceView) findViewById(R.id.my_surface_view);
mDecodeVideoBtn.setOnClickListener(this);
mVideoRenderBtn.setOnClickListener(this);
mAudioDecodeBtn.setOnClickListener(this);
mAudioPlayBtn.setOnClickListener(this);
AVUtils.registerCallback(this);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setCanceledOnTouchOutside(false);
mExecutorService = Executors.newFixedThreadPool(2);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.video_decode_btn:
mProgressDialog.setMessage(“正在解码…”);
mProgressDialog.show();
mExecutorService.submit(new Runnable() {
@Override
public void run() {
AVUtils.videoDecode(input_video_file_path, output_video_file_path);
}
});
break;
case R.id.video_render_btn:
mExecutorService.submit(new Runnable() {
@Override
public void run() {
AVUtils.videoRender(input_video_file_path, mySurfaceView.getHolder().getSurface());
}
});
break;
case R.id.audio_decode_btn:
mProgressDialog.setMessage(“正在解码…”);
mProgressDialog.show();
mExecutorService.submit(new Runnable() {
@Override
public void run() {
AVUtils.audioDecode(input_audio_file_path, output_audio_file_path);
}
});
break;
case R.id.audio_play_btn:
mExecutorService.submit(new Runnable() {
@Override
public void run() {
AVUtils.audioPlay(input_video_file_path);
}
});
break;
}
}
@Override
public void onFinish() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
Toast.makeText(MainActivity.this, “解码完成”, Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mExecutorService.shutdown();
}
}
nativelib.c
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <stdio.h>
#include <libavutil/time.h>
//编码
#