tonybabb
01-02-2014, 02:20 PM
Hello again John,
Happy New Year !!
Sorry this is such a long post but you may recall that several months ago you kindly assisted me to create a talking phrasebook as a smartphone app, when a user tapped a phrase on the screen a sound clip played for a few seconds and the background color changed. We got it working on my PC but when I converted it to an app using Intel XDK (New) and ran it on my Samsung Galaxy S4 smartphone it had the problem that after playing some (20+) sound clips the sounds just didn't play, no error message or anything.
To cut a long story short I have recreated the app using the Intel App Framework v2 in place of jQuery Mobile and the Cordova v2.9 js library in place of jPlayer and it now will play sounds as many times as I want - the underlying problem, I believe, was that the Android OS has finite audio resources and when Android plays a sound file it allocates some resources and does not release the resources when it has finished playing the sound file. It then allocates additional resources each time a sound file is played, eventually it runs out of resources and stops playing. The app programmer has to explicitly release the resources when the sound file has stopped playing. I also had some other issues with jQuery Mobile relating to speed and reliability when positioning the page header/ footers on my smartphone and these also appear to have been fixed with the new version of the app.
So, now that I have the sound working I'm reconstructing the look/ feel/ functionality I previously had with jQuery Mobile/ jPlayer. Specifically I'm trying to merge the code I created to play sound files using cordova with the code you created to play sounds using jPlayer and also to change the background color. I spent some time trying to do this but was unable to follow your js - still learning js but I am getting better.
I would really appreciate your assistance to merge these two versions. Below is the code I created and following that the code you created. Unfortunately I can't show you this on a website because the App Framework doesn't support PC type browsers very well and has device specific drivers.
CURRENT VERSION USING APP FRAMEWORK 2 / CORDOVA 2.9
I currently use a button to play the sound file, of course I'd like to replace this so the user taps anywhere on a phrase to play the associated sound
<input type="button" onClick="soundclip('ageCuantos2F.ogg')" value=" Play Cuantos Anos "> ;
and here's the soundclip function it calls, note that it needs to get the path from the web root and prepend that to the soundfile name. Also, when creating the Media Object that is where I specify the stop/ release functionality to be completed when the audio file stops onSuccess or onError. The cordova API is documented here http://cordova.apache.org/docs/en/2.9.0/cordova_media_media.md.html#Media
Here's the js function called when the button is pressed
<script type="text/javascript">
function soundclip(filename) {
var my_media = new Media(getWebRoot() + "/" + filename,
function() { my_media.stop(); my_media.release();},
function() { my_media.stop(); my_media.release(); console.log ("Audio play error - " + filename)});
my_media.play() ;
} ; // End soundclip
</script>
<script type="text/javascript">
function getWebRoot() {
"use strict" ;
var path = window.location.href ;
path = path.substring( 0, path.lastIndexOf( '/' ) ) ;
return path ;
}
</script>
jQUERY MOBILE/ jPLAYER VERSION
Here's the html
<div class="english"> <!-- Start English -->
How old are you?
</div> <!-- End English -->
<div class="wrapper">
<a class="formal spanish" data-trigger="howoldf" href="ageCuantos2F.mp3">F: Cuántos años tiene?</a>
<a class="formal trigger" id="howoldf" href="#">F: Cuántos años tiene?</a>
<div class="spacer"></div>
<a class="informal spanish" data-trigger="howoldi" href="ageCuantos2I.mp3">I: Cuántos años tienes?</a>
<a class="informal trigger" id="howoldi" href="#">I: Cuántos años tienes?</a>
</div> <!-- End .wrapper -->
and here's the js you created that I just couldn't understand - sorry.
<script type="text/javascript">
jQuery(function($){
var highlight = 'yellow', origcolor = 'transparent', istouch = !!('ontouchstart' in window) || !!('ontouchstart' in document.documentElement) || !!window.ontouchstart || (!!window.Touch && !!window.Touch.length) || !!window.onmsgesturechange || (window.DocumentTouch && window.document instanceof window.DocumentTouch);
var $s = $('.spanish').each(function(i, snd){
$(snd).jPlayer({
ready: function() {
$(snd).jPlayer("setMedia", {
mp3: snd.href,
ogg: snd.href.replace(/\.mp3$/, '.ogg') });
var playstop = function (e) { e && e.preventDefault();
var $this = $(this);
if(!$this.data('play')){
$this.stop(true, true).css({backgroundColor: highlight});
var $playing = $('a[data-playing="true"]');
if($playing.length){
if(istouch){
playstop.apply($playing.get(0));
} else {
$playing.trigger('click');
}
}
} else {
$this.stop(true, true).animate({backgroundColor: origcolor}, 1000);
}
$(snd).jPlayer($this.data('play')? "stop" : "play");
$this.data('play', !$this.data('play'));
$this.attr('data-playing', $this.data('play'));
};
if(istouch){
var dt = this.getAttribute('data-trigger'), $dt = $('#' + dt);
dt = document.getElementById(dt);
dt.addEventListener('touchstart', function(e){$dt.data({tstart: new Date().getTime(), tx: e.pageX, ty: e.pageY});}, false);
dt.addEventListener('touchend', function(e){
var ds = $dt.data('tstart');
if(!ds){return;}
var vx = Math.abs(e.pageX - $dt.data('tx')), vy = Math.abs(e.pageY - $dt.data('ty'));
if(vx < 10 && vy < 10 && new Date().getTime() - ds < 400){
playstop.apply(this, [e]);
$dt.data({tstart: null, tx: null, ty: null});
}
}, false);
} else {
$('#' + this.getAttribute('data-trigger')).click(playstop);
}
},
ended: function(){
$('#' + this.getAttribute('data-trigger')).attr('data-playing', 'false').data('play', false).stop(true, true).animate({backgroundColor: origcolor}, 1000);
},
swfPath: "./"
});
});
});
</script>
I would really appreciate your assistance with this, it's been a long road but I can finally see a light at the end of the tunnel.
Regards,
Tony
Happy New Year !!
Sorry this is such a long post but you may recall that several months ago you kindly assisted me to create a talking phrasebook as a smartphone app, when a user tapped a phrase on the screen a sound clip played for a few seconds and the background color changed. We got it working on my PC but when I converted it to an app using Intel XDK (New) and ran it on my Samsung Galaxy S4 smartphone it had the problem that after playing some (20+) sound clips the sounds just didn't play, no error message or anything.
To cut a long story short I have recreated the app using the Intel App Framework v2 in place of jQuery Mobile and the Cordova v2.9 js library in place of jPlayer and it now will play sounds as many times as I want - the underlying problem, I believe, was that the Android OS has finite audio resources and when Android plays a sound file it allocates some resources and does not release the resources when it has finished playing the sound file. It then allocates additional resources each time a sound file is played, eventually it runs out of resources and stops playing. The app programmer has to explicitly release the resources when the sound file has stopped playing. I also had some other issues with jQuery Mobile relating to speed and reliability when positioning the page header/ footers on my smartphone and these also appear to have been fixed with the new version of the app.
So, now that I have the sound working I'm reconstructing the look/ feel/ functionality I previously had with jQuery Mobile/ jPlayer. Specifically I'm trying to merge the code I created to play sound files using cordova with the code you created to play sounds using jPlayer and also to change the background color. I spent some time trying to do this but was unable to follow your js - still learning js but I am getting better.
I would really appreciate your assistance to merge these two versions. Below is the code I created and following that the code you created. Unfortunately I can't show you this on a website because the App Framework doesn't support PC type browsers very well and has device specific drivers.
CURRENT VERSION USING APP FRAMEWORK 2 / CORDOVA 2.9
I currently use a button to play the sound file, of course I'd like to replace this so the user taps anywhere on a phrase to play the associated sound
<input type="button" onClick="soundclip('ageCuantos2F.ogg')" value=" Play Cuantos Anos "> ;
and here's the soundclip function it calls, note that it needs to get the path from the web root and prepend that to the soundfile name. Also, when creating the Media Object that is where I specify the stop/ release functionality to be completed when the audio file stops onSuccess or onError. The cordova API is documented here http://cordova.apache.org/docs/en/2.9.0/cordova_media_media.md.html#Media
Here's the js function called when the button is pressed
<script type="text/javascript">
function soundclip(filename) {
var my_media = new Media(getWebRoot() + "/" + filename,
function() { my_media.stop(); my_media.release();},
function() { my_media.stop(); my_media.release(); console.log ("Audio play error - " + filename)});
my_media.play() ;
} ; // End soundclip
</script>
<script type="text/javascript">
function getWebRoot() {
"use strict" ;
var path = window.location.href ;
path = path.substring( 0, path.lastIndexOf( '/' ) ) ;
return path ;
}
</script>
jQUERY MOBILE/ jPLAYER VERSION
Here's the html
<div class="english"> <!-- Start English -->
How old are you?
</div> <!-- End English -->
<div class="wrapper">
<a class="formal spanish" data-trigger="howoldf" href="ageCuantos2F.mp3">F: Cuántos años tiene?</a>
<a class="formal trigger" id="howoldf" href="#">F: Cuántos años tiene?</a>
<div class="spacer"></div>
<a class="informal spanish" data-trigger="howoldi" href="ageCuantos2I.mp3">I: Cuántos años tienes?</a>
<a class="informal trigger" id="howoldi" href="#">I: Cuántos años tienes?</a>
</div> <!-- End .wrapper -->
and here's the js you created that I just couldn't understand - sorry.
<script type="text/javascript">
jQuery(function($){
var highlight = 'yellow', origcolor = 'transparent', istouch = !!('ontouchstart' in window) || !!('ontouchstart' in document.documentElement) || !!window.ontouchstart || (!!window.Touch && !!window.Touch.length) || !!window.onmsgesturechange || (window.DocumentTouch && window.document instanceof window.DocumentTouch);
var $s = $('.spanish').each(function(i, snd){
$(snd).jPlayer({
ready: function() {
$(snd).jPlayer("setMedia", {
mp3: snd.href,
ogg: snd.href.replace(/\.mp3$/, '.ogg') });
var playstop = function (e) { e && e.preventDefault();
var $this = $(this);
if(!$this.data('play')){
$this.stop(true, true).css({backgroundColor: highlight});
var $playing = $('a[data-playing="true"]');
if($playing.length){
if(istouch){
playstop.apply($playing.get(0));
} else {
$playing.trigger('click');
}
}
} else {
$this.stop(true, true).animate({backgroundColor: origcolor}, 1000);
}
$(snd).jPlayer($this.data('play')? "stop" : "play");
$this.data('play', !$this.data('play'));
$this.attr('data-playing', $this.data('play'));
};
if(istouch){
var dt = this.getAttribute('data-trigger'), $dt = $('#' + dt);
dt = document.getElementById(dt);
dt.addEventListener('touchstart', function(e){$dt.data({tstart: new Date().getTime(), tx: e.pageX, ty: e.pageY});}, false);
dt.addEventListener('touchend', function(e){
var ds = $dt.data('tstart');
if(!ds){return;}
var vx = Math.abs(e.pageX - $dt.data('tx')), vy = Math.abs(e.pageY - $dt.data('ty'));
if(vx < 10 && vy < 10 && new Date().getTime() - ds < 400){
playstop.apply(this, [e]);
$dt.data({tstart: null, tx: null, ty: null});
}
}, false);
} else {
$('#' + this.getAttribute('data-trigger')).click(playstop);
}
},
ended: function(){
$('#' + this.getAttribute('data-trigger')).attr('data-playing', 'false').data('play', false).stop(true, true).animate({backgroundColor: origcolor}, 1000);
},
swfPath: "./"
});
});
});
</script>
I would really appreciate your assistance with this, it's been a long road but I can finally see a light at the end of the tunnel.
Regards,
Tony