Reputation: 68
I tried searching for this in many places, but could not find an answer. I am making an app with a set of image buttons that have the alphabet on them. I want to be able to click the letter A, and hear sound file A. If I click letter B, I want to hear sound file B. So far I only have letters A and B, and sound files A and B. When I run my code, I can only hear the sound for button A, and when I press letter B, Nothing happens. This is my code:
package com.android.nishad.learn.hindi;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class AlphabetActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alphabet);
ImageButton btn = (ImageButton)findViewById(R.id.imageButton1);
btn.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(this, R.raw.lettera);
mp.start();
while (mp.isPlaying()) {
// do nothing
};
mp.release();
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alphabet);
ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2);
btn2.setOnClickListener(this);
}
public void onClick1(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(this, R.raw.letterb);
mp.start();
while (mp.isPlaying()) {
// do nothing
};
mp.release();
}
};
Upvotes: 0
Views: 1931
Reputation: 40193
First, remove those onCreate1()
and onClick1()
methods. onClick()
method is enough to receive click events for all the buttons in your layout. You can do this using the view.getId()
method. And for playing sounds I'd recommend using the SoundPool class, which has a very clear interface for such purposes. Hope this helps.
Upvotes: 0
Reputation: 3609
package com.android.nishad.learn.hindi;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class AlphabetActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alphabet);
ImageButton btn = (ImageButton)findViewById(R.id.imageButton1);
btn.setOnClickListener(this);
ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2);
btn2.setOnClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.imageButton1:
mp = MediaPlayer.create(this, R.raw.lettera);
break;
case R.id.imageButton2:
mp = MediaPlayer.create(this, R.raw.letterb);
break;
default:
return;
}
mp.start();
while (mp.isPlaying()) {
// do nothing
}
mp.release();
}
}
Upvotes: 0
Reputation: 1077
Try this:
public void onClick(View v) {
switch(v.getId()){
case R.id.R.id.imageButton1:
mp = MediaPlayer.create(this, R.raw.lettera);
break;
case R.id.R.id.imageButton2:
mp = MediaPlayer.create(this, R.raw.letterb);
break;
}
mp.start();
while (mp.isPlaying()) {
// do nothing
};
mp.release();
}
Upvotes: 0
Reputation: 57306
Methods onCreate1
and onClick1
are useless as they are never invoked. Instead you should put all the initialisation code in your onCreate
method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alphabet);
findViewById(R.id.imageButton1).setOnClickListener(this);
findViewById(R.id.imageButton2).setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
int rawid = 0;
switch(v.getId()) {
case R.id.imageButton1: rawid = R.raw.lettera; break;
case R.id.imageButton2: rawid = R.raw.letterb; break;
defalt: return; //do nothing if none of our buttons;
}
mp = MediaPlayer.create(this, rawid);
mp.start();
while (mp.isPlaying()) {
// do nothing
};
mp.release();
}
Note however that this is a very inefficient way of handling it. Instead you should have one MediaPlayer instance and then use it to play various sounds as needed. Creating a new MediaPlayer every time a button is pressed is a waste of resources.
Upvotes: 2