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
Related
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!
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();
I'm from Taiwan and my English is pretty basic, thanks for your forgiveness.
I built a iPad app and published to App Store, which is a kind of communicating board (AAC for the person who has difficult on speaking). It has several buttons on main view and when user touch up one, the app will play a mp3 sound corresponding to that button. I've put all mp3 files on a folder and determining which one to play dynamically at run-time.
Recently I'm working on build the Android version of this app, but I meet a problem that is when I touch up one button on the view, the MediaPlayer play not only the corresponding one but keep playing all the rest mp3 files on the folder continuously. It doesn't happen on the development of iPad version. Any one can help me to resolve this problem and set the MediaPlayer up to working appropriately?
PS. I put all MP3 files on some sub-folders and put them on the "assets" folder. It because for some reasons, all mp3 files are using numeric file name (ex. 0123223.mp3), and which MP3 to play is according to which button user touched up, so I can't use R.raw.0123223 not only because the R class can't has a member named in numeric, but also the target mp3 file can only determining at run-time instead of compile-time, so I can't use the res/raw folder.
My code is as following:
private void PlayMP3(String mp3File) throws IOException {
AssetFileDescriptor afd = this.getAssets().openFd(mp3File);
MediaPlayer player = new MediaPlayer();
player.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
System.out.println(mp.getAudioSessionId() + " finished.");
}
});
player.setDataSource(afd.getFileDescriptor());
player.setLooping(false);
player.prepare();
player.start();
}
the parameter "mp3file" of PlayMP3 is a concatenated string of mp3 file path, according to the button user touched up.
Many thanks!
Your version would work if you had only that one file in the assets directory. The asset directory contents are put together one after another. So, if you do not specify where to start and how many bytes to read, the player will read up to the end as if they are one file.
You can accomplish what you want by using setDataSource with more arguments:
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
This will allow you to define the limits of the player and only play the song you specify in your string mp3File.
You did not post some code.
First step is to insert your sound file into the raw folder
Then, you need only a few lines of code:
MediaPlayer mediaPlayer = null;
mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.mysoundfile);
mediaPlayer.start();
When the application is closing, dont forget to "release" your media player:
mediaPlayer.release();
mediaPlayer = null;
Hi there I'm an android and java newbie and is wondering if there is a better way of doing the following:
I currently have 250 short audio files (.3gp) that i put inside my res/raw folder and based on a certain condition, I want to play certain audio file. For example if the text of the button that is being clicked is the word "and", I want to play the and.3gp sound file.
The following is the code that I use to accomplish what I want to do:
if(word.equals("No"))
{
MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.no);
mp.start();
mp.setOnCompletionListener(listener );
}
else if(word.equals("And"))
{
MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.and);
mp.start();
mp.setOnCompletionListener(listener );
}
.....
The above code is working fine so far but I'm wondering if I can do this without having a 250 else if condition. Is there a way to pass in the audio file to MedialPlayer.Create by file name via Uri? If yes, how do I do it? Thank you.
You can make a hash function that maps a string key to the uri
Loop through all your files in your raw folder and remove the file extension, then test against that.
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.