Reputation: 2886
I am switching from mediaplayer to exoplayer in my project and because there is few documentation I wanted to ask some questions here.
I successfully created the player:
exoPlayer = ExoPlayer.Factory.newInstance(1);
playerControl = new PlayerControl(exoPlayer);
and the music starts playing when I click on my music list:
musicList.addOnItemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, final int position) {
mediaController.show(0);
currentPosition = position;
URL = musicUrl.get(position);
Uri uri = Uri.parse(URL);
sampleSource = new DefaultSampleSource(new FrameworkSampleExtractor(getApplicationContext(), uri, null), 2);
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource, null, true);
exoPlayer.prepare(audioRenderer);
exoPlayer.setPlayWhenReady(true);
}
})
);
The problem is I don't know how to change the music if I click on another track on the list, if the track number 2 is playing if I click on track numer 6 to change to it.If anyone can guide me with this problem it will be perfect.
I really readed all topics from stackoverflow and I can't find the solution in theyr demo app because there is playing only 1 track, they don't change it.
Upvotes: 0
Views: 4223
Reputation: 69
Maybe my answer help u. Im using exoplayer on my app and doing something like this. I not initialize ExoPlayer but this is hard to configure. I just create View
<com.devbrackets.android.exomedia.ui.widget.EMVideoView
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And in fragment i initialize it
@Bind(R.id.video_player)
EMVideoView emVideoView;
With butterknife
and for opening music, videos i create CustomAdapter with recyclerView, where i configuring everything for playing content.
so my aplication look like
PlayerActivity>
CustomPlayerFragment>
VideoGridAdapter>
Upvotes: 0
Reputation: 3
Maybe you need to read about this post. https://github.com/google/ExoPlayer/issues/463
I was confused about the same situation as you. After reading the post, I found that I was using the same extractor every time. So the first track was able to be played and when it came to the second one, the player ramained in the state of ExoPlayer.STATE_BUFFERING.
so, for you, i recommend you to look at your DefaultSampleSource to find if you made a similar mistake.
Upvotes: 0
Reputation: 1208
at the top of your onItemClick method you should do a check and pause/stop the player.
if (exoPlayer != null) {
exoPlayer.setPlayWhenReady(false);
exoPlayer.stop();
exoPlayer.seekTo(0);
}
mediaController.show(0);
...
YOUR LOGIC + CHANGE SAMPLE & RENDERERS
...
exoPlayer.prepare(audioRenderer);
exoPlayer.setPlayWhenReady(true);
This should do the trick
Upvotes: 0
Reputation: 64
Hey if you implement ExoPlayer as in the Demo from google there is ths info about the method stop():
Calling this method does not reset the playback position. If this player instance will be used. * to play another video/audio from its start, then {@code seekTo(0)} should be called after stopping the player and before preparing it for the next video.
Regards
Upvotes: 1