简体   繁体   中英

Problem playing audio on android

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());
     }
        }

}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM