private void setupExoPlayer() { audioCapabilitiesReceiver = new AudioCapabilitiesReceiver(getContext().getApplicationContext(), this); audioCapabilitiesReceiver.register(); emExoPlayer = new EMExoPlayer(null); // Sets the internal listener listenerMux = new EMListenerMux(new MuxNotifier()); emExoPlayer.addListener(listenerMux); emExoPlayer.setMetadataListener(null); emExoPlayer.setSurface(exoVideoSurfaceView.getHolder().getSurface()); exoVideoSurfaceView.getHolder().addCallback(new EMExoVideoSurfaceCallback()); }
/** * Returns if a video is currently in playback * * @return True if a video is playing */ public boolean isPlaying() { if (!useExo) { return videoView.isPlaying(); } return emExoPlayer.getPlayWhenReady(); }
/** * Moves the current video progress to the specified location. * * @param milliSeconds The time to move the playback to */ public void seekTo(int milliSeconds) { if (!useExo) { videoView.seekTo(milliSeconds); } else { emExoPlayer.seekTo(milliSeconds); } }
/** * Retrieves the current buffer percent of the video. If a video is not currently prepared or * buffering the value will be 0. This should only be called after the video is prepared (see * {@link #setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)}) * * @return The integer percent that is buffered [0, 100] inclusive */ public int getBufferPercentage() { if (!useExo) { return videoView.getBufferPercentage(); } return emExoPlayer.getBufferedPercentage(); }
/** * Sets the volume level for devices that support the ExoPlayer (JellyBean or greater). * * @param volume The volume range [0.0 - 1.0] * @return True if the volume was set */ public boolean setVolume(float volume) { if (useExo) { emExoPlayer.setVolume(volume); return true; } return false; }
public void buildRenderers(EMExoPlayer player, RendererBuilderCallback callback) { // Create the Sample Source to be used by the renderers Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE); DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(player.getMainHandler(), player); DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent); ExtractorSampleSource sampleSource = new ExtractorSampleSource( Uri.parse(MediaUtil.getUriWithProtocol(uri)), dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENTS, getExtractor(uri, requestedDefaultType)); // Create the Renderers MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer( sampleSource, null, true, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, MAX_JOIN_TIME, null, player.getMainHandler(), player, DROPPED_FRAME_NOTIFICATION_AMOUNT); EMMediaCodecAudioTrackRenderer audioRenderer = new EMMediaCodecAudioTrackRenderer( sampleSource, null, true, player.getMainHandler(), player); // Create the Render list to send to the callback TrackRenderer[] renderers = new TrackRenderer[EMExoPlayer.RENDER_COUNT]; renderers[EMExoPlayer.RENDER_VIDEO_INDEX] = videoRenderer; renderers[EMExoPlayer.RENDER_AUDIO_INDEX] = audioRenderer; callback.onRenderers(null, null, renderers, bandwidthMeter); }
/** * Sets the Uri location for the video to play * * @param uri The video's Uri * @param defaultMediaType The MediaType to use when auto-detection fails */ public void setVideoURI(Uri uri, MediaUtil.MediaType defaultMediaType) { videoUri = uri; if (!useExo) { videoView.setVideoURI(uri); } else { if (uri == null) { emExoPlayer.replaceRenderBuilder(null); } else { emExoPlayer.replaceRenderBuilder( getRendererBuilder(VideoType.get(uri), uri, defaultMediaType)); listenerMux.setNotifiedCompleted(false); } // Makes sure the listeners get the onPrepared callback listenerMux.setNotifiedPrepared(false); emExoPlayer.seekTo(0); } if (defaultControls != null) { defaultControls.restartLoading(); } }
/** * Retrieves the current position of the audio playback. If an audio item is not currently in * playback then the value will be 0. This should only be called after the item is prepared (see * {@link #setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)}) * * @return The millisecond value for the current position */ public long getCurrentPosition() { if (overridePosition) { return positionOffset + overriddenPositionStopWatch.getTime(); } if (!listenerMux.isPrepared()) { return 0; } if (!useExo) { return positionOffset + videoView.getCurrentPosition(); } return positionOffset + emExoPlayer.getCurrentPosition(); }
/** * Retrieves the duration of the current audio item. This should only be called after the item is * prepared (see {@link #setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)}). If * {@link #overrideDuration(int)} is set then that value will be returned. * * @return The millisecond duration of the video */ public long getDuration() { if (overriddenDuration >= 0) { return overriddenDuration; } if (!listenerMux.isPrepared()) { return 0; } if (!useExo) { return videoView.getDuration(); } return emExoPlayer.getDuration(); }
/** * If a video is currently in playback then the playback will be suspended and and the * progressPoll will be stopped (see {@link #startProgressPoll()}) */ public void suspend() { if (!useExo) { videoView.suspend(); } else { emExoPlayer.release(); } if (defaultControls != null) { defaultControls.updatePlayPauseImage(false); defaultControls.show(); } playRequested = false; stopProgressPoll(); }
/** * If a video is currently in playback then the playback will be stopped and the progressPoll will * be stopped (see {@link #startProgressPoll()}) */ public void stopPlayback() { if (!useExo) { videoView.stopPlayback(); } else { emExoPlayer.setPlayWhenReady(false); } if (defaultControls != null) { defaultControls.updatePlayPauseImage(false); defaultControls.show(); } playRequested = false; stopProgressPoll(); }
/** * Starts the playback for the video specified in {@link #setVideoURI(android.net.Uri)} or {@link * #setVideoPath(String)}. This should be called after the VideoView is correctly prepared (see * {@link #setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)}) */ public void start() { if (!useExo) { videoView.start(); } else { emExoPlayer.setPlayWhenReady(true); } if (defaultControls != null) { defaultControls.updatePlayPauseImage(true); defaultControls.hideDelayed(DefaultControls.DEFAULT_CONTROL_HIDE_DELAY); } playRequested = true; startProgressPoll(); }
@Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); defaultControls = null; stopPlayback(); overriddenPositionStopWatch.stop(); if (emExoPlayer != null) { emExoPlayer.release(); } if (audioCapabilitiesReceiver != null) { audioCapabilitiesReceiver.unregister(); audioCapabilitiesReceiver = null; } }