2016-12-09 22 views
0

これは私が現時点で持っているコードです。同じボタンから別の音が出力されます

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_earthbound); 

    int charNo = (Integer)getIntent().getExtras().get(EXTRA_CHARNO); 
    Earthbound character = Earthbound.chars[charNo]; 

    ImageView photo = (ImageView) findViewById(R.id.photo); 
    photo.setImageResource(character.getImageResourceId()); 
    photo.setContentDescription(character.getName()); 

    TextView name = (TextView) findViewById(R.id.name); 
    name.setText(character.getName()); 

    TextView desc = (TextView) findViewById(R.id.desc); 
    desc.setText(character.getDesc()); 

    Button voice_btn = (Button)this.findViewById(R.id.voice_btn); 
    voice_btn.setContentDescription(character.getName()); 
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.snd_se_narration_characall_Ness); 
    final MediaPlayer mmp = MediaPlayer.create(this, R.raw.snd_se_narration_characall_Lucas); 
    voice_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v){ 
      mp.start(); 
      mmp.start(); 
     } 
    }); 

私は基本的に2つのアクティビティ、ユーザーがリスト表示オプション「地縛」、文字を一覧表示し、Javaクラス、および文字の生物を表示する活動を選択することができるということで最初のものを持っています。 2番目のアクティビティには1つのボタン、がありますが、このボタンはユーザーが選択した文字に応じて音色を変更します。これは私が立ち往生している場所です。

これは、Javaクラスを助けている場合:

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; 
} 
+1

なぜ "JavaScriptの" タグ?あなたの質問にJSの他の言及はないので、私はタグを削除するつもりです。 – nnnnnn

答えて

0

は、あなただけのサウンドの再生を決定する場合やスイッチステートメントを使用できませんか?

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

このような機能があります。

+1

'compareTo == 0'?なぜ、 '.equals()'ではないのですか? –

+0

私はクラスの割り当てに '.equals()'を使いました。私の教授はStringに '.compareTo()'を使う方が良いと言っていました。 2人の異なる教授が実際に私に同じ日に同じことを話しました。 – sbowde4

+1

私は彼らが '=='で "equals"を試していると思ったと思います。 'String#compareTo'のソースコードは次のように言います。*文字列が等しい場合は結果は0です。 'compareTo'は' equals(java.lang.Object) 'メソッドが真を返すときに0を返します。* –

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(); 
     } 
    }); 
+0

2文字以上を扱うのはどうですか? –

+0

@DericWu 'type'を追加し、' getMediaResourceId() 'に' switch case'を追加することができます。 – Yat3s

関連する問題