ImageButton的音板Android应用程序 [英] ImageButton Soundboard android app
问题描述
我刚刚开始我的第一个音板。基本上,这是我迄今为止(除非我有40个声音)。有谁知道一个更好的方式来做到这一点?我得去赴约,但我会回来的今天晚些时候做出回应。谢谢你,谁可以提供帮助。
I'm just starting out with my first soundboard. Basically this is what I have so far (except I have 40 sounds). Does anyone know a better way to do this? I have to go to an appointment, but I will be back later today to respond. Thank you, anyone who can help.
-------------------------音板--------------
-------------------------soundboard--------------
包com.soundboard.app;
package com.soundboard.app;
进口android.app.Activity; 进口android.content.Intent; 进口android.media.MediaPlayer;
import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer;
进口android.os.Bundle; 进口android.view.View; 进口android.widget.Button; 进口android.widget.ImageButton;
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton;
公共类主要的扩展活动{
public class main extends Activity {
MediaPlayer sound1, sound2, sound3;
ImageButton button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sound1 = MediaPlayer.create(this, R.raw.sound1);
button1 = (ImageButton) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound1.start();
}
});
squeak3 = MediaPlayer.create(this, R.raw.squeak3);
dogsqueak = (ImageButton) findViewById(R.id.dogsqueak);
dogsqueak.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
squeak3.start();
}
});
sound2 = MediaPlayer.create(this, R.raw.sound2);
button2 = (ImageButton) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound2.start();
}
});
sound3 = MediaPlayer.create(this, R.raw.sound3);
button3= (ImageButton) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound3.start();
}
});
}
}
推荐答案
以40个按钮,你需要安排这种情况发生在一个循环。
With forty buttons you need to arrange for this to happen in a loop.
虽然还有更聪明的方法可以做到这一点,你可以通过构建启动地图
:
Although there are even more clever ways to do this, you can start by building a Map
:
Map<Integer, Integer> map = new HashMap<Integer, Integer>>();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
...
和然后循环:
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
final MediaPlayer sound = MediaPlayer.create(entry.getValue());
Button button = (ImageButton) findViewById(entry.getKey());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sound.start();
}
});
}
这会给你的循环液的味道。您还需要考虑如何管理你的的MediaPlayer
实例。
This will give you a taste of a looping solution. You also need to consider how you manage your MediaPlayer
instances.
这篇关于ImageButton的音板Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!