-1

I am new to Android programming. Haven't even written a single app, so answers that are extremely detailed are appreciated. So, here it goes: I want to assign some .mp3 sound files to an area of the screen.

For eg, if I click on the picture of a cat, meow.mp3 should play. This is applicable to two different activities. Also, I am looking for a way to mute sounds from both activities from a single source, ie, the MainActivity. I tried figuring out some codes, but they weren't detailed enough for my understanding.

Any help is appreciated, thanks!

Well, this is what I did to display an image(playButtonUp) and open up another View. I need to add a .mp3 file so that the action is signified by a sound, like in the case of clicking of a button.

case MotionEvent.ACTION_DOWN: 
      if ((X > (screenW-playButtonUp.getWidth())/2 &&          
      X < ((screenW-playButtonUp.getWidth())/2) +    
      playButtonUp.getWidth()) &&    
      Y > (int)(screenH*0.7) &&          
      Y < (int)(screenH*0.7) +           
      playButtonUp.getHeight()) {            
  playButtonPressed = true;  

break;
   }
nyrrovro
  • 1
  • 2
  • post ur code that u have tried? – Sam Jun 04 '13 at 04:48
  • Well, I can't. It says that I need more reputation to be able to do so. – nyrrovro Jun 04 '13 at 05:05
  • just copy and past here.. – Sam Jun 04 '13 at 05:10
  • hey man ...edit ur code and post there? – Sam Jun 04 '13 at 05:14
  • http://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java – Stephen Carlson Jun 04 '13 at 05:32
  • 1
    If you are so new to Android I would suggest you build your application part by part. Start building the application with only clickable images, then only implement playing mp3. From your currently posted codes. I would assume you have the images using ImageView, ImageView provides setOnClickListener() method, which you can easily found out which image is selected. – Chor Wai Chun Jun 04 '13 at 05:46
  • I got that part, I am just wondering how to attach .mp3 file to that image so that the sound plays when the clicking takes place. – nyrrovro Jun 04 '13 at 06:27

1 Answers1

0

Lets assume that you have ImageView in your layout. You can get it from source code by typing:

// imageView1 is id referring to your ImageViews xml code
ImageView myImageView = (ImageView) findViewById(R.id.imageView1); 

Then you can add listener to it which fires when you click the image:

myImageView.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v) {   
        // Add the MediaPlayer code here
        //  ...
    }
});

Now you can add some functionality in that event. First i would add a global variable so you can start and stop the media when ever you want:

private MediaPlayer mediaPlayer;

Lets add some code to the onClick event:

// First check if mediaPlayer has been created
if( mediaPlayer != null ) {
    // Then check if the mediaPlayer is playing, if it is, 
    // we want it to stop because if we don't stop it, 
    // there will be multiple sounds playing at the same time
    if( mediaPlayer.isPlaying() ) 
        mediaPlayer.stop();
}

// This creates the player and sets my slap.mp3 to be played
// => Add your mp3's in YourProject/res/raw/
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.slap);
mediaPlayer.start();

Now you have a picture that creates a sound!

You wanted to mute sounds, I assume that you wanted to stop them playing? Use mediaPlayer.stop(); to stop playback but make sure that you have checks for mediaPlayer not null and mediaPlayer.isPlaying() before calling it, otherwise you would get an exception. I hope this will help you somehow!

vilpe89
  • 4,656
  • 1
  • 29
  • 36