无法在模拟器中播放声音

问题描述 投票:0回答:1

我已经在“原始”文件中设置了声音。但是,当我将所有内容放在一起时,正在播放模拟器上的原始喀哒声,但是没有播放“喀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;
}
}
java android android-studio button audio
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.