无法创建MediaPlayer,音板的错误

时间:2011-04-03 16:06:29

标签: android media-player

调试时我无法创建MediaPlayer

当我点击声音按钮时,应用加载然后挂起。 这是logcat错误 我似乎无法找到问题所在。

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)

这是我的代码。

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

以下是准备代码:

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

4 个答案:

答案 0 :(得分:10)

我刚才有类似的问题。我通过简单地将以下内容添加到您的清单文件中来解决它:

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

在ICS之前,不需要互联网许可。

答案 1 :(得分:0)

创建MediaPlayer后,请尝试准备它:

FilmSound.prepare();

答案 2 :(得分:0)

请检查此问题:Android -- Can't play any videos (mp4/mov/3gp/etc.)?

您是否尝试过以不同格式播放文件?也许你的声音需要一些Android没有的编解码器,所以prepare()失败了。

答案 3 :(得分:0)

删除create()之外的onClickListener方法。现在,当您启动应用程序时,它将创建10个FilmSound实例,因为代码在onClickListener之外,然后当您单击按钮时,它将创建FilmSound的第11个实例。可能这是Android 2.2下面的内存不足。使用create()方法后,您无需准备,它将在第一次创建后进行准备。

另外,我非常确定isPlaying()可以抛出异常。

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