I have this Webservice method that I use to get audio from the database. Server-side:
[WebMethod]
public void PlayAudio(int id)
{
byte[] bytes = new byte[0];
using (The_FactoryDBContext db = new The_FactoryDBContext())
{
if (db.Words.FirstOrDefault(word => word.wordID == id).engAudio != null)
{
bytes = db.Words.FirstOrDefault(word => word.wordID == id).engAudio;
MemoryStream ms = new MemoryStream(bytes);
Context.Response.Clear();
Context.Response.AddHeader("ContentType ", "audio/wav");
Context.Response.BinaryWrite(ms.ToArray());
Context.Response.Flush();
Context.Response.Close();
}
}
}
I have this code to play this audio in the browser:
var audio = document.createElement('audio');
audio.id = "test";
audio.autoplay = false;
audio.preload = true;
audio.oncanplaythrough = function () {
foo.disabled = false;
};
var id = $("[id$=hiddenWord_id]").val();
audio.src =// below are the different ways ive tried
foo.onclick = function () {
audio.play();
}
I have tried this both this:
audio.src = "../../WebService.asmx/PlayAudio/?id=" + id;
and this to reach the webservie
document.getElementById('test').src = PlayAudio();
function PlayAudio() {
var id = $("[id$=hiddenWord_id]").val();
$.ajax({
url: "../../WebService.asmx/PlayAudio",
data: "{ 'id': '" + id + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
};
However none of the above seem to work.