Member
19 Points
95 Posts
Aug 23, 2017 09:44 PM|MikeT89|LINK
Hi all,
I'm trying to implement an option for users to play wave files in my MVC application, this is what I have so far
I get the data from the db, then I create a temp directory for my wave file and write the byte into the file
dt.Columns.Add("WavFile", typeof(string)); foreach (DataRow dr in dt.Rows) { try { if (dr["CallerAuthorization"] != null && dr["CallerAuthorization"].ToString() != "") { Byte[] tempAudio = (Byte[])dr["CallerAuthorization"]; if (tempAudio.Length > 0) { dr["WavFile"] = CreateWavFile(tempAudio); } else { dr["WavFile"] = ""; } } else { dr["WavFile"] = ""; } } catch (Exception e) { if (e.Source != null) Console.WriteLine(e.Source); throw; } }
public string CreateWavFile(Byte[] authorization) { string tempDir = Path.GetTempFileName(); tempDir = tempDir.Substring(0, tempDir.Length - 4); tempDir = tempDir + ".wav"; File.WriteAllBytes(tempDir, authorization); return tempDir; }
PartialView
<script> $(document).ready(function () { $(".audiobtn").click(function () { $.ajax({ type: "POST", url: "/SearchPayment/PlayAuthorization", data: { audio: $(this).find('img').attr('alt') }, cache: false, success: function (response) { }, error: function (error) { alert("Unable to play audio"); console.log(error); } }); }) }) </script>
Controller
[HttpPost] public ActionResult PlayAuthorization(string audio) { if (audio != null && audio != "") { try { using (SoundPlayer SPlayer = new SoundPlayer(audio)) { SPlayer.PlaySync(); } } catch (Exception e) { if (e.Source != null) Console.WriteLine(e.Source); throw; } } return new EmptyResult(); }
After debugging the code it's giving me at error saying 'System.InvalidOperationException: 'Sounds API only supports playing PCM wave files.'
Does anyone know how I can fix this issue?
Thanks you
Member
19 Points
95 Posts
How can I play a wave file in mvc
Aug 23, 2017 09:44 PM|MikeT89|LINK
Hi all,
I'm trying to implement an option for users to play wave files in my MVC application, this is what I have so far
I get the data from the db, then I create a temp directory for my wave file and write the byte into the file
PartialView
Controller
After debugging the code it's giving me at error saying 'System.InvalidOperationException: 'Sounds API only supports playing PCM wave files.'
Does anyone know how I can fix this issue?
Thanks you