Created
August 1, 2016 15:32
-
-
Save cmdr2/f80732446231a29502444e1488264c0e to your computer and use it in GitHub Desktop.
An example of using ExoPlayer from scratch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.cmdr2.exoplayerbridge; | |
import android.app.Activity; | |
import android.media.AudioManager; | |
import android.media.MediaCodec; | |
import android.net.Uri; | |
import android.util.Log; | |
import android.view.Surface; | |
import android.view.SurfaceView; | |
import com.google.android.exoplayer.ExoPlayer; | |
import com.google.android.exoplayer.MediaCodecAudioTrackRenderer; | |
import com.google.android.exoplayer.MediaCodecSelector; | |
import com.google.android.exoplayer.MediaCodecVideoTrackRenderer; | |
import com.google.android.exoplayer.SampleSource; | |
import com.google.android.exoplayer.TrackRenderer; | |
import com.google.android.exoplayer.audio.AudioCapabilities; | |
import com.google.android.exoplayer.extractor.ExtractorSampleSource; | |
import com.google.android.exoplayer.upstream.Allocator; | |
import com.google.android.exoplayer.upstream.DataSource; | |
import com.google.android.exoplayer.upstream.DefaultAllocator; | |
import com.google.android.exoplayer.upstream.DefaultUriDataSource; | |
public class ExoPlayerExample { | |
/* globals */ | |
private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; | |
private static final int BUFFER_SEGMENT_COUNT = 256; | |
/* scratchpad */ | |
private Activity activity; | |
private Surface surface; | |
private String audioUrl; | |
private String videoUrl; | |
private ExoPlayer player; | |
public ExoPlayerExample(Activity activity) { | |
this.activity = activity; | |
} | |
public void setSurface(Surface surface) { | |
this.surface = surface; | |
} | |
public void setAudioUrl(String audioUrl) { | |
this.audioUrl = audioUrl; | |
} | |
public void setVideoUrl(String videoUrl) { | |
this.videoUrl = videoUrl; | |
} | |
public void init() { | |
activity.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
doInit(); | |
} | |
}); | |
} | |
public void seekTo(long pos) { | |
player.seekTo(pos); | |
} | |
public long getCurrentPosition() { | |
return player.getCurrentPosition(); | |
} | |
private void doInit() { | |
player = ExoPlayer.Factory.newInstance(2, 1000, 5000); | |
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE); | |
DataSource audioDataSource = new DefaultUriDataSource(activity.getApplicationContext(), "Demo UserAgent"); | |
DataSource videoDataSource = new DefaultUriDataSource(activity.getApplicationContext(), "Demo UserAgent"); | |
Uri audioUri = Uri.parse(audioUrl); | |
Uri videoUri = Uri.parse(videoUrl); | |
SampleSource audioSampleSource = new ExtractorSampleSource(audioUri, audioDataSource, | |
allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE, null, null, 0); | |
SampleSource videoSampleSource = new ExtractorSampleSource(videoUri, videoDataSource, | |
allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE, null, null, 0); | |
TrackRenderer audioTrackRenderer = new MediaCodecAudioTrackRenderer(audioSampleSource, | |
MediaCodecSelector.DEFAULT, null, true, null, null, | |
AudioCapabilities.getCapabilities(activity.getApplicationContext()), AudioManager.STREAM_MUSIC); | |
final TrackRenderer videoTrackRenderer = new MediaCodecVideoTrackRenderer(activity.getApplicationContext(), | |
videoSampleSource, MediaCodecSelector.DEFAULT, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000, | |
null, null, 50); | |
player.prepare(videoTrackRenderer, audioTrackRenderer); | |
player.sendMessage(videoTrackRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface); | |
player.setPlayWhenReady(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment