How do I view a video using ExoPlayer?

Posted on

Question :

I’m trying to implement the simpler example of ExoPlayer ( project link and Project Tutorial Link ). However, after following all the steps in the tutorial, the application runs, but does not display any video signal.

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>

As I said, there is no sign of video work either on the screen or in LogCat.

Could someone tell me if something is wrong or missing from the code?

    

Answer :

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

In the MainActivity.java code, the entire ExoPlayer configuration is correct, but the exoPlayer.release(); line should not run as it exits the Player. So just 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);
}

XML and MANIFEST are correct .

    

Leave a Reply

Your email address will not be published. Required fields are marked *