简体   繁体   中英

Android MediaPlayer will not play sound on newer APIs

I've been using this code and it plays sound just fine on Froyo and Gingerbread (and I assume Honeycomb as well as my friends have used it):

MediaPlayer mp = MediaPlayer.create(this, R.raw.click);

Button clicker = (Button) findViewById(R.id.clicker);

clicker.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v){

            mp.start();

        }
    });

The audio I'm using is in WAV format. I've checked it to make sure it's not corrupted and it's fine. This code and sound file still run correctly on Gingerbread.

On Ice Cream Sandwich and JellyBean devices (a galaxy nexus and a nexus 7 respectively) this code does not work. No sound is played. There's nothing put in logcat. I've searched through the Internet and asked my friends for ideas and I can't come up with anything.

Thank you in advance for your time!

Wrap the call in an IllegalStateException, run it thru the debugger and see what you are getting. Also set a boolean isPlaying=mp.isPlaying(); and check its value. Also try a mp.reset() before starting and see it it works.

Also see http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29

Go ahead and inplement MediaPlayer.OnErrorListener and register the method with the media player. See what error you get.

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