play a simple .wav on a view page? - android

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.

Related

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

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!

Prevent MediaPlayer from play all mp3 files in the folder automatically

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;

Audio attachment with image in android

I have image is my Draw able folder.When I call this image I want to play an audio also which is in my raw folder.can I do this how??please help me.
You can achieve by creating image button and put image on it. and onClick of that button you can play the music using MediaPlayer api.
Refer this : http://developer.android.com/guide/topics/media/index.html
Example code for Play audio from Raw folder
: http://www.barebonescoder.com/2010/06/android-development-audio-playback-safely/
you can write this code at onResume:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),
R.raw.name);
mp.start();
while (mp.isPlaying()) { // donothing
}
;
mp.release();

Playing audio file with MediaPlayer

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.

android - application displaying videos

how do i display videos retrieved from resources in my android application ?
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(context, R.raw.vid);
mp.start();
i have placed a vid.3gp file in my res/raw folder..
i am getting NullPointerException.. what changes do i need to make
why so??
do we need something called as Surface Holder or something similar ?
using the MediaPlayer you should be able to play video

Categories

Resources