无法使用此代码播放声音

时间:2012-05-07 11:13:59

标签: android media-player android-mediaplayer

我在我的应用程序中使用以下代码来播放声音。 但我无法发挥它。 问题是什么?

代码:

public void playSound(int resources){
    try{
        boolean mStartPlaying = true;
        MediaPlayer  mPlayer=null;
        if (mStartPlaying==true){
            mPlayer = new MediaPlayer();

            Uri uri = Uri.parse("android.resource://MY_PACKAGE/" + resources);
            mPlayer.setDataSource(getApplicationContext(),uri);
            mPlayer.prepare();
            mPlayer.start();
        } 
        else{
            mPlayer.release();
            mPlayer = null;
        }
        mStartPlaying = !mStartPlaying;
    }
    catch (IOException e){
        Log.e(LOG_TAG, "prepare() failed");
    }


}

我得到了log cat,如下所示:

05-09 05:24:22.272: VERBOSE/PlayerDriver(95): Completed command PLAYER_INIT status=PVMFFailure
05-09 05:24:22.272: ERROR/PlayerDriver(95): Command PLAYER_INIT completed with an error or info PVMFFailure
05-09 05:24:22.272: ERROR/MediaPlayer(8072): error (1, -1)
05-09 05:24:22.272: ERROR/AudioPlayTest(8072): prepare() failed
05-09 05:24:22.282: VERBOSE/PVPlayer(95): run_set_video_surface s=-2147483648, cancelled=0
05-09 05:24:22.282: VERBOSE/PlayerDriver(95): HandleInformationalEvent: PVMFInfoErrorHandlingComplete
05-09 05:24:22.282: WARN/PlayerDriver(95): PVMFInfoErrorHandlingComplete

1 个答案:

答案 0 :(得分:1)

也许您正在尝试播放不受支持的格式。 你知道什么失败了吗? :new,setDataSource,prepare,start ...而且IOException告诉你输入文件有问题,例如FileNotFoundException或EOFException(尝试按照建议打印出更具体的异常here

ps。:回答评论。