How do I get a Surface to run a video using Exoplayer?

Asked

Viewed 502 times

0

I need to run video on my Android app using Exoplayer, a google project that allows DASH, persistent cache. Official Project Page here

However, the method exoPlayer.sendMessage has a class parameter Surface, but I couldn’t find this kind of view, so I used VideoSurfaceView, but it didn’t work. Follow code and Logcat.

Logcat:

05-07 21:30:10.438    7616-7630/com.promobile.vod.vodmobile E/ExoPlayerImplInternal﹕ Internal runtime error.
java.lang.ClassCastException: com.google.android.exoplayer.VideoSurfaceView cannot be cast to android.view.Surface
        at com.google.android.exoplayer.MediaCodecVideoTrackRenderer.handleMessage(MediaCodecVideoTrackRenderer.java:331)
        at com.google.android.exoplayer.ExoPlayerImplInternal.sendMessageInternal(ExoPlayerImplInternal.java:540)
        at com.google.android.exoplayer.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:219)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
        at com.google.android.exoplayer.util.PriorityHandlerThread.run(PriorityHandlerThread.java:40)

Code Mainactivity.java

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setParameters();

    builderExoPlayer();
}

private void setParameters() {
    videoSurfaceView = (VideoSurfaceView) findViewById(R.id.surface);
}

private void builderExoPlayer() {
    int numRenderers = 2;
    Uri URI = Uri.parse("http://www.semanticdevlab.com/abc.mp4");

    SampleSource sampleSource = new DefaultSampleSource(new FrameworkSampleExtractor(getApplicationContext(), URI, null), numRenderers);

    TrackRenderer videoTrackRenderer = new MediaCodecVideoTrackRenderer(sampleSource, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);
    TrackRenderer audioTrackRenderer = new MediaCodecAudioTrackRenderer(sampleSource);

    ExoPlayer exoPlayer = ExoPlayer.Factory.newInstance(numRenderers);

    exoPlayer.prepare(videoTrackRenderer, audioTrackRenderer);

    exoPlayer.sendMessage(videoTrackRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, videoSurfaceView);

    exoPlayer.setPlayWhenReady(true);
}

code activity_main.xml:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<com.google.android.exoplayer.VideoSurfaceView
    android:id="@+id/surface"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

How do I get this Surface to run Exoplayer?

1 answer

2


Try to get Surface how it is done in the official page demo application you quoted:

exoPlayer.sendMessage(videoTrackRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,
         videoSurfaceView.getHolder().getSurface());

Note that according to the documentation of Surfaceview, the object SurfaceHolder returned by getHolder() may not be available immediately; it may be necessary to implement callbacks surfaceCreated(SurfaceHolder) and surfaceDestroyed(SurfaceHolder).

  • Sorry, there was an error in the variable name surfaceView, she’s the type VideoSurfaceView.

  • I changed my answer.

  • Perfect! I was checking the documentation here and that’s exactly it! Thank you for the detailed explanation!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.