Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
- Added comments
- Bug fixes
- Performance improvements (instead of calling getName(), names of
artist and song are saved in a variable)
- Removed useless readyStateInterval

I hope that I’ve fixed marquee (finally) as well!
  • Loading branch information
tomasko126 committed Nov 15, 2014
1 parent 0822b56 commit 684c046
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 86 deletions.
Binary file modified platforms/blackberry10/build/device/bb10app.bar
Binary file not shown.
Binary file modified platforms/blackberry10/build/simulator/bb10app.bar
Binary file not shown.
19 changes: 13 additions & 6 deletions platforms/blackberry10/www/scripts/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ $(".radio").click(function() {
});

// Fetch metadata for currently playing track
if (actual_cover_url) {
$('<img src="' + actual_cover_url + '">').load(function() {
$(this).insertBefore('#playing').addClass("radioimg");
});
if (isPlaying) {
if (actualCoverUrl) {
$('<img src="' + actualCoverUrl + '">').load(function() {
$(this).insertBefore('#playing').addClass("radioimg");
});
} else {
$('<img src="images/' + station + '.png">').load(function() {
$(this).insertBefore('#playing').addClass("radioimg");
});
}
}

if (station) {
get_song(station);
if (songMetadata && isPlaying) {
$("#artist").text(songMetadata[0]);
$("#song").text(songMetadata[1]);
}
108 changes: 50 additions & 58 deletions platforms/blackberry10/www/scripts/html5audio.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// TODO: Create an object for every station, low-priority atm
var expres = {
station_name: "Rádio EXPRES",
station_description: "Baví nás baviť vás",
station_icon: "../images/expres.png",
stationName: "Rádio EXPRES",
stationDescription: "Baví nás baviť vás",
stationIcon: "../images/expres.png",
stream: {
20: "http://85.248.7.162:8000/20.aac",
48: "http://85.248.7.162:8000/48.aac",
Expand All @@ -12,13 +12,13 @@ var expres = {
}

// Init variables
var actual_cover_url = null;
var getMetadata = null; // Function - setInterval() fn, which calls get_song(radio) for updating name of song
var isPlaying = false; // Bool - If stream is playing, it's given value is true
var readyStateInterval = null; // Function - setInterval() fn, see line 63 for more info
var stream = null; // Object - HTML5 Audio object
var streamURL = null; // String - URL of chosen stream
var station = null;
var actualCoverUrl = null; // String - URL of cover
var getMetadata = null; // Function - setInterval() fn, which calls getName(radio) for updating name of song
var isPlaying = false; // Bool - If stream is playing, it's given value is true
var songMetadata = null; // Array - Contains name of artist and name of song
var stream = null; // Object - HTML5 Audio object
var streamUrl = null; // String - URL of chosen radio stream
var station = null; // String - name of playing radio stream

// HTML5audio object
var html5audio = {
Expand All @@ -29,43 +29,37 @@ var html5audio = {
}

// TODO: Implement switching of quality of stream
// Choose a radio station
switch (radio) {
case "expres":
streamURL = "http://85.248.7.162:8000/96.mp3";
streamUrl = "http://85.248.7.162:8000/96.mp3";
break;
case "slovensko":
streamURL = "http://live.slovakradio.sk:8000/Slovensko_128.mp3";
streamUrl = "http://live.slovakradio.sk:8000/Slovensko_128.mp3";
break;
case "funradio":
streamURL = "http://stream.funradio.sk:8000/fun128.mp3";
streamUrl = "http://stream.funradio.sk:8000/fun128.mp3";
break;
case "europa2":
streamURL = "http://ice2.europa2.sk/fm-europa2sk-128";
streamUrl = "http://ice2.europa2.sk/fm-europa2sk-128";
break;
case "jemne":
streamURL = "http://93.184.69.143:8000/;jemnemelodie-high-mp3.mp3";
streamUrl = "http://93.184.69.143:8000/;jemnemelodie-high-mp3.mp3";
break;
}

// Init chosen radio station
stream = new Audio(streamURL);
stream = new Audio(streamUrl);
station = radio;

get_song(radio);
getName(radio);

getMetadata = setInterval(function() {
get_song(radio);
getName(radio);
}, 15000);

isPlaying = true;
stream.play();

readyStateInterval = setInterval(function(){
if (stream.readyState && stream.readyState <= 2) {
isPlaying = true;
document.getElementById('activityindicator').style.display = 'block';
}
}, 1000);

stream.addEventListener("waiting", function() {
isPlaying = false;
document.getElementById('activityindicator').style.display = 'block';
Expand All @@ -85,58 +79,57 @@ var html5audio = {
},
stop: function() {
document.getElementById('activityindicator').style.display = 'none';
clearInterval(readyStateInterval);
clearInterval(getMetadata);
isPlaying = false;
if (stream)
stream.pause();
stream = null;
actual_cover_url = null;
isPlaying = false;
actualCoverUrl = null;
}
};

// Parse stream track info
// TODO: Don't call ajaxCover() for Expres, Europa 2, Evropa 2 & Frekvence 1,
// TODO: Don't call getCover() for Expres, Europa 2, Evropa 2 & Frekvence 1,
// because cover of track is already included in json callback
var get_song = function(station) {
var getName = function(station) {
function reqListener () {
if (station !== "slovensko" && station !== "europa2") {
var artist_song = null;
var metadata = null;

if (station === "funradio") {
artist_song = [];
artist_song.push($(this.responseText).find("interpret")[0].textContent);
artist_song.push($(this.responseText).find("skladba")[0].textContent);
metadata = [];
metadata.push($(this.responseText).find("interpret")[0].textContent);
metadata.push($(this.responseText).find("skladba")[0].textContent);
}

if (station === "expres") {
var json = JSON.parse(this.responseText);
artist_song = [json.stream.artist, json.stream.song];
metadata = [json.stream.artist, json.stream.song];
}

if (station === "jemne") {
var html = document.implementation.createHTMLDocument('');
html.documentElement.innerHTML = this.responseText;
var text = html.getElementsByTagName("body")[0].innerText;
var info = text.replace(/[0-9]/g, "").replace(/,/g,"").split("-");
artist_song = [info[0], info[1]];
metadata = [info[0], info[1]];
}

if (artist_song[0] && !artist_song[1]) {
$("#artist").text(artist_song[0]);
if (metadata[0] && !metadata[1]) {
$("#artist").text(metadata[0]);
$("#song").text("");
} else if (!artist_song[0] && artist_song[1]) {
} else if (!metadata[0] && metadata[1]) {
$("#artist").text("");
$("#song").text(artist_song[1]);
$("#song").text(metadata[1]);
} else {
$("#artist").html(artist_song[0]).text();
$("#song").html(artist_song[1]).text();
$("#artist").html(metadata[0]).text();
$("#song").html(metadata[1]).text();
}

// LAST.FM API for getting cover of song
if (artist_song[0] && artist_song[1]) {
ajaxCover(artist_song[0], artist_song[1]);

if (metadata[0] && metadata[1]) {
getCover(metadata[0], metadata[1]);
songMetadata = [metadata[0], metadata[1]];
} else {
// If we don't know either name of artist or song, replace img with station img
if (!document.querySelector("#status > img")) {
Expand All @@ -149,16 +142,14 @@ var get_song = function(station) {
}
} else {
if (station === "slovensko") {
// Specific handling of Rádio Slovensko station
var html = document.implementation.createHTMLDocument('');
html.documentElement.innerHTML = this.responseText;
html.querySelector(".ro-slovensko > .playRadio > .overflow > strong").remove();
$("#artist").text("Rádio Slovensko");
$("#song").text(function() {
var text = html.querySelector(".ro-slovensko > .playRadio > .overflow").textContent;
return text.replace(/-/,"").replace(" ","");
});

$("#artist").text("Rádio Slovensko");
} else {
// TODO: The same for Evropa 2, Frekvence 1
$.get("http://rds.lagardere.cz/getRadio.php?station=okey", function(data) {
Expand All @@ -167,8 +158,10 @@ var get_song = function(station) {
var song = data.querySelector("songTitle").textContent.
replace(/^\s*\/\/<!\[CDATA\[([\s\S]*)\/\/\]\]>\s*\z/,"");

if (artist && song)
ajaxCover(artist, song);
if (artist && song) {
getCover(artist, song);
songMetadata = [metadata[0], metadata[1]];
}

$("#artist").text(function() {
return artist;
Expand All @@ -187,10 +180,9 @@ var get_song = function(station) {
} else {
document.querySelector("#status > img").src = "images/" + station +".png";
}

if ($(".marquee").text().length > 20)
$(".marquee").marquee();
}
if ($(".marquee").text().length > 20)
$(".marquee").marquee();
}

if (station) {
Expand All @@ -203,7 +195,7 @@ var get_song = function(station) {
}
}

function ajaxCover(artist, track) {
function getCover(artist, track) {
$.ajax({
type: "POST",
url: "http://ws.audioscrobbler.com/2.0/?",
Expand All @@ -219,7 +211,7 @@ function ajaxCover(artist, track) {

// We already got url of image -> it means that callback was successful,
// so add image to playing div
if (cover_url && actual_cover_url !== cover_url) {
if (cover_url && actualCoverUrl !== cover_url) {
if (document.querySelector("#status > img")) {
document.querySelector("#status > img").src = cover_url;

Expand All @@ -228,7 +220,7 @@ function ajaxCover(artist, track) {
$(this).insertBefore('#playing').addClass("radioimg");
});
}
actual_cover_url = cover_url;
actualCoverUrl = cover_url;
} else if (!cover_url) {
if (!document.querySelector("#status > img")) {
$('<img src="images/' + station + '.png">').load(function() {
Expand All @@ -237,7 +229,7 @@ function ajaxCover(artist, track) {
} else {
document.querySelector("#status > img").src = "images/" + station + ".png";
}
actual_cover_url = cover_url;
actualCoverUrl = cover_url;
} else {}
},
error: function(status) {
Expand Down
19 changes: 13 additions & 6 deletions www/scripts/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ $(".radio").click(function() {
});

// Fetch metadata for currently playing track
if (actualCoverUrl && isPlaying) {
$('<img src="' + actualCoverUrl + '">').load(function() {
$(this).insertBefore('#playing').addClass("radioimg");
});
if (isPlaying) {
if (actualCoverUrl) {
$('<img src="' + actualCoverUrl + '">').load(function() {
$(this).insertBefore('#playing').addClass("radioimg");
});
} else {
$('<img src="images/' + station + '.png">').load(function() {
$(this).insertBefore('#playing').addClass("radioimg");
});
}
}

if (station) {
getName(station);
if (songMetadata && isPlaying) {
$("#artist").text(songMetadata[0]);
$("#song").text(songMetadata[1]);
}
24 changes: 8 additions & 16 deletions www/scripts/html5audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ var expres = {
}

// Init variables
var actualCoverUrl = null;
var actualCoverUrl = null; // String - URL of cover
var getMetadata = null; // Function - setInterval() fn, which calls getName(radio) for updating name of song
var isPlaying = false; // Bool - If stream is playing, it's given value is true
var readyStateInterval = null; // Function - setInterval() fn, see line 63 for more info
var songMetadata = null; // Array - Contains name of artist and name of song
var stream = null; // Object - HTML5 Audio object
var streamUrl = null; // String - URL of chosen radio stream
var station = null; // String - name of playing radio stream
Expand Down Expand Up @@ -58,16 +58,8 @@ var html5audio = {
getName(radio);
}, 15000);

isPlaying = true;
stream.play();

readyStateInterval = setInterval(function(){
if (stream.readyState && stream.readyState <= 2) {
isPlaying = true;
document.getElementById('activityindicator').style.display = 'block';
}
}, 1000);

stream.addEventListener("waiting", function() {
isPlaying = false;
document.getElementById('activityindicator').style.display = 'block';
Expand All @@ -87,7 +79,6 @@ var html5audio = {
},
stop: function() {
document.getElementById('activityindicator').style.display = 'none';
clearInterval(readyStateInterval);
clearInterval(getMetadata);
if (stream)
stream.pause();
Expand Down Expand Up @@ -138,7 +129,7 @@ var getName = function(station) {
// LAST.FM API for getting cover of song
if (metadata[0] && metadata[1]) {
getCover(metadata[0], metadata[1]);

songMetadata = [metadata[0], metadata[1]];
} else {
// If we don't know either name of artist or song, replace img with station img
if (!document.querySelector("#status > img")) {
Expand Down Expand Up @@ -167,8 +158,10 @@ var getName = function(station) {
var song = data.querySelector("songTitle").textContent.
replace(/^\s*\/\/<!\[CDATA\[([\s\S]*)\/\/\]\]>\s*\z/,"");

if (artist && song)
if (artist && song) {
getCover(artist, song);
songMetadata = [metadata[0], metadata[1]];
}

$("#artist").text(function() {
return artist;
Expand All @@ -187,10 +180,9 @@ var getName = function(station) {
} else {
document.querySelector("#status > img").src = "images/" + station +".png";
}

if ($(".marquee").text().length > 20)
$(".marquee").marquee();
}
if ($(".marquee").text().length > 20)
$(".marquee").marquee();
}

if (station) {
Expand Down

0 comments on commit 684c046

Please sign in to comment.