From 0f25382678dc92a4becd48ea523cef52fff81899 Mon Sep 17 00:00:00 2001 From: "gowtham.balamurugan" Date: Mon, 26 Jun 2023 23:21:28 +0530 Subject: [PATCH] update: result activity using exoplayer --- app/build.gradle | 1 + .../gowtham/videotrimmer/MainActivity.java | 52 ++++++++++++++----- app/src/main/res/layout/activity_main.xml | 10 ++-- .../gowtham/library/ui/ActVideoTrimmer.java | 20 ++++--- 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a92d8a2..c909451 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } \ No newline at end of file diff --git a/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java b/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java index 3deddd6..d54281f 100644 --- a/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java +++ b/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java @@ -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; @@ -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; @@ -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); @@ -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) { @@ -149,6 +171,12 @@ private void onDefaultTrimClicked() { showVideoOptions(); } + @Override + protected void onDestroy() { + super.onDestroy(); + videoPlayer.release(); + } + private void onFixedTrimClicked() { trimType = 1; if (isEdtTxtEmpty(edtFixedGap)) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5f3b0e0..dd1d641 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,6 +3,7 @@ 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"> @@ -10,10 +11,13 @@ - + android:layout_height="300dp" + android:layout_gravity="center_vertical" + app:use_controller="true" /> +