我在我的应用程序中使用exoplayer 3次,所以我认为最好在它自己的类中实现它并且只使用它3次。
在执行此操作时,我引入了内存泄漏,因此它泄漏了活动并导致应用程序崩溃。
我不确定为什么会这样,因为我没有关闭活动。我想这是因为活动或视图有一些变化。
如果你能告诉我我的实现有什么问题导致内存泄漏,我们将不胜感激。
这是我的exoplayer控件类:
public class ExoPlayerControls {
Context context;
SimpleExoPlayerView simpleExoplayerView;
SimpleExoPlayer player;
boolean preview;
public ExoPlayerControls(Context context, SimpleExoPlayerView simpleExoplayerView, SimpleExoPlayer player, Boolean preview) {
this.context = context;
this.simpleExoplayerView = simpleExoplayerView;
this.player = player;
this.preview = preview;
}
public boolean setUPExoPlayer (String alarmMediaLocation){
try {
if (player != null){
player.setPlayWhenReady(false);
//URI location of audio
Uri mediaURI = Uri.parse(alarmMediaLocation);
// Data source Factory
DefaultHttpDataSourceFactory datasourceFactory = new DefaultHttpDataSourceFactory("alarm video");
// Extractor Factory
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
// Media source
MediaSource mediasource = new ExtractorMediaSource(mediaURI, datasourceFactory, extractorsFactory, null, null);
// Prepare the exoplayer
player.prepare(mediasource);
player.setPlayWhenReady(false);
} else {
// Create a bandwith meter
BandwidthMeter bandwidthmeter = new DefaultBandwidthMeter();
// Create a track selector
TrackSelector trackselector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthmeter));
// Create the player
player = ExoPlayerFactory.newSimpleInstance(context, trackselector);
//URI location of audio
Uri mediaURI = Uri.parse(alarmMediaLocation);
// Data source Factory
DefaultHttpDataSourceFactory datasourceFactory = new DefaultHttpDataSourceFactory("alarm video");
// Extractor Factory
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
// Media source
MediaSource mediasource = new ExtractorMediaSource(mediaURI, datasourceFactory, extractorsFactory, null, null);
// Bind the player to the view.
simpleExoplayerView.setPlayer(player);
// Prepare the exoplayer
player.prepare(mediasource);
player.setPlayWhenReady(false);
}
} catch (Error e){
Toast.makeText(context, "Exoplayer error" + e, Toast.LENGTH_SHORT).show();
}
return true;
}
public void playAlarmMedia(String alarmMediaLocation){
try {
boolean exoPlayerSet = setUPExoPlayer(alarmMediaLocation);
if (exoPlayerSet){
// Tell exoplayer to start playing immediately
player.setPlayWhenReady(true);
}
} catch (Error e){
Toast.makeText(context, "Exoplayer error" + e, Toast.LENGTH_SHORT).show();
}
}
}
我在我的活动中实现它:
// Initialise exoplayer controls
exoPlayerControls = new ExoPlayerControls(this, simpleExoplayerView, player, preview);
String alarmMediaLocation = downloadedAlarmToPlay.getAlarmMediaLocation();
exoPlayerControls.setUPExoPlayer(alarmMediaLocation);