views:

441

answers:

1

Amendment: This appears to be a problem with the particular audio file I was using. Other applications on the droid such as the Astro file manager also fail to play it, and if I replace it in the package with an AAC file, it plays it without error. I encoded the problematic audio file to MP3 format from a WAV file, using LAME on ubuntu. It would be good to know the limitations of the android media player. mplayer seems to have no problem playing the file on ubuntu. I suppose I should submit a bug report.

Original question: The following code crashes when I try to play it on my droid. The error message given in the log is "Command PLAYER_INIT completed with an error or info PVMFErrNoResources." Then an IOException is raised on the mp.prepare() line. There is a file res/raw/bell.mp3 in my project directory, which I assume corresponds to R.raw.bell in the code below. I am building with "ant debug". In case it's relevant, when I created the project directory with "android create", I set the target number to 4, corresponding in my system to "android 2.0."

What am I doing wrong, here?

    import java.io.IOException;
    import android.app.Activity;
    import android.os.Bundle;
    import android.media.MediaPlayer; 
    import android.util.Log;

    public class testapp extends Activity 
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
     try {
         MediaPlayer mp = MediaPlayer.create(this, R.raw.bell);
         mp.prepare();
         mp.start();
     } catch (IOException e) {
         Log.v(getString(R.string.app_name), e.getMessage());
     }
        }

}
A: 

You do not need to call prepare() if you use the static create() method to get the MediaPlayer. It does the prepare() step for you. You only need to call prepare() if you either use the regular MediaPlayer constructor or if you are trying to reset the clip back to the beginning to play back from the existing MediaPlayer object.

Here is a sample project for playing back sounds (in my case, an Ogg clip).

CommonsWare
Thanks for pointing that out. However, removing the prepare() call didn't remove the error. I was mistaken about the line giving the error. It was actually the create call. I think I have determined the key issue: the audio file is unplayable on the droid for some reason. See my amendment to the question.
fivebells
"It would be good to know the limitations of the android media player." I would first confirm your MP3 fits within the specifications defined at http://developer.android.com/intl/de/guide/appendix/media-formats.html
CommonsWare
Also, the comment from Chris Thompson is relevant. If the file plays successfully on the emulator, the problem is something peculiar to the DROID, and your best option for assistance will be the MOTODEV support boards. If the file fails on the emulator, and it meets the specifications listed above, then there may be a bug in OpenCORE itself, in which case http://b.android.com would be the place to file it (if and only if you can legally supply a file that exhibits the behavior).
CommonsWare
Thanks for the advice. I will test on the emulator before posting about the bug.
fivebells