我正在实现一个音乐应用程序。我正在使用Exoplayer2
我正在尝试使用DynamicConcatenatingMediaSource来动态添加和删除播放列表中的项目。
下面是我的实现。
public void initMusicPlayer(){
if (songs !=null)
{
MediaSource mediaSource;
ArrayList<MediaSource> sources = new ArrayList<>();
MusicItem song;
for (int i=0;i< songs.size();i++)
{
song = songs.get(i);
mediaSource = buildMediaSource(Uri.parse(song.getMusicUrl()));
sources.add(mediaSource);
}
dynamicMediaSource = new DynamicConcatenatingMediaSource();
dynamicMediaSource.addMediaSources(sources);
exoPlayer.prepare(dynamicMediaSource,false,false);
exoPlayer.addListener(this);
if (currentPlayingSongIndex == -1)
{
exoPlayer.setPlayWhenReady(false);
}
else
{
exoPlayer.seekTo(currentPlayingSongIndex,0);
exoPlayer.setPlayWhenReady(true);
}
}
}
public void addItemToPlaylist(MusicItem song,boolean shouldPlay){
long position = exoPlayer.getContentPosition();
MediaSource mediaSource = buildMediaSource(Uri.parse(song.getMusicUrl()));
dynamicMediaSource.addMediaSource(mediaSource);
exoPlayer.prepare(dynamicMediaSource,false,false);
if (shouldPlay)
{
exoPlayer.seekTo(currentPlayingSongIndex,0);
}
else
{
exoPlayer.seekTo(currentPlayingSongIndex,position);
}
}但是,这个实现并不起作用。它不能播放任何东西。
上面的代码有什么问题?
另外,如何动态添加项目到播放列表?上面的addItemToPlaylist能工作吗?
发布于 2018-05-02 16:52:50
这是我创建的函数,用于在Kotlin中播放mp3文件的播放列表。(你可以在Android Studio中复制粘贴它,如果你使用Java,它会被转换成Java)
private fun mediaPlayerConfiguration() {
val bandwidthMeter = DefaultBandwidthMeter()
val extractorsFactory = DefaultExtractorsFactory()
val trackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
val trackSelector = DefaultTrackSelector(trackSelectionFactory)
val defaultBandwidthMeter = DefaultBandwidthMeter()
val dataSourceFactory = DefaultDataSourceFactory(this,
Util.getUserAgent(this, "mediaPlayerSample"), defaultBandwidthMeter)
val dynamicConcatenatingMediaSource = DynamicConcatenatingMediaSource()
for (item in myActivity.list) {
var mediaSource = ExtractorMediaSource.Factory(dataSourceFactory).setExtractorsFactory(extractorsFactory).createMediaSource(Uri.parse(item))
dynamicConcatenatingMediaSource.addMediaSource(mediaSource)
}
mPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
mPlayer.prepare(dynamicConcatenatingMediaSource)
mPlayer.playWhenReady = true
}https://stackoverflow.com/questions/46985480
复制相似问题