音频不在线程Android中播放

时间:2016-11-22 10:46:30

标签: java android android-mediaplayer

所以我有一个我想在一个帖子中播放的音频文件,这是我留下评论的地方//试着在这里播放音频。但即使有逻辑,以确保它只播放一次没有播放音频。相反,它似乎迫使应用程序跳过帧而不是因为帧速率不一致而导致游戏出现一些故障。

这是我一直在使用的代码。

 while (isRunning) {

            // Go back to the if statement and check if the surface still isn't valid
            if (!mySurfaceHolder.getSurface().isValid())
                continue;

            // Lock the canvas for editing, make changes and unlock with new changes
            final Canvas canvas = mySurfaceHolder.lockCanvas();
            canvas.drawRect(0,0,canvas.getWidth(), canvas.getHeight(), pWhite);

            // Create the block to check the answer
            answerObject = new AnswerObject(canvas);
            animalObject.move(canvas, tilt);

            if(answerObject.onCollision(animalObject) != null){


                // Inside of box else is outside of box
                if (answerObject.onCollision(animalObject)) {

                    // If the animal is part of that enviroment
                    if (verifyEnviroment(enviromentObject.getAnimals())){

                        isCorrect = true;

                    } else {

                        isCorrect = false;
                    }

                } else {

                    // If the animal is part of that enviroment
                    if (!verifyEnviroment(enviromentObject.getAnimals())){

                        // Tried playing audio here 
                        isCorrect = true;

                    } else {

                        // Tried playing audio here
                        isCorrect = false;
                    }
                }

                gameActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (isCorrect){


                            scoreObject.setScore(10);

                        } else {

                            healthObject.setHealth(10);
                            healthObject.setHealthImgs(healthObject.getHealth());

                            if (healthObject.getHealth() <= 0){
                                isRunning = false;
                            }

                        }

                        resetGameObjects();
                    }
                });
            }

            mySurfaceHolder.unlockCanvasAndPost(canvas);
        }

0 个答案:

没有答案