1212import com .google .android .exoplayer2 .C ;
1313import com .google .android .exoplayer2 .ExoPlaybackException ;
1414import com .google .android .exoplayer2 .ExoPlayer ;
15- import com .google .android .exoplayer2 .PlaybackParameters ;
1615import com .google .android .exoplayer2 .Player ;
1716import com .google .android .exoplayer2 .SimpleExoPlayer ;
18- import com .google .android .exoplayer2 .Timeline ;
1917import com .google .android .exoplayer2 .audio .AudioAttributes ;
2018import com .google .android .exoplayer2 .source .ConcatenatingMediaSource ;
2119import com .google .android .exoplayer2 .source .MediaSource ;
2220import com .google .android .exoplayer2 .source .ProgressiveMediaSource ;
23- import com .google .android .exoplayer2 .source .TrackGroupArray ;
24- import com .google .android .exoplayer2 .trackselection .TrackSelectionArray ;
2521import com .google .android .exoplayer2 .upstream .DataSource ;
2622import com .google .android .exoplayer2 .upstream .DefaultHttpDataSourceFactory ;
2723import com .google .android .exoplayer2 .util .Util ;
@@ -135,15 +131,18 @@ PlayerState getPlayerState() {
135131 * Establish the media player and allocate its internal resources.
136132 */
137133 public void prepare () {
138- this .taskHandler .run (() -> {
134+ this .taskHandler .run (this ::inlinePrepare );
135+ }
136+
137+ void inlinePrepare () {
138+ if (this .mediaPlayer == null ) {
139139 ExoPlayer player = this .playerFactory .createPlayer (
140140 this .usage ,
141141 this .contentType ,
142142 this .appContext );
143143 player .addListener (this );
144144 this .mediaPlayer = player ;
145- this .mediaPlayer .prepare (mediaSource );
146- });
145+ }
147146 }
148147
149148 @ Override
@@ -159,30 +158,24 @@ public void close() {
159158
160159 @ Override
161160 public void audioReceived (AudioResponse response ) {
162- if (this .mediaPlayer == null ) {
163- prepare ();
164- }
165-
166161 this .taskHandler .run (() -> {
162+ inlinePrepare ();
163+
167164 Uri audioUri = response .getAudioUri ();
168165 MediaSource newTrack = createMediaSource (audioUri );
169166
170- if (mediaPlayer .isPlaying ()) {
171- mediaSource .addMediaSource (newTrack );
172- } else {
173- mediaSource .clear ();
174- mediaSource .addMediaSource (newTrack );
175- mediaPlayer .prepare (mediaSource );
176- }
167+ this .mediaSource .addMediaSource (newTrack );
168+ this .mediaPlayer .prepare (this .mediaSource );
177169 this .playerState = new PlayerState (
178170 true ,
179171 this .playerState .shouldPlay ,
180172 this .playerState .curPosition ,
181173 this .playerState .window
182174 );
175+
176+ inlinePlay ();
183177 });
184178
185- playContent ();
186179 }
187180
188181 @ Override
@@ -236,7 +229,12 @@ public void onIsPlayingChanged(boolean isPlaying) {
236229 }
237230
238231 private void resetPlayerState () {
232+ this .mediaSource .clear ();
239233 this .playerState = new PlayerState (false , false , 0 , 0 );
234+ if (this .mediaPlayer != null ) {
235+ this .mediaPlayer .release ();
236+ this .mediaPlayer = null ;
237+ }
240238 }
241239
242240 @ Override
@@ -266,20 +264,20 @@ public void onAudioFocusChange(int focusChange) {
266264 * Start or resume playback of any TTS responses.
267265 */
268266 public void playContent () {
269- this .taskHandler .run (() -> {
270- if (!playerState .hasContent ) {
271- return ;
272- }
267+ this .taskHandler .run (this ::inlinePlay );
268+ }
273269
274- if (mediaPlayer == null ) {
275- prepare ();
276- }
270+ void inlinePlay () {
271+ if (!playerState .hasContent ) {
272+ return ;
273+ }
277274
278- // only play if focus is granted
279- if (requestFocus () == AudioManager .AUDIOFOCUS_REQUEST_GRANTED ) {
280- mediaPlayer .setPlayWhenReady (true );
281- }
282- });
275+ inlinePrepare ();
276+
277+ // only play if focus is granted
278+ if (requestFocus () == AudioManager .AUDIOFOCUS_REQUEST_GRANTED ) {
279+ mediaPlayer .setPlayWhenReady (true );
280+ }
283281 }
284282
285283 int requestFocus () {
@@ -371,46 +369,4 @@ ExoPlayer createPlayer(int usage, int contentType,
371369 return player ;
372370 }
373371 }
374-
375- // similarly, implementing these listener methods maintains backwards
376- // compatibility for ExoPlayer
377-
378- @ Override public void onTimelineChanged (@ NotNull Timeline timeline ,
379- int reason ) { }
380-
381- @ Override
382- // it's deprecated, but it's still a default method, so we have to
383- // implement it for older versions of Android
384- @ SuppressWarnings ("deprecation" )
385- public void onTimelineChanged (@ NotNull Timeline timeline ,
386- @ Nullable Object manifest , int reason ) { }
387-
388- @ Override
389- public void onTracksChanged (@ NotNull TrackGroupArray trackGroups ,
390- @ NotNull TrackSelectionArray trackSelections ) {
391-
392- }
393-
394- @ Override public void onLoadingChanged (boolean isLoading ) { }
395-
396- @ Override
397- public void onPlaybackSuppressionReasonChanged (
398- int playbackSuppressionReason ) { }
399-
400- @ Override
401- public void onPlayerStateChanged (boolean playWhenReady , int playbackState ) {
402- }
403-
404- @ Override public void onRepeatModeChanged (int repeatMode ) { }
405-
406- @ Override public void onShuffleModeEnabledChanged (
407- boolean shuffleModeEnabled ) { }
408-
409- @ Override public void onPositionDiscontinuity (int reason ) { }
410-
411- @ Override
412- public void onPlaybackParametersChanged (
413- @ NotNull PlaybackParameters playbackParameters ) { }
414-
415- @ Override public void onSeekProcessed () { }
416372}
0 commit comments