--- lang: ja-jp breaks: true --- # Android `SoundPool` を使用してサウンドを鳴らす 2021-08-05 > [Android] SoundPool で効果音を鳴らしてみる > https://akira-watson.com/android/soundpool.html#2 ## `wav`ファイルを`raw`ディレクトリに格納 ![](https://i.imgur.com/uIQqpS7.png) ## activity_main.xml ```xml= <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="154dp" android:layout_marginTop="72dp" android:layout_marginEnd="163dp" android:text="Button" android:onClick="OnClickButton1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="HardcodedText,UsingOnClickInXml" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` ![](https://i.imgur.com/UYlDPZF.png) ## MainActivity.java ```java= public class MainActivity extends AppCompatActivity { private SoundPool soundPool; private int soundClear; private int soundGetCin; private int soundPoCo; private int soundPopup; private int soundQuizRight; private int soundStart; private int soundWarning; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AudioAttributes audioAttributes = new AudioAttributes.Builder() // USAGE_MEDIA // USAGE_GAME .setUsage(AudioAttributes.USAGE_GAME) // CONTENT_TYPE_MUSIC // CONTENT_TYPE_SPEECH, etc. .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); soundPool = new SoundPool.Builder() .setAudioAttributes(audioAttributes) // ストリーム数に応じて .setMaxStreams(7) .build(); soundClear = soundPool.load(this, R.raw.clear , 1); soundGetCin = soundPool.load(this, R.raw.get_cin , 1); soundPoCo = soundPool.load(this, R.raw.po_co , 1); soundPopup = soundPool.load(this, R.raw.popup , 1); soundQuizRight = soundPool.load(this, R.raw.quiz_right , 1); soundStart = soundPool.load(this, R.raw.start , 1); soundWarning = soundPool.load(this, R.raw.warning , 1); Log.i("setOnLoadCompleteListener","ui thread_id="+Thread.currentThread().getId()); soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { Log.i("setOnLoadCompleteListener","worker thread_id="+Thread.currentThread().getId()); Log.i("setOnLoadCompleteListener","sampleId ="+sampleId); Log.i("setOnLoadCompleteListener","status ="+status); } }); } public void OnClickButton1(View view) { Log.i("OnClickButton1","ui thread_id="+Thread.currentThread().getId()); // *.wav の再生 // play(ロードしたID, 左音量, 右音量, 優先度, ループ,再生速度) soundPool.play(soundClear , 1.0f, 1.0f, 0, 0, 1); sleep(); soundPool.play(soundGetCin , 1.0f, 1.0f, 0, 0, 1); sleep(); soundPool.play(soundPoCo , 1.0f, 1.0f, 0, 0, 1); sleep(); soundPool.play(soundPopup , 1.0f, 1.0f, 0, 0, 1); sleep(); soundPool.play(soundQuizRight , 1.0f, 1.0f, 0, 0, 1); sleep(); soundPool.play(soundStart , 1.0f, 1.0f, 0, 0, 1); sleep(); soundPool.play(soundWarning , 1.0f, 1.0f, 0, 0, 1); sleep(); // ボタンの回転アニメーション RotateAnimation buttonRotation = new RotateAnimation(0, 360, view.getWidth()/2, view.getHeight()/2); buttonRotation.setDuration(2000); view.startAnimation(buttonRotation); } private void sleep() { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } ``` ###### tags: `Android` `SoundPool` `音を鳴らす`