accessing SoundManager 2 across functions - javascript

Ok this thing is awesome! I got SoundManager 2 setup within my .js file and have audio playing on my page. My only question at the moment, is figuring out how to play audio outside of the soundManager.setup({...}). For example the following works great...
function mSound() {
/*SETUP SOUND MANAGER 2*/
soundManager.setup({
// where to find flash audio SWFs, as needed
url: 'audio/',
onready: function() {
console.log('SM2 is ready to play audio!');
/*MY SOUND COLLECTIONS*/
soundManager.createSound({
id: 'myIntro',
url: 'audio/Indonesia.mp3',
autoPlay: false,
volume: 15
});
soundManager.play('myIntro');
}
});
}
But if I try to place soundManager.play('myIntro'), into another function like...
function mIntro() {
soundManager.play('myIntro');
}
...the audio does not play. Any advice will be great!
Thanks

I think I solved it. By setting up local variables as parameters for my mSound() function like so...
function mSound(id,url,volume) {
this.id = id;
this.url = url;
this.volume = volume;
/*SETUP SOUND MANAGER 2*/
soundManager.setup({
url: 'audio/',
onready: function() {
//console.log('SM2 is ready to play audio!');
/*MY SOUND COLLECTIONS*/
soundManager.createSound({
id: id,
url: 'audio/'+ url,
volume: volume
});
soundManager.play(id);
}
});
}
...I'm now able to do cool stuff like this within other javascript functions and play sound!
mSound('myIntro','Indonesia.mp3',5);
And you can still still use the soundManager global object properties after you load your custom function. For example you can pause your track like this later in your code...
soundManager.togglePause('myIntro');
Hope this helps someone :)

Have you tried something like this?
var sound = soundManager.getSoundById('myIntro');
sound.play();
It's working in my project.

Related

SoundManager2 Playing Multiple Sounds at once on Android

If you have two sounds in soundmanager2 normally you can play both at the same time, on android and other mobiles calling play on one sound will stop all other sounds.
soundManager.setup({
preferFlash: false,
//, url: "swf/"
onready: function () {
soundManager.createSound({
url: [
"http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3", "http://www.html5rocks.com/en/tutorials/audio/quick/test.ogg"
],
id: "music"
});
soundManager.createSound({
url: [
"http://www.w3schools.com/html/horse.mp3", "http://www.w3schools.com/html/horse.ogg"
],
id: "horse"
});
}
}).beginDelayedInit();
$("#horse").click(function () {
soundManager.play("horse"); // <- stops other sounds on mobile :(
});
$("#music").click(function() {
soundManager.play("music"); // <- stops other sounds on mobile :(
});
http://jsbin.com/hopapagefa/1/edit?html,js,output
Is there some way i can run multiple sounds on android? If not is there some way i can detect this ability to only run one sound at once?
reasoning: I want to have constant background music in a game and play a sound when something happens ideally without stopping the background music.
I didn't find a way to run multiple sounds on android but what i did was detect if sounds had been stopped by this limitation and the restart them where they were once the sound had finished playing e.g.
var isPlaying = function (name) {
return soundManager.getSoundById(name).playState == 1;
};
// we want to play horse music over our background music
var oldPosition = soundManager.getSoundById('music').position;
soundManager.play("horse", {
onfinish: function() {
if(!isPlaying('music')) {
// mobile devices must have cut the background music off :(
// should probably check if its still appropriate to replay the background music
soundManager.play("music", {
position: oldPosition
});
}
}
});

HTML5 audio - get time played of a sound object (howler.js)

Im playing some HTML5 audio using the Howler.js library.
Currently im able to determine the total length of the audio file with sound.duration(); however im not sure how to create a timer to show how much time that has been played.
I create a simple sound object like so:
var sound = new Howl({
src: ['sound.ogg', 'sound.mp3', 'sound.wav'],
autoplay: true,
loop: false,
volume: 1,
onload: function() {
var totalSoundDuration = sound.duration();
},
onplay: function(getSoundId) {
//sound playing
},
onend: function() {
//sound play finished
}
});
I can't seem to find any method within the library (?) to check for currentTime so I can update my timer function.
An alternative route could simply be to trigger/toggle a setInterval upon onplay: like:
var currentTimeTracker=setInterval(function () {myTimer()}, 1000);
function myTimer() {
timePlayed++;
$("#time" ).html(timePlayed);
}
Not sure if that would be a good approach? Any suggestions?
Howler.js ref:
https://github.com/goldfire/howler.js/tree/2.0#global-core-properties
According to http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library sound.pos() should get the current position. The posmethod can get or set, depending on whether you pass through the optional position parameter.

Embedding a SoundCloud recording right away?

