Sound Effects for Game

676 views
Skip to first unread message

Daniel Tse

unread,
Jul 2, 2010, 3:25:43 PM7/2/10
to phonegap
Hi,

I'm currently building a simple game and want to add some sound
effects. Is the media object the best one to use?

I tried the audio object but I can't seem to layer multiple sound
effects on top of each other (e.g. I want to play a sound after the
user has completed a task but also want to have background music going
on as well).

What's been everyone's experience with layering/mixing multiple mp3s
or audio files (wavs? aiffs?)

Thanks!
-Dan

Jesse MacFadyen

unread,
Jul 2, 2010, 4:28:53 PM7/2/10
to Daniel Tse, phonegap
For iPhone the phonegap media object should suit your needs, just
preload all your sounds. I made a simple drumpad app with 16 sound/
pads and it is responsive, and overlapping is not an issue.
Avoid using mp3 or other compressed formats because there is only one
hardware decoder on the device so performance will suffer when mixing
especially.

Hope that helps.
Jesse

Sent from my iPhone

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

Daniel Tse

unread,
Jul 2, 2010, 7:11:12 PM7/2/10
to Jesse MacFadyen, phonegap
Thanks Jesse!

So should the sound files be .wav or .aiff?

-Dan

--
e: D...@DanielTse.com
t: http://Twitter.com/danprime
w: http://DanielTse.com
--------

On 2010-07-02, at 2:28 PM, Jesse MacFadyen <jesse.m...@nitobi.com> wrote:

> For iPhone the phonegap media object should suit your needs, just preload all your sounds. I made a simple drumpad app with 16 sound/pads and it is responsive, and overlapping is not an issue.

Jesse MacFadyen

unread,
Jul 3, 2010, 1:39:35 AM7/3/10
to Daniel Tse, phonegap
Either is fine.

Sent from my iPhone

Björn Nilsson

unread,
Jul 3, 2010, 5:18:49 AM7/3/10
to phonegap
Hi Jesse, is the code for your drum pad app available to view? Reason
is I am curious how you accomplish overlapping sounds, thinking about
this bug I have reported:

http://phonegap.lighthouseapp.com/projects/20116-iphone/tickets/60

/Björn

On Jul 3, 7:39 am, Jesse MacFadyen <jesse.macfad...@nitobi.com> wrote:
> Either is fine.
>
> Sent from my iPhone
>
> On 2010-07-02, at 4:11 PM, Daniel Tse <danpr...@gmail.com> wrote:
>
> > Thanks Jesse!
>
> > So should the sound files be .wav or .aiff?
>
> > -Dan
>
> > --
> > e: D...@DanielTse.com
> > t:http://Twitter.com/danprime
> > w:http://DanielTse.com
> > --------
>
> > On 2010-07-02, at 2:28 PM, Jesse MacFadyen  
> > <jesse.macfad...@nitobi.com> wrote:
>
> >> For iPhone the phonegap media object should suit your needs, just  
> >> preload all your sounds. I made a simple drumpad app with 16 sound/
> >> pads and it is responsive, and overlapping is not an issue.
> >> Avoid using mp3 or other compressed formats because there is only  
> >> one hardware decoder on the device so performance will suffer when  
> >> mixing especially.
>
> >> Hope that helps.
> >> Jesse
>
> >> Sent from my iPhone
>

Jesse MacFadyen

unread,
Jul 3, 2010, 5:45:42 AM7/3/10
to Björn Nilsson, phonegap
function onBodyLoad()
{
document.addEventListener("deviceready",onDeviceReady,false);
}
var sounds = [];
/* When this function is called, PhoneGap has been initialized and is ready to roll */
function onDeviceReady()
{
var btns = document.getElementById("btns");
var htmStr = "";
var template = "<div class='btn' ontouchstart='playsound(event,SOUNDID)'>SOUNDID</div>";
var name = "Basic/Sound";
for(var n = 0; n < 16; n++)
{
sounds.push(new Media(name + n + ".caf"));
htmStr += template.replace(/SOUNDID/g,n);
}
btns.innerHTML = htmStr;

}
function playsound(evt,num)
{
sounds[num].play();
}

2010/7/3 Björn Nilsson <bni...@gmail.com>



--
--
Jesse MacFadyen
{
 blog:'blogs.nitobi.com/jesse',
 email:'jesse.m...@nitobi.com',
 company:
 {
   name:'Nitobi Software',
   site:'www.nitobi.com',
   phone:
   {
     office:'+1 (604) 685-9287',
     tollFree:'1-866-632-2777'
    }
 }
}

Björn Nilsson

unread,
Jul 3, 2010, 11:00:39 AM7/3/10
to phonegap
Thanks,

I tried the code, and while tapping different drums, the sound
overlaps, but it does not when tapping the same "drum" over and over
again. Infact, it cuts the previous sound short and starts the sound
over again from the beginning.

Also If you have an animation going (using setInterval) the animation
will briefly halt, causing non-smooth animation.

PhoneGap 0.8.0 used AudioServicesPlaySystemSound to play sounds, and
none of these problems is present in 0.8.0. However the new
Implementation using AVAudioPlayer clearly doesnt work for arcade game
use cases.

Ideally I think you should be able to just do "new Media('sound/
laser.wav').play();" to play sounds. This is what is closest to how
you would do it in a native JavaScript implemenation of <audio> and
Audio function. "new Audio('sound/laser.wav').play();" works perfect
in Firefox and chrome.

/Björn

On Jul 3, 11:45 am, Jesse MacFadyen <jesse.macfad...@nitobi.com>
wrote:
>  function onBodyLoad()
> {
> document.addEventListener("deviceready",onDeviceReady,false);}
>
>  var sounds = [];
>  /* When this function is called, PhoneGap has been initialized and is ready
> to roll */
> function onDeviceReady()
> {
> var btns = document.getElementById("btns");
> var htmStr = "";
> var template = "<div class='btn'
> ontouchstart='playsound(event,SOUNDID)'>SOUNDID</div>";
> var name = "Basic/Sound";
> for(var n = 0; n < 16; n++)
> {
> sounds.push(new Media(name + n + ".caf"));
> htmStr += template.replace(/SOUNDID/g,n);}
>
>  btns.innerHTML = htmStr;
>
> }
>
>  function playsound(evt,num)
> {
> sounds[num].play();
>
> }
>
> 2010/7/3 Björn Nilsson <bni....@gmail.com>
> > > >>> phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > > >>> For more options, visit this group at
> > > >>>http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > > >>> For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "phonegap" group.
> > To post to this group, send email to phon...@googlegroups.com
> > To unsubscribe from this group, send email to
> > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> --
> --
> Jesse MacFadyen
> {
>  blog:'blogs.nitobi.com/jesse',
>  email:'jesse.macfad...@nitobi.com <email%3A%27jesse.macfad...@nitobi.com>',
Reply all
Reply to author
Forward
0 new messages