Answered by:
Audio setting src from controller

Question
-
User828781522 posted
<audio controls> <source src="/Music/StreamUploadedSongs" type="audio/mpeg"> </audio>
public string StreamUploadedSongs() { return Server.MapPath(Url.Content("~/content/tsp-01-cro_magnon_man.mp3")); }
The above code doesn't work and just returns I:\Users\Name\Desktop\MusicSite\MusicSite\MusicSite\content\tsp-01-cro_magnon_man.mp3
if I put http://localhost:6060/Music/StreamUploadedSongs into the url. Where as I want to set the src to this string.
If I do:
<audio controls> <source src="~/content/tsp-01-cro_magnon_man.mp3" type="audio/mpeg"> </audio>
it works, but I want to use controller.
Wednesday, August 19, 2015 12:53 PM
Answers
-
User1644755831 posted
Hello sity,
Can you please try this and see if it works.
@{ ViewBag.Title = "test2"; Layout = null; } <!DOCTYPE html> <html> <head> <title>content</title> <script src="~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> function changeclick() { $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", url: '@Url.Action("StreamUploadedSongs", "Home")', async: false, success: function (src) { change(src); }, error: function (error) { } }); } function change(sourceUrl) { var audio = $("#audio"); $("#mp3source").attr("src", sourceUrl); audio[0].pause(); audio[0].load();//suspends and restores all audio element audio[0].play(); } </script> </head> <body> <audio id="audio" controls> <source id="mp3source"type="audio/mpeg"> Your browser does not support the audio element. </audio> <input type="button" onclick="changeclick();" /> </body> </html>
Change your controller like this you need not to return absolute path you just need the realative path. Server.MapPath will returns server's physical location client side cant access it.
public string StreamUploadedSongs() { return "~/Content/1.mp3"; }
Hope this helps.
With Regards,
Krunal Parekh
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 21, 2015 2:16 AM
All replies
-
User1258826322 posted
give id to the source element .
you have make a jquery $.ajax call to your below action method ,and change the return type of action method to jsonresult and return through json method. In the success of your $.ajax call use jquery like $("#id").attr("attrName",value returned) -- use value returned from the ajax call to update the attribute value . for $.ajax documentation do little browsing
public jsonresult StreamUploadedSongs() {
return Json(new{ tempVar=
Server.MapPath(Url.Content("~/content/tsp-01-cro_magnon_man.mp3")
}, JsonRequestBehavior.AllowGet);
Thursday, August 20, 2015 5:42 AM -
User1644755831 posted
Hello sity,
Can you please try this and see if it works.
@{ ViewBag.Title = "test2"; Layout = null; } <!DOCTYPE html> <html> <head> <title>content</title> <script src="~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> function changeclick() { $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", url: '@Url.Action("StreamUploadedSongs", "Home")', async: false, success: function (src) { change(src); }, error: function (error) { } }); } function change(sourceUrl) { var audio = $("#audio"); $("#mp3source").attr("src", sourceUrl); audio[0].pause(); audio[0].load();//suspends and restores all audio element audio[0].play(); } </script> </head> <body> <audio id="audio" controls> <source id="mp3source"type="audio/mpeg"> Your browser does not support the audio element. </audio> <input type="button" onclick="changeclick();" /> </body> </html>
Change your controller like this you need not to return absolute path you just need the realative path. Server.MapPath will returns server's physical location client side cant access it.
public string StreamUploadedSongs() { return "~/Content/1.mp3"; }
Hope this helps.
With Regards,
Krunal Parekh
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 21, 2015 2:16 AM