2021.07.11 - [안드로이드] - [안드로이드] 이미지 리스트로 보여주는 방법 Picasso Recyclerview
[안드로이드] 이미지 리스트로 보여주는 방법 Picasso Recyclerview
2021.06.30 - [안드로이드] - [안드로이드] 내가 선택한 색으로 배경색 변경하는 방법 [안드로이드] 내가 선택한 색으로 배경색 변경하는 방법 2021.01.22 - [안드로이드] - [안드로이드] 안드로이드 스튜
aries574.tistory.com
2021.07.13 - [안드로이드] - [안드로이드] 이미지 리스트 다운로드하는 방법 DownloadManager
[안드로이드] 이미지 리스트 다운로드하는 방법 DownloadManager
2021.06.28 - [안드로이드] - [안드로이드]정해진 시간에 알람 울리는 방법 Notification TimePicker [안드로이드]정해진 시간에 알람 울리는 방법 Notification TimePicker 2020.12.25 - [안드로이드] - [안드로..
aries574.tistory.com
이번 시간에는 버튼을 누르면 효과음이 나오는 방법을 알아보겠습니다.
(1) res -> New -> Android Resource Directory
(2) Resource type -> raw
(3) 효과음 파일 넣기
생성된 raw폴더에 효과음 파일(wav)을 넣어주시면 됩니다.
버튼을 4개 생성하고, onClick에 playSound 등록합니다.
메인코드에서 클릭 시, 효과음을 발생시킬 것입니다.
<?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">
<Button
android:id="@+id/btn_sound1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playSound"
android:text="sound1">
</Button>
<Button
android:id="@+id/btn_sound2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playSound"
android:text="sound2">
</Button>
<Button
android:id="@+id/btn_sound3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playSound"
android:text="sound3">
</Button>
<Button
android:id="@+id/btn_sound4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playSound"
android:text="sound4">
</Button>
</LinearLayout>
(1) 효과음에 사용되는 SoundPool 객체를 생성
(2) 버튼 4개에 들어갈 효과음이 sound1 ~ sound4 변수를 생성합니다.
(3) 버튼마다 다른 효과음이 나오도록 switch로 나눠줍니다.
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private SoundPool soundPool;
private int sound1, sound2, sound3, sound4;
@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(6)
.setAudioAttributes(audioAttributes)
.build();
}else{
soundPool = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
}
sound1 = soundPool.load(this, R.raw.sound1, 1 );
sound2 = soundPool.load(this, R.raw.sound2, 1 );
sound3 = soundPool.load(this, R.raw.sound3, 1 );
sound4 = soundPool.load(this, R.raw.sound4, 1 );
}
public void playSound(View v){
switch (v.getId()){
case R.id.btn_sound1:
// 재생시킬파일, 왼쪽볼륨 크기, 오른쪽볼륨 크기, 우선순위, 재생횟수, 재생속도
soundPool.play(sound1, 1, 1, 0, 0, 1);
break;
case R.id.btn_sound2:
soundPool.play(sound2, 1, 1, 0, 0, 1);
break;
case R.id.btn_sound3:
soundPool.play(sound3, 1, 1, 0, 0, 1);
break;
case R.id.btn_sound4:
soundPool.play(sound4, 1, 1, 0, 0, 1);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//메모리 해제
soundPool.release();
soundPool = null;
}
}
맘에 드셨다면 공감 부탁드려요문의 댓글 환영합니다. |
[안드로이드] 자동완성 텍스트뷰 쉽게 만드는 방법 (0) | 2021.07.28 |
---|---|
[안드로이드] 간단하게 노래 재생하는 방법 MediaPlayer (0) | 2021.07.24 |
[안드로이드] 이미지 리스트 다운로드하는 방법 DownloadManager (0) | 2021.07.13 |
[안드로이드] 이미지 리스트로 보여주는 방법 Picasso Recyclerview (0) | 2021.07.11 |
[안드로이드] 내가 선택한 색으로 배경색 변경하는 방법 (0) | 2021.06.30 |
댓글 영역