Это мой код для остановки активности аудиопроигрывателя при использовании функции audio player.stop () без использования , если условие вызывает Исключение нулевого указателя Ошибка.
public void onBackPressed() {
super.onBackPressed();
if(audioPlayer != null)
audioPlayer.stop();
if (mSimpleExoPlayer != null)
mSimpleExoPlayer.stop();
audioWriteAdapters.audioPlayPlayer.stop();
}
}
вот код для аудиоплеера страницы адаптера, я хочу Остановить аудиоплеер от активности. Первоначально присвойте аудиоплеер как ноль, внутри значения настройки аудиопроигрывателя функциональности из массива list.audio проигрыватель работает нормально в представлении адаптера.
public class AudioWriteAdapter extends BaseAdapter {
public MediaPlayer audioPlayPlayer = null;
public boolean isPlaying = false;
String finalTimerString = "";
String secondsString = "";
private ArrayList<MessageAudio> messageAudioArrayList;
private LayoutInflater inflater;
private Context context;
private ItemClickListener clickListener;
private Runnable mRunnable;
private Handler mHandler;
public AudioWriteAdapter(Context context){
this.context = context;
}
public AudioWriteAdapter(ArrayList<MessageAudio> messageAudioArrayList, Context mContext, ItemClickListener clickListener) {
this.messageAudioArrayList = messageAudioArrayList;
this.context = mContext;
this.clickListener = clickListener;
}
@Override
public int getCount() {
return messageAudioArrayList.size();
}
@Override
public Object getItem(int i) {
return messageAudioArrayList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
public void clearMediaPlayer() {
if (audioPlayPlayer != null) {
audioPlayPlayer.release();
audioPlayPlayer = null;
}
}
@Override
public View getView(final int i, final View view, ViewGroup viewGroup) {
View rowView = view;
if (rowView == null) {
rowView = LayoutInflater.from(context).inflate(R.layout.audio_view_pagers, null);
final TextView typeTitle, typeDate;
final ImageView typeDelete, typeEdit, viewRecord, pauseViewAudio, playViewAudio;
final LinearLayout viewAudioLayout;
final TextThumbSeekBar audioViewSeekbar;
typeTitle = rowView.findViewById(R.id.typeTitle);
typeDelete = rowView.findViewById(R.id.typeDelete);
typeEdit = rowView.findViewById(R.id.typeEdit);
viewRecord = rowView.findViewById(R.id.viewRecord);
viewAudioLayout = rowView.findViewById(R.id.viewAudioLayout);
pauseViewAudio = rowView.findViewById(R.id.pauseViewAudio);
playViewAudio = rowView.findViewById(R.id.playViewAudio);
audioViewSeekbar = rowView.findViewById(R.id.audioViewSeekbar);
//typeDate = typeLayout.findViewById(R.id.typeDate);
typeTitle.setText(messageAudioArrayList.get(i).getTitle());
//rowView.setTag("1");
viewRecord.setTag(messageAudioArrayList.get(i).getPath());
viewRecord.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
mHandler = new Handler();
viewAudioLayout.setVisibility(View.VISIBLE);
if (audioPlayPlayer == null) {
audioPlayPlayer = new MediaPlayer();
try {
audioPlayPlayer.setDataSource(String.valueOf(viewRecord.getTag()));
audioPlayPlayer.prepare();
} catch (java.io.IOException e) {
e.printStackTrace();
}
audioPlayPlayer.start();
if (pauseViewAudio.getVisibility() == View.GONE) {
pauseViewAudio.setVisibility(View.VISIBLE);
}
playViewAudio.setVisibility(View.GONE);
isPlaying = true;
final int duration = audioPlayPlayer.getDuration();
audioViewSeekbar.setProgress(0);
audioViewSeekbar.setMax(duration / 1000);
mRunnable = new Runnable() {
@Override
public void run() {
if (audioPlayPlayer != null) {
int mCurrentPosition = audioPlayPlayer.getCurrentPosition() / 1000; // In milliseconds
audioViewSeekbar.setProgress(mCurrentPosition);
String Total = ((duration / 1000) / 60) + ":" + (duration / 1000) % 60;
int totalLength = duration / 1000;
int remainingLength = (duration - audioPlayPlayer.getCurrentPosition()) - duration;
Log.e("Duration == >", "Total length ==>" + totalLength + " Remaining ==>" + remainingLength);
String remainingSeconds = String.valueOf(((remainingLength / 1000) % 60) - ((duration / 1000) / 60));
if (remainingSeconds.length() < 2) {
remainingSeconds = "0" + remainingSeconds;
}
String remainingTotal = ((remainingLength / 1000) / 60) + ":" + remainingSeconds;
Log.e("Remaining Duration == >", " Remaining ==>" + remainingTotal);
//seekBar.setProgressText(remainingTotal+"/"+Total);
Log.e("Cuurent Position is==>", String.valueOf(mCurrentPosition));
audioViewSeekbar.setProgressText(milliSecondsToTimer(audioPlayPlayer.getCurrentPosition()) + "/" + Total);
}
mHandler.postDelayed(mRunnable, 1000);
}
};
mHandler.postDelayed(mRunnable, 1000);
}
}
}
);
audioViewSeekbar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//audioPlayPlayer.seekTo(i*1000);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
pauseViewAudio.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
audioPlayPlayer.pause();
if (mHandler != null) {
mHandler.removeCallbacks(mRunnable);
}
isPlaying = false;
playViewAudio.setVisibility(View.VISIBLE);
pauseViewAudio.setVisibility(View.GONE);
}
}
);
playViewAudio.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isPlaying) {
if (audioPlayPlayer != null) {
audioPlayPlayer.start();
isPlaying = true;
pauseViewAudio.setVisibility(View.VISIBLE);
playViewAudio.setVisibility(View.GONE);
final int duration = audioPlayPlayer.getDuration();
mRunnable = new Runnable() {
@Override
public void run() {
if (audioPlayPlayer != null) {
int mCurrentPosition = audioPlayPlayer.getCurrentPosition() / 1000; // In milliseconds
audioViewSeekbar.setProgress(mCurrentPosition);
String Total = ((duration / 1000) / 60) + ":" + (duration / 1000) % 60;
int totalLength = duration / 1000;
int remainingLength = (duration - audioPlayPlayer.getCurrentPosition()) - duration;
Log.e("Duration == >", "Total length ==>" + totalLength + " Remaining ==>" + remainingLength);
String remainingSeconds = String.valueOf(((remainingLength / 1000) % 60) - ((duration / 1000) / 60));
if (remainingSeconds.length() < 2) {
remainingSeconds = "0" + remainingSeconds;
}
String remainingTotal = ((remainingLength / 1000) / 60) + ":" + remainingSeconds;
Log.e("Remaining Duration == >", " Remaining ==>" + remainingTotal);
//seekBar.setProgressText(remainingTotal+"/"+Total);
Log.e("Cuurent Position is==>", String.valueOf(mCurrentPosition));
audioViewSeekbar.setProgressText(milliSecondsToTimer(audioPlayPlayer.getCurrentPosition()) + "/" + Total);
}
mHandler.postDelayed(mRunnable, 1000);
}
};
mHandler.postDelayed(mRunnable, 1000);
}
}
}
}
);
typeDelete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (clickListener != null) {
//int pos = businessPartnerModelArrayList.indexOf(businessPartnerModelArrayFilteredList.get(getAdapterPosition()));
clickListener.onWriteDeleteClick("audio", i);
}
}
}
);
}
return rowView;
}
private String milliSecondsToTimer(long milliseconds) {
// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}
// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
finalTimerString = minutes + ":" + secondsString;
// return timer string
return finalTimerString;
}
protected void initializeAudioSeekBar() {
}
}