How to create video trim functionality in android studio in just 5 minutes


Hello Friends, today i came with another tutorials. In this tutorial you will easily create trim functionality in just 5 minutes which means it will be super easy for you and everything will be automatically done ( no need to write so much coding, you just need 5 to 10 line of code for it ). Lets start tutorial, now you already now video trimming means to cut whole video and make new sub video from it.


video trim in android studio
video trim in android studio

Practical Implementation


1. you need these permission, so add these permission to your manifest file.



<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2. Also you need to call for permission explicitly during runtime by following


if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE },1);
    }

}


3. You need to import video trim library by adding this line of code to gradle project file and click sync the project, it will download all the required libraries file for you.


implementation 'life.knowledge4:k4l-video-trimmer:1.0'


4. Now you are ready to use library functionality, add first video trim UI element to your layout that is


<life.knowledge4.videotrimmer.K4LVideoTrimmer    
android:id="@+id/timeLine"    
app:layout_constraintBottom_toBottomOf="parent"    
app:layout_constraintEnd_toEndOf="parent"    
app:layout_constraintStart_toStartOf="parent"    
app:layout_constraintTop_toTopOf="parent"    
android:layout_height="match_parent"
android:layout_width="match_parent" />


5. In the main class ( java coding ) , set linking of xml (backend) with code , then set maximum duration, set output file destination and most thing is you have set video URI (Uri is basically the path of video file you choose by file chooser or any other way) and last thing is listener when you set trim start time or last time and click on save listener will generate new file with selected area of user. Below is code for this


videoTrimmer  = ((K4LVideoTrimmer) findViewById(R.id.timeLine));
videoTrimmer.setMaxDuration(60);
final String output = direct+"/"+  getIntent().getStringExtra("key")+".mp4";
videoTrimmer.setDestinationPath(output);


if (videoTrimmer != null) {
    videoTrimmer.setVideoURI(Uri.parse(path));
}

videoTrimmer.setOnTrimVideoListener(new OnTrimVideoListener() {
    @Override    public void getResult(Uri uri) {


        activity.runOnUiThread(new Runnable() {
            @Override            public void run() {

                Toast.makeText(trim_video.this ,"Video trim successfully .... ", Toast.LENGTH_SHORT).show();

            }
        });


        System.out.println( "file = " + uri.getPath() + " = " + uri.toString()  );
        //String videopath = WritetoExternalStorage.getVideopath( uri  , trim_video.this );
        File f = new File( uri.getPath() );
        File to = new File( output );

        f.renameTo(to);

        finish();


    }

    @Override    public void cancelAction() {


    }
});


If it still does not help you or you are extremely weak or noob in android app development then there is last option for you that is get all source code i create and try to study it Link is available below




Previous Post Next Post