Похоже, что вы реализовали onClick в вашем XML, а также setOnClickListener в вашем Java.
Итак, насколько я могу судить, ваш код запускает onClickListener, который запускает ваш метод openActivity2 (), и пропускает метод playSound ().
Попробуйте удалить onClick в XML и интегрировать playSound () в setOnClickListener .
Вы также можете добавить запрос AudioFocus. Этот код работает: в моем случае я использовал класс фрагмента, но это можно использовать в любом упражнении.
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class fragment1 extends Fragment {
//These are the declarations
Button btn;
MediaPlayer mMediaPlayer;
private AudioManager mAudioManager;
//This tells the media player what to do if AudioFocus is changed
private AudioManager.OnAudioFocusChangeListener
mOnAudioFocusChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN){
mMediaPlayer.start();
}else if (focusChange == AudioManager.AUDIOFOCUS_LOSS){
releaseMediaPlayer();
}
}
};
//This tells the media player what to do when the playback is done
private MediaPlayer.OnCompletionListener mOnCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
releaseMediaPlayer();
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.**YOUR LAYOUT HERE**,container,false);
//This assigns the audio manager to this view
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
btn = view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* First calls the releaseMediaPlayer() method to make sure there
* are no other instances of the media player that exist.
*/
releaseMediaPlayer();
//This requests the AudioFocus
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
//This if statement deals with the above request
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
mMediaPlayer = MediaPlayer.create(getActivity(), **YOUR MEDIA RESOURCE HERE**);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
}
}
});
return view;
}
//This tells the media player to stop if the app is closed
@Override
public void onStop(){
super.onStop();
releaseMediaPlayer();
}
//This method is to release the instance of the media player
private void releaseMediaPlayer(){
if (mMediaPlayer != null){
mMediaPlayer.release();
mMediaPlayer = null;
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
}