ExoPlayer问题

时间:2017-11-16 21:00:03

标签: java android exoplayer

当我按下按钮返回上一个活动时,exoplayer继续播放声音。为什么呢?

这是我的代码:

public class MainActivity extends AppCompatActivity {

    SimpleExoPlayerView exoPlayerView;
    SimpleExoPlayer exoPlayer;
    String videoURL = "myurl";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        exoPlayerView = (SimpleExoPlayerView) findViewById(R.id.exo_player_view);
     try {


         BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
         TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
         exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

         Uri videoURI = Uri.parse(videoURL);

         DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
         ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
         MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);

         exoPlayerView.setPlayer(exoPlayer);
         exoPlayer.prepare(mediaSource);
         exoPlayer.setPlayWhenReady(true);
     }catch (Exception e){
         Log.e("MainAcvtivity"," exoplayer error "+ e.toString());
     }

    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这是因为您没有释放资源。只需使用此代码

@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT <= 23) {
 exoPlayer.release();
}

}

@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT > 23) {
   exoPlayer.release();
 }
}