-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
25 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
122 changes: 122 additions & 0 deletions
122
app/src/main/java/ml/medyas/bakingapp/Classes/ExoPlayerManager.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package ml.medyas.bakingapp.Classes; | ||
|
||
|
||
/* | ||
* Copyright 2016 The Android Open Source Project | ||
* Copyright 2017 Rúben Sousa | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.widget.ImageView; | ||
|
||
import com.google.android.exoplayer2.DefaultLoadControl; | ||
import com.google.android.exoplayer2.DefaultRenderersFactory; | ||
import com.google.android.exoplayer2.ExoPlayerFactory; | ||
import com.google.android.exoplayer2.LoadControl; | ||
import com.google.android.exoplayer2.SimpleExoPlayer; | ||
import com.google.android.exoplayer2.source.ExtractorMediaSource; | ||
import com.google.android.exoplayer2.source.MediaSource; | ||
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; | ||
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; | ||
import com.google.android.exoplayer2.trackselection.TrackSelection; | ||
import com.google.android.exoplayer2.trackselection.TrackSelector; | ||
import com.google.android.exoplayer2.ui.PlayerView; | ||
import com.google.android.exoplayer2.upstream.DataSource; | ||
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; | ||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; | ||
import com.google.android.exoplayer2.util.Util; | ||
|
||
|
||
public class ExoPlayerManager { | ||
|
||
private MediaSource mediaSourceBuilder; | ||
private PlayerView playerView; | ||
private SimpleExoPlayer player; | ||
private String thumbnailsUrl; | ||
private ImageView imageView; | ||
private Context context; | ||
|
||
|
||
public ExoPlayerManager(PlayerView playerView, Context context) { | ||
this.playerView = playerView; | ||
this.context = context; | ||
} | ||
|
||
public void play(Uri uri) { | ||
// Produces DataSource instances through which media data is loaded. | ||
DataSource.Factory dataSourceFactory = | ||
new DefaultDataSourceFactory(context, Util.getUserAgent(context, "BackingApp")); | ||
this.mediaSourceBuilder = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri); | ||
} | ||
|
||
public SimpleExoPlayer getPlayer() { | ||
return player; | ||
} | ||
|
||
public void onStart() { | ||
if (Util.SDK_INT > 23) { | ||
createPlayers(); | ||
} | ||
} | ||
|
||
public void onResume() { | ||
if (Util.SDK_INT <= 23) { | ||
createPlayers(); | ||
} | ||
} | ||
|
||
public void onPause() { | ||
if (Util.SDK_INT <= 23) { | ||
releasePlayers(); | ||
} | ||
} | ||
|
||
public void onStop() { | ||
if (Util.SDK_INT > 23) { | ||
releasePlayers(); | ||
} | ||
} | ||
|
||
|
||
private void releasePlayers() { | ||
if (player != null) { | ||
player.release(); | ||
player = null; | ||
} | ||
} | ||
|
||
private void createPlayers() { | ||
if (player != null) { | ||
player.release(); | ||
} | ||
player = createFullPlayer(); | ||
playerView.setPlayer(player); | ||
} | ||
|
||
private SimpleExoPlayer createFullPlayer() { | ||
TrackSelection.Factory videoTrackSelectionFactory | ||
= new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter()); | ||
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); | ||
LoadControl loadControl = new DefaultLoadControl(); | ||
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance( | ||
new DefaultRenderersFactory(playerView.getContext()), | ||
trackSelector, loadControl); | ||
player.setPlayWhenReady(false); | ||
player.prepare(mediaSourceBuilder); | ||
return player; | ||
} | ||
} |
This file contains 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
This file contains 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