Stopping a dynamically created Soundcloud oEmbed player when closing a modal - javascript

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
});

Does not seem that there is a way to do so from SC JS SDK.
Best you can do is to delete SC iframe:
$('#audio_1_body').html('')

Related

Stopping a vimeo video on close of a modal, when multiple videos are in one page

I have multiple pop ups on one page. each pop up not coded with bootstrap, has a bootstrap carousel inside it. within those bootstrap carousels there may or may not be a vimeo video.
The issue were having is stopping the video from playing on dismiss of the popup or pressing the left and right buttons because with there being multiple videos in different carousels the script below copies the video from the first src it finds and replaces every video with that src.
I've tried using closest() and find() and also tried both of those on the script within the click function to say (this).closest($frame) but this doesn't work either.
Any help would be greatly appreciated.
function stopVideo() {
var $frame = $('iframe#nofocusvideo');
// saves the current iframe source
var vidsrc = $frame.attr('src');
// sets the source to nothing, stopping the video
$frame.attr('src', '');
// sets it back to the correct link so that it reloads immediately on the next window open
$frame.attr('src', vidsrc);
}
$(".carousel-control-prev").click(function(){
stopVideo();
});
$(".carousel-control-next").click(function(){
stopVideo();
});
$(".fa-times").click(function(){
stopVideo();
});
var $frame = $('.carousel-item.active').find('iframe'); fixed this issue

How to get videojs to unload automatically

I'm currently using VideoJS in a Rails application (where there is a video player on every page) to display videos and I'm encountering a very strange problem:
The player works perfectly fine on the first page I visit but if I play the video and visit another page, the video from the first page keeps playing in the background even though the page isn't open anymore (you can hear the audio) and the video on the page you visited doesn't initialize properly (options aren't applied which means the video can't be played because controls are an option) and the console reads VIDEOJS: WARN: Player "player" is already initialised. Options will not be applied.
How to I get VideoJS to unload itself when the user leaves the page and why does it keep playing in the first place, the HTML5 video player didn't do that before.
Is the best way around this to get VideoJS to reload itself manually on page load? If so, how can that be done?
Note: If I navigate to any other page within the website the videos continue to not initialize, but if I reload the page, any page, the video on said page works again.
Note 2: Turns out that the onbeforeunload javascript event doesn't even fire if I click a link to another page, it only fires if you're going to a whole different website, so I can't even use that to .dispose() VideoJS on page unload.
Note 3: .reset() doesn't seem to be working either.
You can check to see if the player already exists and unload it, then reload it.
I was actually able to figure out a fairly simple and elegant solution:
if (player) {player.dispose()} else {var player}
player = videojs('player', {
//options
});
First it checks to see if the player exists. If it does, it destroys the VideoJS instance. If it doesn't, it creates the variable. Then it initializes the player.
By Referring this issue : https://github.com/videojs/video.js/issues/2904
We can re-write the above solution to something like this:
const playerId = 'video-player';
const videoOptions = {
controls: true,
sources: [{
src: 'test-file.mp4',
type: 'video/mp4',
}]
};
let player;
let players = videojs.players;
const imaOptions = { adTagUrl };
if (players && Object.keys(players).length) {
player = players[playerId];
player.dispose();
}
player = videojs(playerId,videoOptions);
player.ima(imaOptions);
I found this one to be the solution:
var oldPlayer = document.getElementById('my-player');
videojs(oldPlayer).dispose();
it's in the docs actually

How to pause videos with Javascript

So i have a small problem in my website, first of all i made it with Wordpress, and i have a page with tabs every tab contain a video embedded from Youtube, my problem is when i passed from a tab to another the previous video doesn't stop, this is my code :
[tabgroup][tab title="Sport"]
[embedyt]https://www.youtube.com/watch?v=FVsgso273EE?theme=light[/embedyt][/tab]
[/tab]
[tab title="Action"]
[embedyt]https://www.youtube.com/watch?v=zuzaxlddWbk?theme=light[/embedyt][/tab]
[/tab]
[tab title="Strategi"]
[embedyt]https://www.youtube.com/watch?v=LKrRCADS7To?theme=light[/embedyt][/tab]
[/tab]
[/tabgroup]
and this is my page to understand the situation My-website
in my website you will find the videos tabs if you click in the SPIL menu item.
so plz if someone has any idea to do that i will be very appreciative :)
You can use the Youtube JavaScript API to do stuff like play, pause, seek to a certain time in a video, set the volume, mute the player, and other useful functions.
https://developers.google.com/youtube/js_api_reference
Just attach an event handler to your tabs that when clicked, get a reference to the current video, pause it then play the new one.
Here's a live demo of such functionality: https://developers.google.com/youtube/youtube_player_demo
If you are using HTML video tag you can use the code given below
var myVideo=document.getElementById("video1");
function playVid()
{
myVideo.play();
}
function pauseVid()
{
myVideo.pause();
}
Follow the given link as example http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_av_met_play_pause

