无法在模拟器中播放声音

时间:2020-01-15 02:42:26

标签: java android android-studio button audio

我已在“原始”文件中设置了声音。但是,当我将所有内容放在一起时,正在播放模拟器上的原始喀哒声,但是没有播放“喀click声”。

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

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

activity_main

<?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>

主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();

        soundPool = new SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(audioAttributes)
                .build();


    } else {
        soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC,
                0);
    }

    sound1 = soundPool.load(this, R.raw.sound1, 1);

    button = findViewById(R.id.Special_Button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openActivity2();
        }
    });

}

public void openActivity2() {
    Intent intent = new Intent(this, Activity2.class);
    startActivity(intent);
}

public void playSound(View v) {
    soundPool.play(R.raw.sound1, 1,1,0
    ,0,1);
}

@Override
protected void onDestroy() {
    super.onDestroy();

    soundPool.release();
    soundPool = null;
}
}

1 个答案:

答案 0 :(得分:0)

好像您已经在XML中实现了 onClick ,在Java中实现了 setOnClickListener

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

尝试删除XML中的onClick,然后将playSound()集成到 setOnClickListener 中。

您还可以添加AudioFocus请求。 这段代码有效:就我而言,我使用了fragment类,但是可以在任何活动中使用。

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);
      }
   }
}
相关问题