当我单击Array
. 我想要做的是当你点击不同的图片时播放不同的声音。
到目前为止,这是我的 Java 代码:
public class gorendis extends Activity implements MediaPlayer.OnCompletionListener{
private ImageView play;
private ImageView a2;
private MediaPlayer mp;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.huruf);
play=(ImageView)findViewById(R.id.a1);
a2=(ImageView)findViewById(R.id.a2);
//gambar=(ImageButton)findViewById(R.id.gambar);
//gambar.setImageResource(R.drawable.a1);
play.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
a2.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
setup();
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void stop() {
// TODO Auto-generated method stub
}
private void play() {
mp.start();
play.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.doajodoh);
mp.setOnCompletionListener(this);
} catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
loadClip();
play.setEnabled(true);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
}
主要布局 XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView>
<LinearLayout>
<ImageView
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="53px"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/a1" >
</ImageView>
<ImageView
android:id="@+id/a2"
android:layout_width="wrap_content"
android:layout_height="53px"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/a2" >
</ImageView>
</LinearLayout>
</ScrollView>