Skip to content

Commit

Permalink
update: result activity using exoplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
gowtham.balamurugan committed Jun 26, 2023
1 parent cfdb52c commit 0f25382
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
implementation project(path: ':library')
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.cocosw:bottomsheet:1.5.0@aar'
implementation 'com.google.android.exoplayer:exoplayer:2.17.1'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
52 changes: 40 additions & 12 deletions app/src/main/java/com/gowtham/videotrimmer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
import androidx.core.content.ContextCompat;

import com.cocosw.bottomsheet.BottomSheet;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.audio.AudioAttributes;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.gowtham.library.utils.CompressOption;
import com.gowtham.library.utils.LogMessage;
import com.gowtham.library.utils.TrimType;
Expand All @@ -31,8 +41,8 @@
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = "MainActivity";
private VideoView videoView;
private MediaController mediaController;
private StyledPlayerView playerView;
private ExoPlayer videoPlayer;
private EditText edtFixedGap, edtMinGap, edtMinFrom, edtMAxTo;
private int trimType;

Expand All @@ -43,14 +53,12 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
result.getData() != null) {
Uri uri = Uri.parse(TrimVideo.getTrimmedVideoPath(result.getData()));
Log.d(TAG, "Trimmed path:: " + uri);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();

videoView.setOnPreparedListener(mediaPlayer -> {
mediaController.setAnchorView(videoView);
});
DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(uri));
videoPlayer.addMediaSource(mediaSource);
videoPlayer.prepare();
videoPlayer.setPlayWhenReady(true);

String filepath = String.valueOf(uri);
File file = new File(filepath);
Expand Down Expand Up @@ -85,18 +93,32 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.video_view);
playerView = findViewById(R.id.player_view);
edtFixedGap = findViewById(R.id.edt_fixed_gap);
edtMinGap = findViewById(R.id.edt_min_gap);
edtMinFrom = findViewById(R.id.edt_min_from);
edtMAxTo = findViewById(R.id.edt_max_to);
mediaController = new MediaController(this);


findViewById(R.id.btn_default_trim).setOnClickListener(this);
findViewById(R.id.btn_fixed_gap).setOnClickListener(this);
findViewById(R.id.btn_min_gap).setOnClickListener(this);
findViewById(R.id.btn_min_max_gap).setOnClickListener(this);
initPlayer();
}

private void initPlayer() {
try {
videoPlayer = new ExoPlayer.Builder(this).build();
playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
playerView.setPlayer(videoPlayer);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MOVIE)
.build();
videoPlayer.setAudioAttributes(audioAttributes, true);
} catch (Exception e) {
e.printStackTrace();
}
}

private void openTrimActivity(String data) {
Expand Down Expand Up @@ -149,6 +171,12 @@ private void onDefaultTrimClicked() {
showVideoOptions();
}

@Override
protected void onDestroy() {
super.onDestroy();
videoPlayer.release();
}

private void onFixedTrimClicked() {
trimType = 1;
if (isEdtTxtEmpty(edtFixedGap))
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.core.widget.NestedScrollView style="@style/VView">

<LinearLayout style="@style/VView">

<VideoView
android:id="@+id/video_view"
<com.google.android.exoplayer2.ui.StyledPlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="300dp" />
android:layout_height="300dp"
android:layout_gravity="center_vertical"
app:use_controller="true" />


<LinearLayout
style="@style/VView"
Expand Down
20 changes: 14 additions & 6 deletions library/src/main/java/com/gowtham/library/ui/ActVideoTrimmer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -466,12 +467,19 @@ protected void onPause() {
@Override
protected void onDestroy() {
super.onDestroy();
if (videoPlayer != null)
videoPlayer.release();
if (progressView != null && progressView.isShowing())
progressView.dismiss();
deleteFile("temp_file");
stopRepeatingTask();
try {
if (videoPlayer != null)
videoPlayer.release();
if (progressView != null && progressView.isShowing())
progressView.dismiss();
File f=new File(getCacheDir(), "temp_video_file");
if(f.exists()){
f.delete();
}
stopRepeatingTask();
} catch (Exception e) {
LogMessage.e(Log.getStackTraceString(e));
}
}

@Override
Expand Down

0 comments on commit 0f25382

Please sign in to comment.