0

I'm a noob trying to work something out and learn from it.

I have two imagebuttons and when i click them I get a kind of "schick" sound rather than the sound files that i have in the /res/raw/ directory.

This is my code:

public void button_clicked1(View v) 
{
    text1.setText("1"+width);  

     mp = MediaPlayer.create(GameScreen.this, R.raw.a);   
     mp.start();  
    }


public void button_clicked2(View v) 
{
    text1.setText("2"+height);    
     mp = MediaPlayer.create(GameScreen.this, R.raw.b);   
 mp.start();
    }

What am I doing wrong?

Thanks!

Ok, changed the above code to this:

 public void button_clicked1(View v) 
    {
        text1.setText("1"+width);  

         mp = MediaPlayer.create(GameScreen.this, R.raw.piano_a);   
             try {
                    mp .prepare();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
         mp.start();



        }


    public void button_clicked2(View v) 
    {
        text1.setText("2"+height);    
        mp = MediaPlayer.create(GameScreen.this, R.raw.piano_b);   

            try {
                mp .prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     mp.start();
        }

And it still does not work

Ryan
  • 9,821
  • 22
  • 66
  • 101

1 Answers1

0

EDIT: Try this:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

in your main application code. This will tell the AudioManager that when your application has focus, the volume keys should adjust music volume (found that here).

After that, make sure that your volume is up - it may just be playing the sounds with no volume.

Community
  • 1
  • 1
mopsled
  • 8,445
  • 1
  • 38
  • 40
  • I put another suggestion in my answer. – mopsled Aug 14 '11 at 21:52
  • Ok, I think i found the problem, the files are WAV files... and am not sure if thats supported – Ryan Aug 14 '11 at 21:53
  • Nope, not supported: http://developer.android.com/guide/appendix/media-formats.html – Ryan Aug 14 '11 at 21:55
  • [.WAVs are supported](http://developer.android.com/guide/appendix/media-formats.html). Check out the last column of the last supported audio format. There may be another problem. Let me know if your code works with a sound file that isn't a WAV. – mopsled Aug 14 '11 at 21:56
  • Ok, got it working!!! In the image button went through each property and found something that says "Sound effects enabled", then turned that to false... and the damn thing worked! – Ryan Aug 14 '11 at 23:58
  • Glad to hear it! You should put that in your original post (on top) or in a separate answer, and I'll delete mine. – mopsled Aug 15 '11 at 02:56