开发者

Unable to create MediaPlayer, error for a soundboard

开发者 https://www.devze.com 2023-02-21 13:59 出处:网络
when debugging i get unable to create MediaPlayer The app loads and then hangs when i click on a button for sound.

when debugging i get unable to create MediaPlayer

The app loads and then hangs when i click on a button for sound. Here is the logcat errors i can't seem to find what went wrong.

04-03 16:00:17.273: ERROR/gralloc(61): [unregister] handle 0x4456d0 still locked (state=40000001)
04-03 16:00:22.793: ERROR/PlayerDriver(31): Command PLAYER_PREPARE completed with an error or info PVMFErrResource
04-03 16:00:22.803: ERROR/MediaPlayer(279): error (1, -17)
04-03 16:00:17.273: ERROR/gralloc(61): [unregister] handle 0x4456d0 still locked (state=40000001)

Here is my code.

package com.yes.hotrod;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class main extends Activity {
MediaPlayer FilmSound=new MediaPlayer();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    Button button1 = (Button) findViewById(R.id.button1);
    FilmSound = MediaPlayer.create(getApplicationContext(),
            R.raw.animalkingdom);
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.animalkingdom);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.animalkingdom);
                FilmSound.start();
            }
        }
    });

    Button button2 = (Button) findViewById(R.id.button2);
    FilmSound = MediaPlayer.create(getApplicationContext(),
            R.raw.bigassstunt);
    button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.bigassstunt);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.bigassstunt);
                FilmSound.start();
            }
        }
    });
    Button button3 = (Button) findViewById(R.id.button3);
    FilmSound = MediaPlayer
            .create(getApplicationContext(), R.raw.godsofwar);
    button3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.godsofwar);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.godsofwar);
                FilmSound.start();
            }
        }
    });
    Button button4 = (Button) findViewById(R.id.button4);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.greentea);
    button4.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.greentea);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.greentea);
                FilmSound.start();
            }
        }
    });
    Button button11 = (Button) findViewById(R.id.button11);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.havefun);
    button11.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.havefun);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.havefun);
                FilmSound.start();
            }
        }
    });
    Button button6 = (Button) findViewById(R.id.button6);
    FilmSound = MediaPlayer.create(getApplicationContext(),
            R.raw.kisswashot);
    button6.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.kisswashot);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.kisswashot);
                FilmSound.start();
            }
        }
    });
    Button button7 = (Button) findViewById(R.id.button7);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.ohshit);
    button7.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.ohshit);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.ohshit);
                FilmSound.start();
            }
        }
    });
    Button button8 = (Button) findViewById(R.id.button8);
    FilmSound = MediaPlayer.create(getApplicationContext(),
            R.raw.throwingstar);
    button8.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.throwingstar);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.throwingstar);
                FilmSound.start();
            }
        }
    });
    Button button9 = (Button) findViewById(R.id.button9);
    FilmSound = MediaPlayer.create(getApplicationContext(),
            R.raw.twolegittwoquit);
    button9.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                F开发者_高级运维ilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.twolegittwoquit);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.twolegittwoquit);
                FilmSound.start();
            }
        }
    });
    Button button10 = (Button) findViewById(R.id.button10);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.whiskey);
    button10.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.whiskey);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.whiskey);
                FilmSound.start();
            }
        }
    });
    Button button5 = (Button) findViewById(R.id.button5);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.demons);
    button5.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FilmSound.isPlaying()) {
                FilmSound.stop();
                FilmSound.reset();
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.demons);
                FilmSound.start();
            } else {
                FilmSound = MediaPlayer.create(getApplicationContext(),
                        R.raw.demons);
                FilmSound.start();
            }
        }
    });
}
}

Here is the code with prepare:

Button button1 = (Button) findViewById(R.id.button1);
        FilmSound = MediaPlayer.create(getApplicationContext(),
                R.raw.animalkingdom);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FilmSound.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (FilmSound.isPlaying()) {
                    FilmSound.stop();
                    FilmSound.reset();

                    FilmSound = MediaPlayer.create(getApplicationContext(),
                            R.raw.animalkingdom);
                    FilmSound.start();
                } else {
                    FilmSound = MediaPlayer.create(getApplicationContext(),
                            R.raw.animalkingdom);
                    FilmSound.start();
                }
            }
        });


I just had a similar issue. I solved it by simply adding the following to your manifest file:

 <uses-permission android:name="android.permission.INTERNET" />

Prior to ICS no Internet Permission was required.


After you create the MediaPlayer, try preparing it:

FilmSound.prepare();


Check this question out: Android -- Can't play any videos (mp4/mov/3gp/etc.)?

Have you tried playing a file in a different format? Maybe your sound requires some codec that Android doesn't have, so the prepare() fails.


Remove the create() method outside of the onClickListener. Right now when you start the app it will create 10 instances of FilmSound since the code is outside of onClickListener, then when you click a button it will create the 11th instance of FilmSound. Likely this is running out of memory below Android 2.2. You don't need to prepare after using the create() method, it will prepare after create the first time.

Also, I'm pretty sure isPlaying() can throw exceptions.

if (FilmSound != null) {
try {
        if (FilmSound.isPlaying()) { 
             FilmSound.release(); 
             FilmSound = null;
        }
    } catch(Exception e) {FilmSound= new MediaPlayer(); }};

if (FilmSound == null) {
    FilmSound= new MediaPlayer(); 
}
    FilmSound = MediaPlayer.create(this, clip);
    FilmSound.start();
    FilmSound.setOnCompletionListener(this);
0

精彩评论

暂无评论...
验证码 换一张
取 消