[英]SoundPool media playing as ringtone not as media
public class Soundboard extends AppCompatActivity {
private SoundPool soundPool;
private int sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8, sound9, sound10, sound11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soundboard);
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(11)
.setAudioAttributes(audioAttributes)
.build();
} else {
soundPool = new SoundPool(11, AudioManager.STREAM_MUSIC, 0);
}
sound1 = soundPool.load(this, R.raw.tournament, 1);
sound2 = soundPool.load(this, R.raw.tournament2_fortnite, 1);
sound3 = soundPool.load(this, R.raw.uuuaaaa, 1);
sound4 = soundPool.load(this, R.raw.lass_heute_fortnite, 1);
sound5 = soundPool.load(this, R.raw.kannst_du_fortnite, 1);
sound6 = soundPool.load(this, R.raw.fortnite_mit_dir, 1);
sound7 = soundPool.load(this, R.raw.papapapaka, 1);
sound8 = soundPool.load(this, R.raw.haeusl, 1);
sound9 = soundPool.load(this, R.raw.cringe_musik, 1);
sound10 = soundPool.load(this, R.raw.lachen, 1);
sound11 = soundPool.load(this, R.raw.furz, 1);
}
public void playSound(View v) {
switch (v.getId()) {
case R.id.imageButton:
soundPool.play(sound1, 1, 1, 0, 0, 1);
break;
case R.id.imageButton2:
soundPool.play(sound2, 1, 1, 0, 0, 1);
break;
case R.id.imageButton3:
soundPool.play(sound3, 1, 1, 0, 0, 1);
break;
case R.id.imageButton4:
soundPool.play(sound4, 1, 1, 0, 0, 1);
break;
case R.id.imageButton5:
soundPool.play(sound5, 1, 1, 0, 0, 1);
break;
case R.id.imageButton6:
soundPool.play(sound6, 1, 1, 0, 0, 1);
break;
case R.id.imageButton7:
soundPool.play(sound7, 1, 1, 0, 0, 1);
break;
case R.id.imageButton8:
soundPool.play(sound8, 1, 1, 0, 0, 1);
break;
case R.id.imageButton9:
soundPool.play(sound9, 1, 1, 0, 0, 1);
break;
case R.id.imageButton10:
soundPool.play(sound10, 1, 1, 0, 0, 1);
break;
case R.id.imageButton11:
soundPool.play(sound11, 1, 1, 0, 0, 1);
break;
}
}
你好呀。 我目前正在设计一个音板,我正在使用 soundPool 来播放声音。 它工作得很好,但我的声音被播放为铃声。 但我想让它们作为媒体播放? 我该怎么做,我没有找到任何关于这个主题的东西。 但也许我太笨拙了。
我希望你能帮助我,伯恩德
尝试使用 AudioAttributes.USAGE_GAME。
protected void createSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
createNewSoundPool();
} else {
createOldSoundPool();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void createNewSoundPool() {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(attributes)
.setMaxStreams(MAX_STREAMS)
.build();
}
protected void createOldSoundPool() {
soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, SRC_QUALITY);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.