Play an SWF when a javascript button is clicked

I'm new to Javascript and to Flash, and I'm developing a browser Flash video recorder.
Right now, when I direct my browser to a certain URL, it asks for permission to access the webcam and starts recording - and streaming to a Red5 server on Ubuntu.
I now need to add a javascript button, which when clicked will start the recording, and when clicked again, will stop it. Can someone tell me how to play the .swf file only when the button is clicked? And how to stop playing it when the button is clicked again?
Thanks.
As Pete said, ExternalInterface is certainly what's needed to make function calls between a SWF and the surrounding HTML page.
You can also use swfobject to do cross-browser compatible SWF embedding/loading into a page dynamically.
I've made a little demo of the two features you're looking for here: http://akineticblog.com/fl/ExtIntDemo/index.html
The AS3 code for this is:
import flash.external.ExternalInterface;
var recording:Boolean = false;
//These are two TextFields instantiated on the stage as visual indicators
recordingTF.visible = false;
notRecordingTF.visible = true;
function recording_toggle():void {
recording = ! recording;
//Your actual record start/stop code would go here//
recordingTF.visible = recording;
notRecordingTF.visible = ! recording;
}
if (ExternalInterface.available) {
//This registers the AS3-side function to the JS-side reference
ExternalInterface.addCallback("recording_toggle", recording_toggle);
}
For the HTML/JS side, you can check the page source of the above link, but the main parts are:
function swf_load() {
swfobject.embedSWF("ExtIntDemo.swf", "flashMovie", "500", "250", "10.1", "swf/expressinstall.swf", flashvars, params, attributes);
};
- to be called when you want to load in the SWF, replacing a div with the id 'flashMovie'.
And:
<button onclick="flashMovie.recording_toggle()">Toggle recording</button>
to send the 'recording_toggle' function call into the SWF itself.
You can use ExternalInterface to communicate between JavaScript and AS3. After that it's just a case of making the button call the AS3 functions to you want to perform.

Mozilla Addon SDK - Problem with Reddit example on 2nd page

I got this problem during development of my own addon, but I get the same with the reddit example, so I'll use that for simplicity.
Using the exact code from the example found here, this is what happens.
Reddit Example
This example add-on creates a panel containing the mobile version of Reddit. When the user clicks on the title of a story in the panel, the add-on opens the linked story in a new tab in the main browser window.
To accomplish this the add-on needs to run a content script in the context of the Reddit page which intercepts mouse clicks on each title link and fetches the link's target URL. The content script then needs to send the URL to the add-on script.
main.js:
var data = require("self").data;
var reddit_panel = require("panel").Panel({
width: 240,
height: 320,
contentURL: "http://www.reddit.com/.mobile?keep_extension=True",
contentScriptFile: [data.url("jquery-1.4.4.min.js"),
data.url("panel.js")]
});
reddit_panel.port.on("click", function(url) {
require("tabs").open(url);
});
require("widget").Widget({
id: "open-reddit-btn",
label: "Reddit",
contentURL: "http://www.reddit.com/static/favicon.ico",
panel: reddit_panel
});
panel.js:
$(window).click(function (event) {
var t = event.target;
// Don't intercept the click if it isn't on a link.
if (t.nodeName != "A")
return;
// Don't intercept the click if it was on one of the links in the header
// or next/previous footer, since those links should load in the panel itself.
if ($(t).parents('#header').length || $(t).parents('.nextprev').length)
return;
// Intercept the click, passing it to the addon, which will load it in a tab.
event.stopPropagation();
event.preventDefault();
self.port.emit('click', t.toString());
});
The icon is displayed in the bar, and clicking it launches the panel. Clicking a link within the panel opens it in a new tab - just as described and expected.
Clicking the "next page" link within the tab successfully fetches the next page, within the panel - as expected.
Clicking a link on the 2nd page does NOT open it in a tab, it opens it WITHIN the panel.
Here's my guess: When the page reloads within the panel, it does not reload the script specified in the contentScriptFile. Does anyone else experience this? And is there a workaround?
I'm using SDK 1.0 and FF 5.0
Question cross-posted on the Addon forum here
Recieved this answer at the Mozilla forum, and will post here as well for future reference.
Perhaps you are seeing Bug 667664 - Panel content scripts don't work after reloading or changing location. I think the workaround is to load the content in an iframe.
Which I believe might be the case, I'll try this during the day and report back.
EDIT: iframe seems to do the trick

Categories