'Android online mediaplayer with seekbar and play pause button
I want to make a list in recyclerview which each item has a play pause button and a seekbar. I finally used this code to make it, but when I click on play button, it crashes and it is not working.
This code works when your media is offline, but when I want to use it on online mode and get the file from a URL, it cause problem.
and the error of android studio indicate to
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setAudioStreamType(int)' on a null object reference at com.example.wp.VersionAdapter4.startmediaplayer(VersionAdapter4.java:252) at com.example.wp.VersionAdapter4.lambda$onBindViewHolder$0$com-example-wp-VersionAdapter4(VersionAdapter4.java:61) at com.example.wp.VersionAdapter4$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
and it cant get the file as string
How can I fix this?
public class VersionAdapter4 extends RecyclerView.Adapter<VersionAdapter4.VersionViewHolder>{
private MediaPlayer mediaPlayer;
Context context;
VersionAdapter4 adapter;
private final ArrayList<Version4> versionlist;
private int currentPlayingPosition;
private final SeekbarUpdater seekbarUpdater;
private VersionViewHolder playingHolder;
public VersionAdapter4(Context context, ArrayList<Version4> versionlist) {
this.versionlist = versionlist;
this.currentPlayingPosition = -1;
seekbarUpdater = new SeekbarUpdater();
this.context = context;
}
@NonNull
@Override
public VersionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VersionViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.raw3, parent, false));
}
@Override
public void onBindViewHolder(@NonNull VersionViewHolder holder, int position) {
Version4 version4 = versionlist.get(position);
boolean isExpandable = versionlist.get(position).isExpandable();
holder.expandableLayout.setVisibility(isExpandable ? View.VISIBLE : View.GONE);
if (position == currentPlayingPosition) {
playingHolder = holder;
updateplayingview();
} else {
updatenoneplayingview(holder);
}
}
public void onViewRecycled(VersionViewHolder holder) {
super.onViewRecycled(holder);
if (currentPlayingPosition == holder.getAdapterPosition()) {
updatenoneplayingview(playingHolder);
playingHolder = null;
}
}
private void updatenoneplayingview(VersionViewHolder holder) {
holder.sbProgress.removeCallbacks(seekbarUpdater);
holder.sbProgress.setEnabled(false);
holder.sbProgress.setProgress(0);
holder.btnplay.setText("play");
}
private void updateplayingview() {
playingHolder.sbProgress.setMax(mediaPlayer.getDuration());
playingHolder.sbProgress.setProgress(mediaPlayer.getCurrentPosition());
playingHolder.sbProgress.setEnabled(true);
if (mediaPlayer.isPlaying()) {
playingHolder.sbProgress.postDelayed(seekbarUpdater, 100);
playingHolder.btnplay.setText("pause");
} else {
playingHolder.sbProgress.removeCallbacks(seekbarUpdater);
playingHolder.btnplay.setText("play");
}
}
private class SeekbarUpdater implements Runnable {
@Override
public void run() {
if (null != playingHolder && null != mediaPlayer) {
playingHolder.sbProgress.setProgress(mediaPlayer.getCurrentPosition());
playingHolder.sbProgress.postDelayed(this, 100);
}
}
}
@Override
public int getItemCount() {
return versionlist.size();
}
public class VersionViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
// Version3 version = versionsList.get(i);
SeekBar sbProgress;
Button btnplay;
RelativeLayout expandableLayout;
public VersionViewHolder(@NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.linear);
expandableLayout = itemView.findViewById(R.id.expandable);
btnplay = itemView.findViewById(R.id.track_play);
btnplay.setOnClickListener(this);
sbProgress = itemView.findViewById(R.id.seekbar);
sbProgress.setOnSeekBarChangeListener(this);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Version4 version4 = versionlist.get(getAdapterPosition());
version4.setExpandable(!version4.isExpandable());
notifyItemChanged(getAdapterPosition());
}
});
}
@Override
public void onClick(View view) {
if (getAdapterPosition() == currentPlayingPosition) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
} else {
currentPlayingPosition = getAdapterPosition();
if (mediaPlayer != null) {
if (null != playingHolder) {
updatenoneplayingview(playingHolder);
}
mediaPlayer.release();
}
playingHolder = this;
try {
startmediaplayer(versionlist.get(currentPlayingPosition).getSong());
} catch (IOException e) {
e.printStackTrace();
}
}
updateplayingview();
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
mediaPlayer.seekTo(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
private void startmediaplayer(String getsong) throws IOException {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.reset();
mediaPlayer.setDataSource(getsong);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
releasemediaplayer();
}
});
}
private void releasemediaplayer(){
if (null!=playingHolder){
updatenoneplayingview(playingHolder);
}
mediaPlayer.release();
mediaPlayer=null;
currentPlayingPosition=-1;
}
}
Solution 1:[1]
Try implementing a preparelistenerand prepareAsync.
mMediaPlayer.setDataSource();
mMediaPlayer.setOnPreparedListener();
mMediaPlayer.prepareAsync();
Solution 2:[2]
Technically you can create all the servesr with any number of server instances (service layers) you want - all running on the same database. That will work just fine.
However there is no built-in load balancing so you would have to handle that on your own.
The most common way of doing this is simply by providing different shortcuts to different groups of users. Each shortcut is linked to their own server instance and thus the users are directed to different servers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GGK stands for Ukraine |
| Solution 2 | kaspermoerch |
