0

I have onClickListener and I need to add sound to it.

When I press button the sound must start. It's on sd-card.

I try to use MediaPlayer but it's need to put resourse id into constructor.

MediaPlayer.create(cont, resid);

I need absolute path not id from resourse.

How to do this?

Try to do like this:

File mp3File = new File(Environment.getExternalStorageDirectory(), "test.mp3");
Uri mp3Uri = Uri.fromFile(mp3File);
Button yourButton = (Button) findViewById(R.id.button);
final MediaPlayer mp = MediaPlayer.create(this, mp3Uri);

yourButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        mp.start();
    }
});

But get an err:

12-07 20:50:24.198: E/AndroidRuntime(6124): FATAL EXCEPTION: main
12-07 20:50:24.198: E/AndroidRuntime(6124): java.lang.NullPointerException
12-07 20:50:24.198: E/AndroidRuntime(6124):     at izuchaika.act.click_myOnClickListener.onClick(click_myOnClickListener.java:30)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at android.view.View.performClick(View.java:2532)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at android.view.View$PerformClick.run(View.java:9277)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at android.os.Handler.handleCallback(Handler.java:587)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at android.os.Looper.loop(Looper.java:143)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at android.app.ActivityThread.main(ActivityThread.java:4196)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at java.lang.reflect.Method.invoke(Method.java:507)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-07 20:50:24.198: E/AndroidRuntime(6124):     at dalvik.system.NativeStart.main(Native Method)
Val
  • 4,225
  • 8
  • 36
  • 55
  • 1
    You asked if this is possible. I answered yes. That is the answer to your question actually. If you want to know how this can be realised, please ask the appropriate question *(and show what you already found out or tried)*. I just wanted to show you that your question quality is somewhat low. Please edit it and fill in some details. Thanks. –  Dec 07 '11 at 17:34
  • possible duplicate of [Playing Audio files from SD card](http://stackoverflow.com/questions/7879053/playing-audio-files-from-sd-card) – Andrejs Cainikovs Dec 07 '11 at 17:59

1 Answers1

3

You should use MediaPlayer.create(context, uri) instead of the variant with the resource id. This way you can initialise the media player with a simple file uri, e.g. like this:

File mp3File = new File(Environment.getExternalStorageDirectory(), "test.mp3");
Uri mp3Uri = Uri.fromFile(mp3File);
Button yourButton = (Button) findViewById(R.id.button);
final MediaPlayer mp = MediaPlayer.create(this, mp3Uri);

yourButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        mp.start();
    }
});

In this case the file is a mp3, named test.mp3 and placed in the root folder of the sdcard or external storage. You may want to adjust that to fit your needs.

  • Please note that I'm not too familiar with the media framework *(didn't need it for my own work yet)*.This is a small, working sample that I came up with in a few minutes. I was just sure that it's possible to play a sound on a button click. Take it as a way to say thanks for editing the question. :) –  Dec 07 '11 at 18:12
  • Seems like mp.start() fails with a NullPointerException. This means that `MediaPlayer.create()` failed somehow because it returned `null`. I'd say make sure that you changed the file location and name accordingly *(your file will be most likely have a different name than test.mp3)*. –  Dec 07 '11 at 19:02