I need to run video in my Android application using ExoPlayer, a google project that allows DASH, persistent cache. Official Project Page here
However, the exoPlayer.sendMessage
method has a parameter of class Surface
, but I could not find this type of View, so I used VideoSurfaceView
, but it did not work. Here's 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);
}
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?