Playing video on TextureView
Here is how you can do it:
(solution by the question author, that he posted as an update in the question)
Public class MediaPlayerDemo_Video extends Activity implements TextureView.SurfaceTextureListener {
private MediaPlayer mMediaPlayer;
private TextureView mPreview;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mPreview = new TextureView(this);
mPreview.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mPreview.setSurfaceTextureListener(this);
extras = getIntent().getExtras();
setContentView(mPreview);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
try {
mMediaPlayer= new MediaPlayer();
mMediaPlayer.setDataSource("http://daily3gp.com/vids/747.3gp");
mMediaPlayer.setSurface(s);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And animating it works really well.
Playing a video after a video in Android's TextureView has issues
You have a couple of options, maybe.
In theory you can clear the Surface by disconnecting the media player, clearing the surface with GLES, and then connecting the media player again. See clickPlayStop()
and clearSurface()
in Grafika's PlayMovieSurfaceActivity. This uses MediaCodec, not MediaPlayer, so I'm not sure how it'll translate.
Another option is to use the TextureView transform rather than a custom frame layout to set your aspect ratio. Because your layout doesn't change, there is no flash of a distorted frame. See adjustAspectRatio()
in PlayMovieActivity for an example.
How to play video one by one in TextureView in android?
onSurfaceTextureAvailable
method doesn't get triggered when the MediaPlayer
object has completed playback, it gets triggered when the Surface texture is ready to be used.
You need to implement MediaPlayer.OnCompletionListener
, the onCompletion
method gets triggered once the playback is completed.
Start the playback of the next video in the list once the playback of the first video is completed.
Playing video after another video on textureview
I finally fixed the issue by using this:
mediaPlayer!!.setOnCompletionListener {
val fileDescriptor = assets.openFd("confetti_floor_video.mp4")
mediaPlayerObj!!.reset()
mediaPlayerObj!!.setDataSource(
fileDescriptor!!.fileDescriptor,
fileDescriptor!!.startOffset,
fileDescriptor!!.length
)
mediaPlayerObj!!.prepareAsync()
}
Hope this helps someone else :)
Playing video on texture view with aspect ratio of video
You can either change the size of the View (using a custom FrameLayout), or use the TextureView matrix to change the way the texture is rendered.
Examples of both can be found in Grafika. The "Play video (TextureView)" activity demonstrates configuring the matrix to match the aspect ratio of a video. See the adjustAspectRatio()
method, the core of which is:
Matrix txform = new Matrix();
mTextureView.getTransform(txform);
txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
txform.postTranslate(xoff, yoff);
mTextureView.setTransform(txform);
Note it centers as well as scales the video.
Related Topics
Listview Is Blank While Using Getfilter Function
How to Check If Current Thread Is Not Main Thread
Kafka - Unable to Send a Message to a Remote Server Using Java
How to Connect to Ftps Server with Data Connection Using Same Tls Session
Default Constructor VS. Inline Field Initialization
Jersey /* Servlet Mapping Causes 404 Error for Static Resources
How to Return 2 Values from a Java Method
How Are Integers Internally Represented at a Bit Level in Java
Android: How to Stretch an Image to the Screen Width While Maintaining Aspect Ratio
How to Change Color of the Back Arrow in the New Material Theme
Javafx Freeze on Desktop.Open(File), Desktop.Browse(Uri)
Differences Between System.Out.Println() and Return in Java
Reusing a Preparedstatement Multiple Times
Hibernate 5 :- Org.Hibernate.Mappingexception: Unknown Entity
How to Run Certain Task Every Day at a Particular Time Using Scheduledexecutorservice