I would like to crop and play multiple videos in one activity using texture view in android programmatically. The code below throws an exception " Caused by: java.lang.IllegalArgumentException: surfaceTexture must not be null".
Mainactivity.java class
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private TextureView videoView1;
private TextureView videoView2;
private MediaPlayer mediaPlayer1;
private MediaPlayer mediaPlayer2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView1 = findViewById(R.id.video_view_1);
videoView2 = findViewById(R.id.video_view_2);
String path = "android.resource://" + getPackageName() + "/" + R.raw.patila;
mediaPlayer1 = new MediaPlayer();
try {
mediaPlayer1.setDataSource(path);
mediaPlayer1.setSurface(new Surface(videoView1.getSurfaceTexture()));
mediaPlayer1.setOnPreparedListener(mp -> mp.start());
mediaPlayer1.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer2 = new MediaPlayer();
try {
mediaPlayer2.setDataSource(path);
mediaPlayer2.setSurface(new Surface(videoView2.getSurfaceTexture()));
mediaPlayer2.setOnPreparedListener(mp -> mp.start());
mediaPlayer2.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer1.release();
mediaPlayer2.release();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextureView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/video_view_1"/>
<TextureView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/video_view_2"/>
</LinearLayout>
I have done multiple research about the same and how can achieve multiple video cropping and playing on one activity. I'm really confused on the best methodology to achieve the same because I'm new to TextureView. Kindly assist me on how to go about it.
When working with a
TextureView(a similar thing applies toSurfaceView) you have to listen to callbacks about the state of theSurfaceTexture.In the case of the
TextureViewyou need to implementTextureView.SurfaceTextureListener(see: docs). It is only within the callbackonSurfaceTextureAvailablethat theSurfaceTextureis non-null and so inonCreatetheSurfaceTextureisnulland thus you are getting the error you're getting.When testing I didn't have much success with accessing a file in the
rawresources directory and so I opted for including a test video file in theassetsinstead. ThesetDataSourcefunction can also be used by providing anAssetFileDescriptor.This code should work. You may be able to refactor the
releasecalls on theMediaPlayerinstances into theonSurfaceTextureDestroyedcallback. You may also be able to refactor the anonymousSurfaceTextureListenerinto a class to neaten up the code but I kept it like this for ease.For some good resources look at; Android Graphics Architecture Docs and the Grafika Sample Applications