要实现Android中使用ExoPlayer 2平滑切换音乐并实现重叠,你可以使用以下代码示例:
首先,在build.gradle文件中添加ExoPlayer的依赖:
implementation 'com.google.android.exoplayer:exoplayer:2.X.X'
然后,你可以创建一个ExoPlayerHelper类,该类封装了ExoPlayer的常用操作和事件监听器。
import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.LoopingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.util.Util;
public class ExoPlayerHelper {
private Context mContext;
private SimpleExoPlayer mExoPlayer;
private PlayerView mPlayerView;
private MediaSource mMediaSource;
private boolean mIsPlaying = false;
public ExoPlayerHelper(Context context, PlayerView playerView) {
mContext = context;
mPlayerView = playerView;
initializePlayer();
}
private void initializePlayer() {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector, loadControl);
mPlayerView.setPlayer(mExoPlayer);
}
public void play(String url) {
Uri uri = Uri.parse(url);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext,
Util.getUserAgent(mContext, mContext.getApplicationInfo().name));
mMediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.setExtractorsFactory(new DefaultExtractorsFactory())
.createMediaSource(uri);
mExoPlayer.prepare(mMediaSource);
mExoPlayer.setPlayWhenReady(true);
mIsPlaying = true;
}
public void stop() {
if (mExoPlayer != null) {
mExoPlayer.stop();
mExoPlayer.release();
mExoPlayer = null;
mPlayerView.setPlayer(null);
mIsPlaying = false;
}
}
public boolean isPlaying() {
return mIsPlaying;
}
public void setVolume(float volume) {
if (mExoPlayer != null) {
mExoPlayer.setVolume(volume);
}
}
public void setLooping(boolean looping) {
if (mMediaSource != null) {
mMediaSource = new LoopingMediaSource(mMediaSource);
}
}
public void setOnCompletionListener(final OnCompletionListener listener) {
if (mExoPlayer != null) {
mExoPlayer.addListener(new ExoPlayer.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {}
@Override
public void onLoadingChanged(boolean isLoading) {}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_ENDED) {
listener.onCompletion();
}
}
@Override
public void onPlayerError(ExoPlaybackException error) {}
@Override
public void onPositionDiscontinuity(int reason) {}
});
}
}
public interface OnCompletionListener {
void on