Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/feed-media-audio-player.min.js

Large diffs are not rendered by default.

29 changes: 25 additions & 4 deletions html/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,43 @@
<div class="status"></div>
</div>

<div>
<select id="ip">
<option value="US" selected>US</option>
<option value="CA">CA</option>
</select>
</div>


<script>
console.log('**** PAGE RELOAD ******');

const ips = {
US: '75.37.70.130',
CA: '192.206.151.131'
};

const country = this.location.search ? this.location.search.slice(1) : 'US';
const ip = ips[country];

$('#ip').val(country);

$('#ip').on('change', function() {
window.location.search = $(this).val();
});

var simulcast = 'nGwPEXk4js9V4Yp3HcyM2i';

// Create basic player.
console.log('pre');
var player = Feed.resumable(60000) ||
new Feed.Player('demo', 'demo', {
new Feed.Player('country', 'country', {
baseUrl: 'https://stage.feed.fm',
debug: true,
simulcast: simulcast,
remoteLogging: true
});
console.log("post!");

player.session.extraHeaders = { 'X-Streaming-For': ips[country] };
console.log('state', player.getCurrentState());

// Display all the events the player triggers
Expand Down
5 changes: 3 additions & 2 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ function supports_html5_storage() {
var Player = function (token, secret, options) {
if (!secret) {
// restore from saved state
this._restore(token);

// this._restore(token);
console.log("^^^ this was messing up chromecast");
} else {
options = options || {};
options.maxRetries = options.maxRetries || 10;
Expand Down Expand Up @@ -499,6 +499,7 @@ Player.prototype._onSoundPause = function (playId) {
};

Player.prototype._onSoundFinish = function (playId, withError) {

if (!this.state.activePlay || (this.state.activePlay.id !== playId)) {
log('received sound finish, but active play does not match', this.state.activePlay, playId);
return;
Expand Down
130 changes: 92 additions & 38 deletions src/speaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const IOS = [
// iPad on iOS 13 detection
|| (navigator.userAgent.includes('Mac') && 'ontouchend' in document);

const CHROMECAST = navigator.userAgent.includes('CrKey');

const brokenWebkit = IOS && /OS 13_[543210]/i.test(navigator.userAgent);

const SILENCE = IOS ?
Expand All @@ -82,6 +84,8 @@ const SILENCE = IOS ?
//const SILENCE = 'https://dgase5ckewowv.cloudfront.net/feedfm-audio/1573592316-88123.m4a';

var Sound = function (speaker, options, id, url) {


var obj = Object.assign(this, Events);

obj.id = id;
Expand All @@ -92,6 +96,9 @@ var Sound = function (speaker, options, id, url) {
obj.speaker = speaker;
obj.loaded = false;

obj.savedSrc = '';
obj.savedPos = 0;

if (options) {
this.startPosition = +options.startPosition;
this.endPosition = +options.endPosition;
Expand Down Expand Up @@ -145,7 +152,10 @@ Sound.prototype = {
// pause playback of the current sound clip
pause: function () {
log('sound ' + this.id + ' pause');

return this.speaker._pauseSound(this);


},

// resume playback of the current sound clip
Expand Down Expand Up @@ -296,10 +306,14 @@ Speaker.prototype = {
log('initialize audio called from', e);
}

if (!CHROMECAST){
this.fading = this._createAudio(SILENCE);
this.preparing = this._createAudio(this.prepareWhenReady ? this.prepareWhenReady : SILENCE);
}

this.audioContext = createAudioContext();

this.active = this._createAudio(SILENCE);
this.fading = this._createAudio(SILENCE);

const pwr = this.prepareWhenReady;
if (pwr) {
Expand Down Expand Up @@ -415,7 +429,7 @@ Speaker.prototype = {
return;
}

if (audio === this.fading.audio) {
if (!CHROMECAST && (audio === this.fading.audio)) {
revoke(audio);
audio.src = SILENCE;
this.fading.sound = null;
Expand Down Expand Up @@ -473,7 +487,7 @@ Speaker.prototype = {
return;
}

if ((audio === this.fading.audio) && this.fading.sound) {
if (!CHROMECAST && audio === this.fading.audio && this.fading.sound) {
if (this.fading.sound.endPosition && (audio.currentTime >= (this.fading.sound.endPosition / 1000))) {
this.fading.sound = null;
revoke(this.fading.audio);
Expand Down Expand Up @@ -502,18 +516,16 @@ Speaker.prototype = {
this.active.sound = null;
revoke(this.active.audio);
this.active.audio.src = SILENCE;

sound.trigger('finish');

} else if (this.active.sound.fadeOutEnd && (audio.currentTime >= this.active.sound.fadeOutStart)) {
} else if (!CHROMECAST && this.active.sound.fadeOutEnd && (audio.currentTime >= this.active.sound.fadeOutStart)) {
// song hit start of fade out
this._setVolume(this.active);

// active becomes fading, and fading becomes active
var fading = this.fading;
this.fading = this.active;
this.active = fading;

this.active.sound = null; // not used any more

// pretend the song finished
Expand Down Expand Up @@ -610,7 +622,14 @@ Speaker.prototype = {
this.outstandingSounds[sound.id] = sound;

// start loading sound, if we can
//this.prepare(url, optionsAndCallbacks.startPosition);
if (!this.active || !this.active.audio) {
log('no audio prepared yet, so preparing when ready');
this.prepareWhenReady = sound.url;

} else if (CHROMECAST || this.preparing.audio.src === SILENCE) {
log('preparing sound now');
this._prepare(sound.url, sound.startPosition);
}

return sound;
},
Expand Down Expand Up @@ -641,8 +660,10 @@ Speaker.prototype = {
}

var ranges = this.active.audio.buffered;


var range = (ranges.length > 0) && ranges.end(ranges.length - 1);
if (range >= this.active.audio.duration) {
if (!CHROMECAST && (ranges.length > 0) && (ranges.end(ranges.length - 1) >= this.active.audio.duration)) {
log('active song has loaded enough, so preparing', url);
if (hasBlobSupport()) {
return this._preload(url);
Expand Down Expand Up @@ -863,18 +884,17 @@ Speaker.prototype = {
// empty out any pending request
this.prepareWhenReady = null;

if (!url) {
return false;
}
if (CHROMECAST){
log('preparing ' + url);

if (this.preparing && (this.preparing.audio.src === url) && this.preparing.canplaythrough) {
log('play already prepared!');
// song is already prepared!
return true;
}
if (this.active.audio.playing) {
this.active.audio.pause();
}

if (this.preparing.audio.src !== url) {
log('preparing', url);
this.active.audio.src = url;
}
else if (this.preparing.audio.src !== url) {
log('preparing ' + url);

if (this.preparing.audio.playing) {
this.preparing.audio.pause();
Expand All @@ -886,7 +906,11 @@ Speaker.prototype = {
this.preparing.audio.src = url;
}

if (startPosition && (this.preparing.audio.currentTime !== startPosition)) {
if (CHROMECAST && (startPosition && this.active.audio.currentTime !== startPosition)) {
log('advancing preparing audio to', startPosition / 1000);
this.active.audio.currentTime = startPosition / 1000;
}
else if (startPosition && (this.preparing.audio.currentTime !== startPosition)) {
log('advancing preparing audio to', startPosition / 1000);
this.preparing.audio.currentTime = startPosition / 1000;
}
Expand All @@ -908,9 +932,13 @@ Speaker.prototype = {
}

if (this.active.sound === sound) {
if (this.active.audio.paused) {
if (this.active.audio.paused || this.active.audio.src === 'not-a-real-file-paused' ) {
log(sound.id + ' was paused, so resuming');

if (CHROMECAST)
{
this.active.audio.currentTime = sound.savedPos;
this.active.audio.src = sound.savedSrc;
}
// resume playback
this.active.audio.play()
.then(function () {
Expand All @@ -924,7 +952,7 @@ Speaker.prototype = {
sound.trigger('finish');
});

if (this.fading.sound) {
if (!CHROMECAST && this.fading.sound) {
this.fading.audio.play()
.then(function () {
log('resumed fading playback');
Expand All @@ -946,7 +974,7 @@ Speaker.prototype = {
return;
}

if (this.preloaded && (this.preloaded.url === sound.url) && this.preloaded.blobUrl) {
if (!CHROMECAST && this.preloaded && (this.preloaded.url === sound.url) && this.preloaded.blobUrl) {
log(sound.id + ' using preloaded audio', this.preloaded);

if (this.preparing.audio.playing) {
Expand All @@ -970,9 +998,19 @@ Speaker.prototype = {

// swap prepared -> active
var active = this.active;
this.active = this.preparing;
this.preparing = active;
if (!CHROMECAST){
this.active = this.preparing;
this.preparing = active; // don't throw sound object in active until playback starts (below)
}

// notify clients that whatever was previously playing has finished
if (!CHROMECAST && this.preparing.sound) {
this.preparing.audio.src = SILENCE;

var finishedSound = this.preparing.sound;
this.preparing.sound = null;
finishedSound.trigger('finish');
}
this.preparing.canplaythrough = false;

revoke(this.preparing.audio);
Expand Down Expand Up @@ -1091,11 +1129,13 @@ Speaker.prototype = {
this._setVolume(this.active);

// active becomes fading, and fading becomes active
var fading = this.fading;
this.fading = this.active;
this.active = fading;

this.active.sound = null; // not used any more
if (!CHROMECAST)
{
var fading = this.fading;
this.fading = this.active;
this.active = fading;
this.active.sound = null;
} // not used any more
}

} else {
Expand All @@ -1120,17 +1160,31 @@ Speaker.prototype = {
},

_pauseSound: function (sound) {
if (this.active && (sound === this.active.sound)) {
if (sound.awaitingPlayResponse) {
// wait for the play() call to complete before pausing, otherwise
// we'll get an exception
this.active.pauseAfterPlay = true;
if (this.active && (sound.url === this.active.audio.src)) {
if (this.active.sound === sound) {
if (CHROMECAST)
{
sound.savedPos = this.active.audio.currentTime;
sound.savedSrc =this.active.audio.src;
this.active.audio.src = 'not-a-real-file-paused';
}
else
{
this.active.audio.pause();
}

//
} else {
this.active.audio.pause();
}
// if active.sound isn't assigned, then the song is still being loaded.
// if we try to pause() right now, it will cause the play() to throw an
// exception... so just throw up a flag for this
this.active.pauseAfterPlay = true;
}
this.active.audio.pause();

}

if (this.fading && this.fading.audio) {
if (!CHROMECAST && this.fading && this.fading.audio) {
this.fading.audio.pause();
}
},
Expand Down