How to view a video using Exoplayer?

Asked

Viewed 530 times

3

I’m trying to implement the simplest example of Exoplayer (project link and Project Tutorial Link). However, after following all the steps of the tutorial, the application runs, but does not show any sign of running the video.

Mainactivity.java

public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback {
private VideoSurfaceView surfaceView;
private Surface surface;

public static final String TAG = "VodExoPlayer";

private ExoPlayer exoPlayer;
private DefaultSampleSource sampleSource;
private MediaCodecVideoTrackRenderer videoTrackRenderer;
private MediaCodecAudioTrackRenderer audioTrackRenderer;

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

    setParameters();

    builderExoPlayer();
}

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

    surfaceView.getHolder().addCallback(this);
}

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

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

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

    exoPlayer = ExoPlayer.Factory.newInstance(numRenderers);

    exoPlayer.prepare(videoTrackRenderer, audioTrackRenderer);

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

    exoPlayer.setPlayWhenReady(true);
    exoPlayer.release();
}


/**
 * Métodos Abaixo surgiram de "surfaceView.getHolder().addCallback(this)" com SurfaceHolder.Callback
 */

@Override
public void surfaceCreated(SurfaceHolder holder) {
    surface = holder.getSurface();
    Log.i(TAG, "Surface created...");
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    //Nada a fazer
    Log.i(TAG, "Surface changed...");
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    surface = null;
    Log.i(TAG, "Surface destroyed...");
}

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" />

manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Like I said, there’s no sign of video working on the screen or on Logcat.

Could someone tell me if there’s something wrong or missing in the code?

1 answer

2


After a few days trying, I got the solution! There is virtually no material on Exoplayer.

In the code Mainactivity.java, all Exoplayer configuration is correct, but the line exoPlayer.release(); should not be executed as it terminates the Player. So, simply remove it:

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

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

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

exoPlayer = ExoPlayer.Factory.newInstance(numRenderers);

exoPlayer.prepare(videoTrackRenderer, audioTrackRenderer);

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

exoPlayer.setPlayWhenReady(true);
}

The XML code and MANIFEST are correct.

Browser other questions tagged

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