当用户点击正确答案时如何播放声音

Posted

技术标签:

【中文标题】当用户点击正确答案时如何播放声音【英文标题】:How to play a sound when a user clicks the right answer 【发布时间】:2016-08-26 10:52:15 【问题描述】:

我正在使用 AS3 在 Flash 中创建一个测验。当用户单击正确或错误的答案时,我无法尝试包含声音。

如何正确播放声音?

这是下面的代码

import flash.media.Sound;

//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)

    if (myTextField11.text == answer1)
            trace("Correct answer!!");
            gotoAndPlay(2,"quiz1");
            count = count + 10;
            scoreBox.text = (count).toString();
            setTimeout(gotoAndPlay, 1250, 1, "quiz2");


         else 
                trace(myTextField11);
                gotoAndPlay(3,"quiz1"); 
                var mySound:Sound = new WrongAnswer();
                WrongAnswer.play();
                setTimeout(gotoAndPlay, 1250, 1,"quiz2");

                

我现在已经做到了,但没有运气:

import flash.media.Sound;

//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)

    if (myTextField11.text == answer1)
            trace("Correct answer!!");
            gotoAndPlay(2,"quiz1");
            count = count + 10;
            scoreBox.text = (count).toString();
            setTimeout(gotoAndPlay, 1250, 1, "quiz2");


         else 
                trace(myTextField11);
                gotoAndPlay(3,"quiz1");

                MediaPlayer player = MediaPlayer player=MediaPlayer.create(YourActivity.this,R.raw.WrongAnswer);
player.start();


            setTimeout(gotoAndPlay, 1250, 1,"quiz2");

            

【问题讨论】:

【参考方案1】:

如果您将mySound 声明为WrongAnswer(); 声音的变量名,则在通过代码访问时使用完全相同的名称。

import flash.media.Sound;

//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)

    if (myTextField11.text == answer1)
    
        trace("Correct answer!!");
        gotoAndPlay(2,"quiz1");
        count = count + 10;
        scoreBox.text = (count).toString();
        setTimeout(gotoAndPlay, 1250, 1, "quiz2");
     
    else 
    
        trace(myTextField11);
        gotoAndPlay(3,"quiz1"); 
        var mySound:Sound = new WrongAnswer();
        //WrongAnswer.play();
        mySound.play();
        setTimeout(gotoAndPlay, 1250, 1,"quiz2");
    


如果仍有问题,请告诉我。我要给出的替代方案是通过代码嵌入声音,但在这里可能没有必要......

【讨论】:

【参考方案2】:
MediaPlayer player=MediaPlayer.create(YourActivity.this,R.raw.nameofile);
player.start();

此代码有助于播放音乐。 将代码放在 Onclick() 方法中。

【讨论】:

感谢您的回复,我已经尝试过这个(编辑中的示例)但没有运气。我做对了吗? 您的活动名称是什么?我只是随机保留了YourActivity!你需要在那里写下你的活动名称,你的班级名称 @TripatheeGaurav,这是 android SDK 代码吗? Asker 正在使用 AS3 (Flash) 代码创建 Android 应用。 问题是针对 AS3,而不是针对 Java (Android)。投反对票。

以上是关于当用户点击正确答案时如何播放声音的主要内容,如果未能解决你的问题,请参考以下文章