Please explain to me how to properly use the player + SeekBar
. using the method of stopping is not an error, but the program crashes when starting and stopping a few songs. using realase error output
error
I run the player and seekbar:
holder.start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
releaseMP();
try {
global_position = position;
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(recordBeans.get(global_position).getFile());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
holder.seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.start();
startPlayProgressUpdater(holder.seekBar);
}
});
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
mediaPlayer = null;
}
});
}
});
error points to a string
recordBeans.get(global_position).setSeekPos(mediaPlayer.getCurrentPosition());
Start the player and upgrade method seekbar:
public void startPlayProgressUpdater(final SeekBar seekBar) {
if (mediaPlayer != null) {
recordBeans.get(global_position).setSeekPos(mediaPlayer.getCurrentPosition());
} else {
recordBeans.get(global_position).setSeekPos(0);
}
if ((Integer) seekBar.getTag() == global_position) {
seekBar.setProgress(recordBeans.get(global_position).getSeekPos());
}
if (mediaPlayer != null) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater(seekBar);
}
};
handler.postDelayed(notification, 1000);
}
}
Stop button:
holder.stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((Integer) v.getTag() == global_position && mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
});
When seekbar
comes to an end:
java.lang.NullPointerException
at com.example.MANAGMENT.fragments.adapters.RecordAdapter$4.run(RecordAdapter.java:165)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
try
wherever u r releasing media player