Basically I want the user to record a sound with the SoundCloud recorder and once they click save, the sound they just recorded will be embedded on to my webpage.
I use the SC.record() method to get the recording. This is my function for where I save the recordings... Right now nothing is embedded when I try to run it
$('#save a').click(function(e) {
var currentURL ="";
e.preventDefault();
SC.connect({
connected: function() {
$('.status').html('Uploading...');
SC.recordUpload({
track: {
title: 'whatever',
sharing: 'public'
}
}, function(track) {
currentURL = '"'+track.permalink_url+'"';
$('#SCtracks').append('<li id='+currentURL+'>'+currentURL+'</li>');
SC.oEmbed(currentURL, {color: "ff0066"}, document.getElementById(currentURL));
});
}
});
});
But if I go in and call SC.oEmbed with a URL to a recording I made earlier it works fine.
So I think I might be trying to embed the recording before it is fully uploaded, but I don't know where else I could put that statement.
Thanks
This answered my question. I guess you have to just keep checking the state until it is for sure done. If someone know a quicker way please answer

Soundmanager2 mp3 player button preload next sound

I use Soundmanager2's mp3 player button to play mp3s links on my website. I used the following modifcation in-order to preload the next mp3 during playing the current mp3.
play: function() {
sm.load('basicMP3Sound'+(1*this._data.oLink.title)+1);
//mycode end
pl.removeClass(this._data.oLink,this._data.className);
this._data.className = pl.css.sPlaying;
pl.addClass(this._data.oLink,this._data.className);
},
In the above example you may notce that the title tag in this._data.oLink.title which I added it to the mp3 link to handle files order in simple way, for example:
/002001.mp3
However, I noticed that the next mp3 link does not preloaded during playing the current mp3. This is because the next mp3 does not being played contiousely or start played after finish playing its previous mp3. In other word, it takes some time or delay to be downloaded.
Is there something wrong in my code? or what are your suggestions?
Notice a life demo of this mp3 layer is found in this link
Edit: Approach to work with the SoundManager inline button player
In your setup params in mp3-player-button.js under config, where playNext : true|false is found (line 39), update it to look like this:
...
this.config = {
// configuration options
playNext: true, // stop after one sound, or play through list until end
autoPlay: false, // start playing the first sound right away
preloadNext : true // preload next sound when previous sound starts to play
};
...
Then, further down under the this.events object (line 96), modify the play function to preload the next sound:
...
this.events = {
// handlers for sound events as they're started/stopped/played
play: function() {
pl.removeClass(this._data.oLink,this._data.className);
this._data.className = pl.css.sPlaying;
pl.addClass(this._data.oLink,this._data.className);
if (pl.config.preloadNext) {
var nextLink = (pl.indexByURL[this._data.oLink.id]+1);
if (nextLink<pl.links.length) {
sm.load(nextLink)
}
}
},
...
In summary, when a song starts, we check to see if there's a next song; if there is, we pull its id from the array of sounds that was created when we instantiated SoundManager. In this case, SoundManager is sm, so we simply pass the id of our next song to the sm.load() function.
Here is a live demo: http://plnkr.co/edit/mpBkfDIrLNduMAWJjRfN.
Try this approach:
Using the onplay and onfinish events of the createSound method, you can chain a series files which delegate each other.
Explicitly telling firstSound: "when you start to play, preload the next sound, and when you finish playing, play that next sound"
JavaScript
var firstSound = soundManager.createSound({
id: 'firstSound',
url: 'path/to/your/firstSound.mp3'
});
var secondSound = soundManager.createSound({
id: 'secondSound',
url: 'path/to/your/secondSound.mp3'
});
// Kickoff first sound
firstSound.load();
// Define the chain of events
firstSound.play({
onplay: function () {
// When `firstSound` starts, preload `secondSound`
secondSound.load();
},
onfinish: function () {
// Repeat, with next sound
secondSound.play({...
})
}
});

Reference URL with JavaScript to play sound?

Im using soundcloud dot com to upload my sounds. i want to press a button within my mobile application and have that sound play.
So basically i want my sound to be referenced from the URL that I am given when the button is pressed by the user.
I need to do it with Javascript only. No HTML 5 please. Any help is greatly appreciated cause this is Xtremely frustrating. Any ideas? Thanks in advance.
It's pretty simple to get started really:
function playSound(url) {
var a = new Audio(url);
a.play();
}
Use that as whatever event handler you want for your application. Of course, I'd hope you'd want to do more than just play (for example, maybe pause would be good too?), but it's a start.
let sound = new Audio("http://sound.com/mySound.mp3");
//on play event:
sound.onplay = () => {
};
//on pause event:
sound.onpause = () => {
};
//on end event:
sound.onended = () => {
};
//play:
sound.play();
//pause:
sound.pause();
//stop:
sound.pause();
sound.currentTime = 0;
Use jPlayer to play sound using Javascript. This will take you a lot of time and frustration.
jplayer.org/
Here's what your code might look like with jPlayer. Note: You're not forced to use a skin with jPlayer because all it is is just an API to play audio.
Example code to play a video or audio on load.
$(function() { // executed when $(document).ready()
$("#jpId").jPlayer( {
ready: function () {
$(this).jPlayer("setMedia", {
m4v: "http://www.myDomain.com/myVideo.m4v" // Defines the m4v url
}).jPlayer("play"); // Attempts to Auto-Play the media
},
supplied: "m4v",
swfPath: "jPlayer/js"
});
});
http://jplayer.org/latest/developer-guide/

Categories

Resources