实施新发布的ExoPlayer 2

时间:2016-09-19 21:45:30

标签: android exoplayer

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

3 个答案:

答案 0 :(得分:5)

这是通过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);

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

答案 1 :(得分:1)

以下是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) {

}
}

答案 2 :(得分:0)

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()
}
}