实施新发布的 ExoPlayer 2

Implementing the new released ExoPlayer 2

Exoplayer 库对我来说似乎太复杂了。谁能帮助我如何使用 Android 上新发布的 exoplayer 2 库来播放广播电台 url?我目前正在尝试从服务器流式传输 mp3 文件,并希望将播放器放在 IntentService 中以保持音频文件在后台播放。提前致谢。

这是通过 ExoPlayer2 获取音频流的非常基本的设置:

Handler handler = new Handler();
TrackSelector trackSelector = new DefaultTrackSelector(handler);
LoadControl loadControl = new DefaultLoadControl();
SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);

Uri audioUri = Uri.parse("http://stream.basso.fi:8000/stream");
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("ExoPlayerDemo");
ExtractorsFactory extractor = new DefaultExtractorsFactory();
MediaSource audioSource = new ExtractorMediaSource(audioUri, dataSourceFactory, extractor, null, null);
exoPlayer.prepare(audioSource);
exoPlayer.setPlayWhenReady(true);

是的,有很多障碍要跳过...:)

这里是 ExoPlayer for mp3 的简单演示

compile 'com.google.android.exoplayer:exoplayer:r2.4.2'

然后

    public class MainActivity extends AppCompatActivity implements ExoPlayer.EventListener{

private BandwidthMeter bandwidthMeter;
private TrackSelector trackSelector;
private TrackSelection.Factory trackSelectionFactory;
private SimpleExoPlayer player;
private DataSource.Factory dataSourceFactory;
private ExtractorsFactory extractorsFactory;
private DefaultBandwidthMeter defaultBandwidthMeter;
private MediaSource mediaSource;

private String songUrl = "http://www.mfiles.co.uk/mp3-downloads/edvard-grieg-peer-gynt1-morning-mood-piano.mp3";

private Button stopButton;
private Button startButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startButton = (Button) findViewById(R.id.btnStart);
    stopButton = (Button) findViewById(R.id.btnStop);

    bandwidthMeter = new DefaultBandwidthMeter();
    extractorsFactory = new DefaultExtractorsFactory();

    trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

    trackSelector = new DefaultTrackSelector(trackSelectionFactory);

    defaultBandwidthMeter = new DefaultBandwidthMeter();

    dataSourceFactory = new DefaultDataSourceFactory(this,
            Util.getUserAgent(this, "mediaPlayerSample"),defaultBandwidthMeter);


    mediaSource = new ExtractorMediaSource(Uri.parse(songUrl),
            dataSourceFactory,
            extractorsFactory,
            null,
            null);

    player = ExoPlayerFactory.newSimpleInstance(this,trackSelector);

    player.addListener(this);
    player.prepare(mediaSource);

    Log.v("TEST","playing state : " + player.getPlaybackState());

    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.setPlayWhenReady(true);
        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.setPlayWhenReady(false);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    player.setPlayWhenReady(false);
}


@Override
public void onLoadingChanged(boolean isLoading) {
    Log.i("TEST", "onLoadingChanged: " + isLoading + "");
    Log.i("TEST", "Buffered Position: " + player.getBufferedPosition() + "");
    Log.i("TEST", "Buffered Percentage: " + player.getBufferedPercentage() + "");
}

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

    if(playbackState == ExoPlayer.STATE_READY){
        Log.i("TEST", "ExoPlayer State is: READY");
    } else if (playbackState == ExoPlayer.STATE_BUFFERING){
        Log.i("TEST", "ExoPlayer State is: BUFFERING");
    } else if (playbackState == ExoPlayer.STATE_ENDED){
        Log.i("TEST", "ExoPlayer State is: ENDED");
    } else if (playbackState == ExoPlayer.STATE_IDLE){
        Log.i("TEST", "ExoPlayer State is: IDLE");
    }


}

@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {

}

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

}

@Override
public void onPlayerError(ExoPlaybackException error) {

}

@Override
public void onPositionDiscontinuity() {

}

@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

}
}
class RadioService : Service() {

private lateinit var exoPlayer: ExoPlayer

override fun onBind(p0: Intent?): IBinder? = null

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Timber.d("Radio is played")
val bandwidthMeter = DefaultBandwidthMeter()
val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
val streamUrl = intent?.getStringExtra(Constant.STREAM_URL)
exoPlayer = ExoPlayerFactory.newSimpleInstance(DefaultRenderersFactory(this), trackSelector, DefaultLoadControl())

val dataSourceFactory = DefaultDataSourceFactory(this,
    Util.getUserAgent(this, getString(R.string.app_name)), bandwidthMeter)

val hlsMediaSource = HlsMediaSource(Uri.parse(streamUrl), dataSourceFactory, Handler(), null)
exoPlayer.prepare(hlsMediaSource)
exoPlayer.playWhenReady = true
return START_NOT_STICKY
}

override fun onDestroy() {
Timber.d("Radio is stopped")
exoPlayer.playWhenReady = false
super.onDestroy()
}
}