How do I attach a sound file to a button on click - android

How do I attach an MP3 sound file to a button on click method. I already have an invisibility on the same button how can I change the invisibility to a scale to zero along with sound file
mainButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);

I think you can use MediaPlayer for that
First place your mp3 file in the raw folder of your project, then on your activity create a method like this:
fun playSong(){
val mediaPlayer = MediaPlayer.create(this, R.raw.your_mp3_file)
mediaPlayer.start()
}
call this method inside your click Listener, and then mediaPlayer.stop() to stop playing.
I would suggest checking the mediaplayer docs to see all available methods/properties of it, I think it's the simplest way of playing audio on Android.
Also check the resources guide on android docs to make sure you have what you need to import your mp3 file in your project.
Good luck!

Related

Playing mp3 voice for android application on clicking button

hi i am developing android application in which i need to play a saved mp3 voice when someone click on button. on clicking button another activity will be opened and saved voice should be played
how can i do it??
Regards
First, you need a service to play MP3 via android.media.MediaPlayer
Second, you can put your saved MP3 in res/raw folder. (manual create it, if it doesn't exist)
And this could help : Get URI of .mp3 file stored in res/raw folder in android
......
cheers~
Here is a sample of code:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int res = getResources().getIdentifier ( "yourfile", "raw","your.package.here" );
mPlayer = MediaPlayer.create ( YourActivity.this, res );
mPlayer.start();
}
});
You should create a directory res/raw with the file yourfile.mp3 in it

Android: playing many short media files

I am wring a program to play with about 1000 short media files which are stored in "raw". My programe is simple. It plays a new media file each time a button is clicked. The name of media file is read from an array list and I am using MediaPlayer
Here is my code when a button is clicked (only one button):
#Override
public void onClick(View arg0) {
i++;
String fileName=soundArray.get(i);
int soundID=getResources().getIdentifier(fileName, "raw", getPackageName());
if(soundID>0){
MediaPlayer mySound=MediaPlayer.create(getApplicationContext(),soundID);
mySound.start();
}
}
This code works fine with first two or three button clickings. After that, it is crashed. Do you think there is a problem with my code and should I use SoundPool in my case?
use prepare() before starting the mediaplayer
mySound.prepare();

Playing audio files continously from a listview, without user click on the listitem

I have a list of audio files in a directory in sdcard. I have derived the files in a listview, now I want to play the files continuously, without user click on the listitem. Any suggestion please
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
if (currentIx < playListLength - 1) {
currentIx++;
mp.reset();
// Change Current Song here using the set data source method and whatever else must change
mp.start();
}
}
});
when ever a audio is playing callbacks are automatically called.
if the file completely played OnCompletionListener call back is called.you can again start the next file in that method.
To play songs continuously Have a look on the following URL
http://coderzheaven.com/index.php/2011/02/play-all-songs-from-your-raw-directory-in-android-continuously/
Thanks
Deepak

play a simple .wav on a view page?

In android, is there a way I can conditionally play a small .wav file when a layout is displayed to the user (on load), like so:
if (randonGen == 3) {
//play small wav sound here
mTheMessage = (TextView) findViewById(R.id.resultText);
mTheImageButton = (ImageButton) findViewById(R.id.face_image);
mTheImageButton.setBackgroundDrawable(bitDraw);
mThePicture.setImageBitmap(cameraBitmap);
}
Where the code to call my sound.wav from my assets folder is called and played?
Actually - I figured out that a simple Media Play will do the trick with small wavs:
MediaPlayer mp = MediaPlayer.create(this, R.raw.your_wav);
mp.start();
android.media.SoundPool looks like it will do what you want.

how can i play small music file when i tuch an image in android?

I am New to android, My task is to playing a small audio file when we touch an image?
Depending on your target API version there are a different ways of doing this.
Assuming you have something like this inside your layout xml file.
<ImageView
android:id="#+id/ImageThatWillPlaySound"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androidL:onClick="playSound">
</ImageView>
Note androidL:onClick="playSound" which will run the function in your activity that matches the definition:
public void playSound(View v) {
}
Inside that function, you would use MediaPlayer as xandy suggested.
Something like this
public void playSound(View v) {
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file);
mp.start();
}
Where inside your assets folder you had a file called sound_file.
Use MediaPlayer to play.

Categories

Resources