相同的按钮输出不同的声音

时间:2016-12-09 02:19:20

标签: java android

这是我目前的代码。

private String name;
private String desc;
private int imageResourceId;
private int voiceId;

public Earthbound(String name, String desc, int imageResourceId, int voice){
    this.name = name;
    this.desc = desc;
    this.imageResourceId = imageResourceId;
    this.voiceId = voiceId;
}
public static final Earthbound[] chars = {
        new Earthbound("Ness", "Ness is the silent main protagonist of EarthBound (Mother 2 in Japan), " +
                "and is analogous to Ninten and Lucas in their respective games. He greatly enjoys baseball; " +
                "not only are most of his weapons various types of baseball bats, " +
                "but he can also equip several baseball caps. ", R.drawable.ness, R.raw.snd_se_narration_characall_Ness),
        new Earthbound("Lucas", "Lucas is the central character of Mother 3, out of seven main characters total. " +
                "(Lucas, Kumatora, Duster, Boney, Claus, Flint, and Salsa). He is from Tazmily Village. " +
                "He is the gentle twin of Claus. ", R.drawable.lucas, R.raw.snd_se_narration_characall_Lucas),
};

public String getName() {
    return name;
}

public String getDesc() {
    return desc;
}

public int getImageResourceId() {
    return imageResourceId;
}

@Override
public String toString() {
    return this.name;
}

public int getVoiceId() {
    return voiceId;
}

我基本上有两个活动,第一个是用户可以选择列表视图选项" Earthbound&#34 ;,一个列出字符的Java类,以及一个显示字符bio的活动。第二个活动有一个按钮,但是这个按钮会根据用户选择的角色改变声音。这就是我被困住的地方。

如果有帮助,这是Java类:

<script>var globalVariableReady = true;</script>

2 个答案:

答案 0 :(得分:0)

您是否只能使用if或switch语句来确定播放哪种声音?

if ((character.getName().compareTo("Ness")) == 0)
            mp.start();
else
            mmp.start();

这样的事情应该有效。

答案 1 :(得分:0)

如果您只是需要选择要播放的声音,您可以为您设置声音资源Earthbound,其他人使用您的代码,无需知道要选择的资源。

private String name;
private String desc;
private int imageResourceId;
private int voiceId;

public Earthbound(String name, String desc, int imageResourceId, int voice){
    this.name = name;
    this.desc = desc;
    this.imageResourceId = imageResourceId;
    this.voiceId = voiceId;
}
public static final Earthbound[] chars = {
        new Earthbound("Ness", "Ness is the silent main protagonist of EarthBound (Mother 2 in Japan), " +
                "and is analogous to Ninten and Lucas in their respective games. He greatly enjoys baseball; " +
                "not only are most of his weapons various types of baseball bats, " +
                "but he can also equip several baseball caps. ", R.drawable.ness, R.raw.snd_se_narration_characall_Ness),
        new Earthbound("Lucas", "Lucas is the central character of Mother 3, out of seven main characters total. " +
                "(Lucas, Kumatora, Duster, Boney, Claus, Flint, and Salsa). He is from Tazmily Village. " +
                "He is the gentle twin of Claus. ", R.drawable.lucas, R.raw.snd_se_narration_characall_Lucas),
};

public String getName() {
    return name;
}

public String getDesc() {
    return desc;
}

public int getImageResourceId() {
    return imageResourceId;
}

@Override
public String toString() {
    return this.name;
}

public int getVoiceId() {
    return voiceId;
}

public int getMediaResourceId() {
    return TextUtils.equals(name, "Ness") ? R.raw.snd_se_narration_characall_Ness : R.raw.snd_se_narration_characall_Lucas;
}

然后

final MediaPlayer mp = MediaPlayer.create(this, character.getMediaResourceId());
voice_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            mp.start();
        }
    });