Java 无法在模拟器中播放声音

Java 无法在模拟器中播放声音,java,android,android-studio,button,audio,Java,Android,Android Studio,Button,Audio,我在一个“原始”文件中设置了一个声音。然而,当我把所有的东西放在一起时,emulator上原来的点击声音正在播放,而我的“鼠标点击”声音却没有 我已将鼠标单击声音设置为“sound1” 声音只有1秒长,我不知道这是否重要 主要活动 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ap

我在一个“原始”文件中设置了一个声音。然而,当我把所有的东西放在一起时,emulator上原来的点击声音正在播放,而我的“鼠标点击”声音却没有

我已将鼠标单击声音设置为“sound1”

声音只有1秒长,我不知道这是否重要

主要活动

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity1" />

    <ImageButton
        android:id="@+id/Special_Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:src="@drawable/button_images"
        android:onClick="playSound"/>

</LinearLayout>

看起来您已经在XML中实现了onClick,在Java中实现了setOnClickListener

据我所知,您的代码正在运行onClickListener,它正在启动openActivity2()方法,并跳过playSound()方法

尝试删除XML中的onClick并将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);
      }
   }
}

好的,您是否尝试过实现AudioFocus请求?我将编辑我的答案,以包含播放媒体文件的工作代码。@Longhorns2102是否已修复?
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);
      }
   }
}