0

I tried to play video on TextureView. I have googled it and gone through and this question on stackoverflow in order to play video in TextureView. I've used Local file instead of remote video stream in setDataSource(). I am not getting any error in logcat and it shows that video started playing. But i see only black screen on the display. I tried sample code this github project. Even in this case also only black screen appears. I am able to play same video through VideoView, so there is no problem in video file.

Here's the code i've tried so far...

MainActivity.java

private TextureView mTextureView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        this.mTextureView=(TextureView) findViewById(R.id.textureView);
        this.mTextureView.setSurfaceTextureListener(this);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
 }

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
        int height) {
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(Environment
                .getExternalStorageDirectory()
                + File.separator
                + "video1.mp4");
        mMediaPlayer.setSurface(new Surface(surface));
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <TextureView android:id="@+id/textureView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

Please help me on this issue... Thanks in advance

EDIT
Is something missing in my code or am i doing something wrong...

Community
  • 1
  • 1
Bhupesh
  • 477
  • 1
  • 8
  • 22
  • See "play video (TextureView)" activity in Grafika - https://github.com/google/grafika . It uses MediaCodec rather than MediaPlayer, but it's the same idea. – fadden Aug 03 '15 at 02:30

1 Answers1

0

If you try playing videos on Android simulator it won't work because, at the moment of this answer, the Android Virtual Devices aren't capable of video playing. You need to test it on a real device.