Skip to content

Commit 8da7e7a

Browse files
committed
MediaPlayerActivity接入基类Activity
MediaPlayerActivity接入基类Activity
1 parent af92d20 commit 8da7e7a

6 files changed

Lines changed: 80 additions & 49 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
<!-- 音视频解码播放 -->
4242
<activity
4343
android:name=".activity.MediaPlayerActivity"
44-
android:screenOrientation="landscape"
45-
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
44+
android:screenOrientation="landscape" />
4645
<!-- 本地推流直播 -->
4746
<activity
4847
android:name=".activity.PushActivity"
@@ -55,8 +54,7 @@
5554
<!-- 滤镜特效 -->
5655
<activity
5756
android:name=".activity.FilterActivity"
58-
android:screenOrientation="landscape"
59-
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
57+
android:screenOrientation="landscape" />
6058
<!--视频倒播-->
6159
<activity android:name=".activity.VideoReverseActivity"
6260
android:screenOrientation="landscape"

app/src/main/cpp/media_player.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,18 @@ void* decode_func(void* arg){
411411
//根据stream_index获取对应的AVPacket队列
412412
AVPacketQueue *queue = player->packets[stream_index];
413413
int ret = 0;
414-
int video_frame_count = 0, audio_frame_count = 0;
414+
// int video_frame_count = 0, audio_frame_count = 0;
415415
for(;;) {
416416
pthread_mutex_lock(&player->mutex);
417417
AVPacket *packet = (AVPacket*)queue_pop(queue, &player->mutex, &player->cond);
418418
pthread_mutex_unlock(&player->mutex);
419419

420420
if(stream_index == player->video_stream_index) {//视频流
421421
ret = decode_video(player, packet);
422-
LOGI("decode video stream = %d", video_frame_count++);
422+
// LOGI("decode video stream = %d", video_frame_count++);
423423
} else if(stream_index == player->audio_stream_index) {//音频流
424424
ret = decode_audio(player, packet);
425-
LOGI("decode audio stream = %d", audio_frame_count++);
425+
// LOGI("decode audio stream = %d", audio_frame_count++);
426426
}
427427
av_packet_unref(packet);
428428
if(ret < 0){
@@ -432,7 +432,7 @@ void* decode_func(void* arg){
432432
}
433433

434434
JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_MediaPlayer_setup
435-
(JNIEnv * env, jclass clazz, jstring filePath, jobject surface){
435+
(JNIEnv * env, jobject instance, jstring filePath, jobject surface){
436436

437437
const char *file_name = (*env)->GetStringUTFChars(env, filePath, JNI_FALSE);
438438
int ret;
@@ -455,15 +455,15 @@ JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_MediaPlayer_setup
455455
//初始化音频相关参数
456456
audio_decoder_prepare(player);
457457
//初始化音频播放器
458-
audio_player_prepare(player, env, clazz);
458+
audio_player_prepare(player, env, instance);
459459
//初始化音视频packet队列
460460
init_queue(player, PACKET_SIZE);
461461

462462
return 0;
463463
}
464464

465465
JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_MediaPlayer_play
466-
(JNIEnv * env, jclass clazz){
466+
(JNIEnv * env, jobject instance){
467467
pthread_mutex_init(&player->mutex, NULL);
468468
pthread_cond_init(&player->cond, NULL);
469469

@@ -488,7 +488,7 @@ JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_MediaPlayer_play
488488
}
489489

490490
JNIEXPORT void JNICALL Java_com_frank_ffmpeg_MediaPlayer_release
491-
(JNIEnv * env, jclass clazz){
491+
(JNIEnv * env, jobject instance){
492492
//释放内存以及关闭文件
493493
free(player->audio_track);
494494
free(player->audio_track_write_mid);

app/src/main/java/com/frank/ffmpeg/activity/FilterActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ int getLayoutId() {
104104
protected void onCreate(Bundle savedInstanceState) {
105105
super.onCreate(savedInstanceState);
106106

107+
hideActionBar();
107108
initView();
108109
registerLister();
109110

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
11
package com.frank.ffmpeg.activity;
22

3-
import android.os.Environment;
4-
import android.support.v7.app.AppCompatActivity;
53
import android.os.Bundle;
64
import android.util.Log;
75
import android.view.SurfaceHolder;
86
import android.view.SurfaceView;
7+
import android.view.View;
8+
import android.widget.Button;
9+
910
import com.frank.ffmpeg.MediaPlayer;
1011
import com.frank.ffmpeg.R;
1112
import com.frank.ffmpeg.util.FileUtil;
1213

13-
import java.io.File;
14-
1514
/**
1615
* 音视频解码播放
1716
* Created by frank on 2018/2/12.
1817
*/
1918

20-
public class MediaPlayerActivity extends AppCompatActivity implements SurfaceHolder.Callback {
19+
public class MediaPlayerActivity extends BaseActivity implements SurfaceHolder.Callback {
20+
2121
private static final String TAG = MediaPlayerActivity.class.getSimpleName();
22-
SurfaceHolder surfaceHolder;
23-
private final static String PATH = Environment.getExternalStorageDirectory().getPath() + File.separator;
24-
private String filePath = PATH + "hello.mp4";
22+
23+
private SurfaceHolder surfaceHolder;
24+
2525
private MediaPlayer mediaPlayer;
2626

27+
private boolean surfaceCreated;
28+
29+
private Button btnSelectFile;
30+
31+
@Override
32+
int getLayoutId() {
33+
return R.layout.activity_media_player;
34+
}
35+
2736
@Override
2837
protected void onCreate(Bundle savedInstanceState) {
2938
super.onCreate(savedInstanceState);
30-
setContentView(R.layout.activity_media_player);
39+
40+
hideActionBar();
3141
initView();
3242
initPlayer();
3343
}
3444

3545
private void initView(){
36-
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_media);
46+
SurfaceView surfaceView = getView(R.id.surface_media);
3747
surfaceHolder = surfaceView.getHolder();
3848
surfaceHolder.addCallback(this);
39-
40-
// Button btn_slow = (Button) findViewById(R.id.btn_play_slow);
41-
// Button btn_fast = (Button) findViewById(R.id.btn_play_fast);
49+
btnSelectFile = getView(R.id.btn_select_file);
50+
initViewsWithClick(R.id.btn_select_file);
4251
}
4352

4453
private void initPlayer(){
@@ -47,21 +56,7 @@ private void initPlayer(){
4756

4857
@Override
4958
public void surfaceCreated(SurfaceHolder holder) {
50-
if (!FileUtil.checkFileExist(filePath)){
51-
return;
52-
}
53-
54-
new Thread(new Runnable() {
55-
@Override
56-
public void run() {
57-
int result = mediaPlayer.setup(filePath, surfaceHolder.getSurface());
58-
if(result < 0){
59-
Log.e(TAG, "mediaPlayer-->setup");
60-
return;
61-
}
62-
mediaPlayer.play();
63-
}
64-
}).start();
59+
surfaceCreated = true;
6560
}
6661

6762
@Override
@@ -83,4 +78,35 @@ protected void onDestroy() {
8378
}
8479
}
8580

81+
private void startPlay(final String filePath) {
82+
new Thread(new Runnable() {
83+
@Override
84+
public void run() {
85+
int result = mediaPlayer.setup(filePath, surfaceHolder.getSurface());
86+
if(result < 0){
87+
Log.e(TAG, "mediaPlayer setup error!");
88+
return;
89+
}
90+
mediaPlayer.play();
91+
}
92+
}).start();
93+
}
94+
95+
@Override
96+
void onSelectedFile(String filePath) {
97+
if (!FileUtil.checkFileExist(filePath)){
98+
return;
99+
}
100+
if (surfaceCreated) {
101+
btnSelectFile.setVisibility(View.GONE);
102+
startPlay(filePath);
103+
}
104+
}
105+
106+
@Override
107+
void onViewClick(View view) {
108+
if (view.getId() == R.id.btn_select_file) {
109+
selectFile();
110+
}
111+
}
86112
}

app/src/main/res/layout/activity_media_player.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
43
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
tools:context="com.frank.ffmpeg.activity.MediaPlayerActivity">
4+
android:layout_height="match_parent">
75

86
<SurfaceView
97
android:id="@+id/surface_media"
@@ -30,4 +28,12 @@
3028
android:text="@string/video_fast"
3129
android:visibility="gone"/>
3230

31+
<Button
32+
android:id="@+id/btn_select_file"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:layout_centerInParent="true"
36+
android:layout_margin="16dp"
37+
android:text="@string/select_file" />
38+
3339
</RelativeLayout>

app/src/main/res/values/colors.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<color name="colorPrimary">#3F51B5</color>
44
<color name="colorPrimaryDark">#303F9F</color>
55
<color name="colorAccent">#FF4081</color>
6-
<color name="colorMain">#ffffff</color>
7-
<color name="colorBord">#d7d7d7</color>
8-
<color name="redBtn">#ff2c29</color>
9-
<color name="blueBtn">#00ddff</color>
10-
<color name="gray">#bebebe</color>
11-
<color name="orange">#ff9800</color>
12-
<color name="trasnsWhite">#32ffffff</color>
6+
<color name="colorWhite">#FFFFFF</color>
7+
<color name="colorBord">#D7D7D7</color>
8+
<color name="redBtn">#FF2C29</color>
9+
<color name="blueBtn">#00DDFF</color>
10+
<color name="gray">#BEBEBE</color>
11+
<color name="orange">#FF9800</color>
12+
<color name="trasnsWhite">#32FFFFFF</color>
1313
</resources>

0 commit comments

Comments
 (0)