How do I view a video using ExoPlayer?

3

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?

    
asked by anonymous 09.05.2015 / 01:54

1 answer

2

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 .

    
15.05.2015 / 01:03