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();
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 have to 2 songs.I used radio buttons for both songs so when select first it should play me the first song and when i click on second. Second song should be played.I have used Play,pause and stop button so when i select first song and click play first song should be played.how to use media player for 2 songs.Previously i used 2 media players for different songs. How to use one media player.
Previously I have used this statements for two songs
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
mediaPlayer1 = MediaPlayer.create(getApplicationContext(),R.drawable.rocky);
My question is i want to use only one media player for both songs
You better stop being lazy and search for the solution on your own aswell. #1Up pretty much answered your question. For your second question: This is Uri
Keep using two mediaplayers, in cases like this there is no problem doing that. Using only one reference would mean you'd have to recreate it each time you want to change clip, or stopping it and calling setDataSource(context, URI).
If you use only one reference to mediaplayer the user will have to wait for the clip to be ready each time it has to be played, while in your implementation both sound clips are ready to be played at any time.
Anyways, here is a setDataSource example:
MediaPlayer mp = MediaPlayer.create(context, firstSongUriOrRes);
public void play(int clip)
{
if(mp.isPlaying()) //Stop the mediaplayer if it's already playing
mp.stop();
switch(clip) //Choose the clip to be played
{
case 0:
mp.setDataSource(context, firstSongUriOrRes);
break;
case 1:
mp.setDataSource(context, secondSongUriOrRes);
break;
}
mp.prepare();
mp.start(); //Start the mediaplayer
}
Another way to use setDataSource is to place the audio files inside the asset directory and use this code:
AssetFileDescriptor fd = context.getAssets().openFd("pathInsideAssets/fileName");
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
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
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;
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