1

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();
                FilmSound.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();
                }
            }
        });
MByD
  • 135,866
  • 28
  • 264
  • 277
Kyler Daniel
  • 15
  • 1
  • 1
  • 5
  • You can't just stop and start and create a MediaPlayer. You have to wait for it to be prepared, then start. See: http://developer.android.com/guide/topics/media/index.html scroll down to the example – Blundell Apr 03 '11 at 16:12
  • 1
    It's Android Market God trying to speak to you in his loud voice : "Yet again another soundboard you shall not post !!" :D Just kidding :) – Yahel Apr 04 '11 at 13:12
  • @Yahel I know right, but it's supposed to be a simple starter app for me to learn. "Android Market God" Hilarious. – Kyler Daniel Apr 04 '11 at 20:14

4 Answers4

10

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.

TitaniuM
  • 321
  • 1
  • 10
  • 19
0

After you create the MediaPlayer, try preparing it:

FilmSound.prepare();
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • @ Matthew I tried to prepare the media player with that code and i'm still getting unable to create media player. – Kyler Daniel Apr 04 '11 at 13:08
  • Are you able to play your file with known working code, such as API Demos? You may try it there first as a sanity check. – Matthew Apr 04 '11 at 14:07
  • what's worse is it works fine on android 2.2. I can't get it to work on any os version below that. – Kyler Daniel Apr 04 '11 at 20:22
0

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.

Community
  • 1
  • 1
BigFwoosh
  • 1,197
  • 10
  • 17
0

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);
gary
  • 4,227
  • 3
  • 31
  • 58
Chris
  • 328
  • 2
  • 2