2013-08-14 4 views
0

Итак, у меня есть небольшое приложение, основанное на игре (ножницы для бумажной бумаги), но я хочу вернуть текст с победителем и звуковым эффектом ... Итак, у меня есть 2 небольших mp3-файла аплодисменты.mp3 и fail.mp3 Как я могу сделать, чтобы вернуть текст и воспроизвести файл :(? Я пробовал несколько вещей, но он говорит мне «undefined» или другие ошибки ... :(или, может быть, он возвращает мне текст, который я могу купить услышать звук ..JavaScript return text + soundsound

var compare = function(choice1) { 
    var winSound = document.getElementById('./sounds/Applause.mp3'); 
    var lostSound = document.getElementById('./sounds/Fail.mp3'); 
    var computerChoice = Math.random(); 
    if (computerChoice < 0.34) { 
     computerChoice = "rock"; 
    } else if(computerChoice <= 0.67) { 
     computerChoice = "paper"; 
    } else { 
     computerChoice = "scissors"; 
    } 
    if (choice1 === computerChoice) { 
     return('The result is a tie!'); 
     } 
    else if (choice1 === 'rock') { 
     if (computerChoice === 'scissors') { 
      result = 'You are the winner.' + winSound.play(); 
     } else { 
      return 'Computer is the winner.'; 
     } 
    } 
    else if (choice1 === 'paper') { 
     if (computerChoice === 'rock') { 
      return 'You are the winner.'; 
     } else { 
      return 'Computer is the winner.'; 
     } 
    } 
    else { 
     if (computerChoice === 'rock') { 
      return 'You are the winner.'; 
     } else { 
      return 'Computer is the winner.'; 
     } 
    } 
}; 

var game = function(choice1) { 
    document.getElementById('result').innerHTML = compare(choice1); 
}; 

ответ

1

создается звуковой объект:

var audio = new Audio(); 

Затем назначьте СРК на звук, который вы хотите воспроизвести в обратном вызове, и играть:

audio.src = './sounds/Applause.mp3'; 
audio.load(); 
audio.play(); 

return(text); 

Возможно, вы захотите c заблаговременно произносите звуки, чтобы не было задержки.

1

Вам не нужно, чтобы вернуть звук вместе с текстом. Таким образом, вы можете заменить эту строку

 result = 'You are the winner.' + winSound.play(); 

с:

 winSound.play(); 
     result = 'You are the winner.'; 

Кроме того, он не выглядит, как вы правильно загружая аудио. Вероятно, вы захотели положить что-то вроде <audio id="win-sound" src="./sounds/Applause.mp3"></audio> в свой HTML, а затем использовать document.getElementById('win-sound') (см. http://www.w3schools.com/html/html5_audio.asp).

0

winSound.play() не возвращает строку, но имеет побочный эффект воспроизведения звука. Кроме того, вы присваиваете результат переменной вместо ее возвращения. Таким образом, вместо

result = 'You are the winner.' + winSound.play(); 

пишут:

winSound.play(); 
return 'You are the winner.'; 
0

Вы можете либо создать <audio> тег в abhitalks рекомендуется, или добавьте к источнику HTML:

HTML

<audio id="player"></audio> 

Javascript

var winSound = './sounds/Applause.mp3'; 
var lostSound = './sounds/Fail.mp3'; 
var player = document.getElementById('player'); //audio element 
if (result == 'You are the winner.') { 
    player.src = winSound; 
} else if (result == 'Computer is the winner.') { 
    player.src = lostSound; 
} else { 
    player.src = ''; 
} 
player.load(); 
player.play(); 

JSFiddle: full code/result page