0

I'm trying to play an mp3 file which is stored in my res/raw folder properly. But it seems quite difficult to reach this aim. I have a button inside a destination defined in an navigation schema. Its name is : English. Inside its code we can find this listener attached to the only button in its view....

//Button related to play btn
    view.findViewById(R.id.btn_audio).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            AssetFileDescriptor afd = getActivity().getResources().openRawResourceFd(R.raw.a01);
            try {
                player.setDataSource(afd.getFileDescriptor());
                player.prepare();
                player.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

When the button is pressed, the error is:

E/MediaPlayer: error (1, -2147483648)
W/System.err: java.io.IOException: Prepare failed.: status=0x1
    at android.media.MediaPlayer._prepare(Native Method)
    at android.media.MediaPlayer.prepare(MediaPlayer.java:1163)
    at com.example.myapplication.Ingles$3.onClick(Ingles.java:74)
W/System.err:     at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

And I have no idea why and what to do now to solve this...Any clue?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
heptagono
  • 69
  • 7

1 Answers1

0

Try Closing the File Descriptor

MediaPlayer.Prepare() will throw an IOException when using an AFD only if the AFD provided cannot be read (see MediaPlayer.setDataSource()). The developer is totally responsible for closing the file descriptor created, and not closing it can cause conflicts upon compilation. You can do that directly after MediaPlayer.setDataSource() is called.

Sets the data source (AssetFileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns. (see AssetFileDescriptor.close())

    view.findViewById(R.id.btn_audio).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            AssetFileDescriptor afd = getActivity().getResources().openRawResourceFd(R.raw.a01);
            try {
                player.setDataSource(afd.getFileDescriptor());
                afd.close();
                player.prepare();
                player.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

If the problem persists, this might mean that your file is corrupt or not the correct format. The more probable answer is that you haven't correctly retrieved your RAW file.

EMcCrary
  • 39
  • 7