i have an image with some text that i have the audio for.
I want to know how i can set it up so the user can the the button and the audio will start to play.
Thanks!
I have a solution I think it might work.
Fisrt of all, instead of working it an ImageView would be easier to work with ImageButton.
So, in your layour XML file, select ImageButton and create one using your image with the text.
also you might want to set a null bacground for your ImageButton, this is the code
android:background="#null"
Next, in your java class select the sound and the ImageButton and set it to play your sound when clicked.
would be like this
final MediaPlayer yourSoundName = MediaPlayer.create(Lotr1.this,R.raw.yoursoundfile);
final ImageButton yourImageButtonName =(ImageButton) findViewById(R.id.yourImage);
yourImageButtonName .setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
yourSoundName.start();
}
}
});
Any quenstion I would be glad to help
Related
I need some help,
I have one ImageButton that plays and stops a tune, I want the button to change to a stop symbol when playing and then back to a play symbol when stopped. So far I have the symbol and tune playing when the ImageButton is clicked the first time, but when it is clicked the second time, the tune stops but the image does not change, any advice?
mPlayTune.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tuneMp.isPlaying()) {
tuneMp.stop();
tuneMp.prepareAsync();
mPlayTune.setImageResource(R.drawable.ic_av_play_arrow);
}else
tuneMp.start();
mPlayTune.setImageResource(R.drawable.ic_av_stop);
}
});
Couldn't you just make 2 objects and hide or show them by clicking them? It's not a clean implementation but for sure a workaround.
I solved it, it was simply changing
tuneMp.start();
mPlayTune.setImageResource(R.drawable.ic_av_stop);
to
mPlayTune.setImageResource(R.drawable.ic_av_stop);
tuneMp.start();
this way it sets the icon before hitting tuneMp.start();
I'm a bit new to Android, therefore question may sound ridiculous, but:
Is there any way to make a button in form of smthg? There ate tons of apps, where you press on a tree or some house and some action starts. How is that made?
Not action, but form. Or there is no way to make button NOT rectangular?
You are talking about imagebutton.Image button documentation
For example creating a button with tree background.Example:
<ImageButton android:src="#drawable/tree.png"></ImageButton>
Yes you can do that. Considering your trees and house as an image.
ImageView im;
im = (ImageView) findViewById(R.id.image_of_tree);
im.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//perform some action
}
});
Off-course When you get inflated your layout and your view(can be any view) is available by id. your button can be an ImageView, ImageButton etc. just find the id for your view and set whatever event you want to listen by this view.
I need to do a two image button with onClick.That two image button have to be located in same place.one image button is to start functionality for voice record and other image button is stop functionality for voice record.
I done a functionality exactly.My only problem is to use two image button for stop and start in same place.
I searched many tutorials and SO posts.But I didn't get it.
Anyone can help me with this.
Just use one button and the method setBackgroundResource of the ImageButton. use one and the same button and in the onClick method use this line:
yourButton.setBackgroundResource(R.drawable.startImg); //for start of the recording
and
yourButton.setBackgroundResource(R.drawable.stopImg); //for stop of the recording
if (myAudioRecorder==null) {
myAudioRecorder.start();
audioButton.setImageResource(R.drawable.stop);
} else if (myAudioRecorder != null) {
audioButton.setImageResource(R.drawable.record);
}
Doese anyone know of a tutorial on how to make a Volume Controller like this one (with a Save button at the bottom). This is my problem, how to make that "Save" button. I can create all the volume SeekBars, but unfortunately when I make changes on SseekBar it automatically changes the volume from the phone settings (ringer, notification, media, in call).
Thanks in advance.
First off, use the SeekBar and retrieve the value it sets to. Don't set the volumes while the SeekBar is adjusted. And then, when the button Save is pushed, send those values to the AudioManager.
Use this link for more detailed explanation: Android Development: Change Media volume? as well as this one: SeekBar1.java
Here's a quick example of how it might look like:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
ImageButton save = (ImageButton) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, valueFromSeekBar, flagIfNeeded);
}
}); //End setOnClickListener()
I hava a set of buttons. When I click on a button it should produce sound.
Example:
Button b=new Button(this);
b.setText("Press");
b.setOnClickListener(new OnClickListener)[
public void click(View v)
{
b.setSoundEffectsEnabled(true);
});}
This doesn't work though, can anyone help me please.
What do you mean on default sound? If you want to play your own sound, you must create a MediaPlayer like this.
MediaPlayer mediaPlayer = MediaPlayer.create(this, [here is your sound in the raw file]);
and in the click method you need to implement this:
mediaPlayer.start();
or you can use soundpool too.
Hope it helps.
You can also use the build in sound notifications
ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, ToneGenerator.MAX_VOLUME);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
If you want to play the default click sound when you click on the button, then setting b.setSoundEffectsEnabled(true) should work (although it doesn't need to be on the listener), but it is dependent on the device option to play audible selection. Try checking the device's sound settings if it is on.