How it works now, I click on R.id.b1, the animation is played, the melody starts to play, everything that is needed, but also needs to be done so that if the second time I clicked on R.id.b1 the mediplayer stopped and if the 3rd clicked on R.id.b1, a new playback started, but in fact, if I click on R.id.b1 again, the song just starts playing again, but this is due to mediaPlayer.reset (); but this code is necessary for me, because it resets the old one. melody if i play new
if (pageNumber == 1) { rootViewB = inflater.inflate(R.layout.fragment2, container, false); for (int index = 0; index < buttonIds.length; index++) { arr_imageB[index] = (ImageView) rootViewB.findViewById(buttonIds[index]); } for (int j = 0; j < arr_imageB.length; j++) { arr_imageB[j].setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { for (int index = 0; index < buttonIds.length; index++) { arr_imageB[index].clearAnimation(); //очищаю анимацию } switch (v.getId()) { case R.id.b1: { //запуск анимации v.startAnimation(anim); /воспроизвожу мелодию playSample(soundsRawResIds[0]); } break; Method code in which I try to stop the player: Now this way: I chose R.id.b1: igaret melody, clicked again R.id.b1: stopped, clicked again the melody does not start, but should
private void playSample(int resid) { AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(resid); if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } else { try { //mediaPlayer.reset(); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); mediaPlayer.prepareAsync(); afd.close(); } catch (IllegalArgumentException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } catch (IllegalStateException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } catch (IOException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } } }