I am working on a webapp that allows people to share links from Soundcloud by copying and pasting the URL. Then the webapp generates a running list of people, allowing users to open a modal that loads the a Soundcloud player (via SC.oEmbed). The URL is derived from the element that opens up the modal. So SC.oEmbed opens up the URL found within the href attribute.
I have everything working great, except that when the modal is closed the music keeps playing.
There doesn't seem to be a stop() or pause() function associated with this player like there is with the Widget player. The only solution I have found is upon close to have SC.oEmbed open up another random soundcloud link but with auto_play off. This works but seems really inelegant and a waste of resources. There must be a better way no? Oh, and I'm using Twitter Bootstrap too.
Here's my jQuery code:
$(document).ready( function() {
SC.initialize({
client_id: "xxxxxxxxxxxxxxxxxx",
});
$("a[data-target='#myModal']").click(function(event) {
event.preventDefault(); /* makes sure <a> link doesn't actually open new window */
var url = this.href; // gets Soundcloud URL from href in <a> element
SC.oEmbed(url, { auto_play: false }, document.getElementById("audio_1_body"));
});
/* this part below is what I want to improve -- loading up a random track, just to stop playing of track that was loaded in the modal */
$("#myModal").on("hidden.bs.modal", function(){
url = "https://soundcloud.com/the-deep-dark-woods/sugar-mama-1";
SC.oEmbed(url, { auto_play: false }, document.getElementById("audio_1_body")); //this last part is what I want to improve
});