touch events using javascript

4,379 views
Skip to first unread message

Sudhakar

unread,
Jan 6, 2011, 8:18:45 AM1/6/11
to iPhoneWebDev
i would like to use an app when i tap on a button when this event
occurs only i would like an audio file to play which is only for 2-3
seconds like a beep sound

using html and javascript only and not in xcode how can i use an event
so that when i tap on a button i can call a javascript function which
will play the audio file for few seconds

Peter Rust

unread,
Jan 6, 2011, 12:27:32 PM1/6/11
to iphone...@googlegroups.com
Sudhakar,

I believe a "tap" event in a Javascript page viewed in Safari fires normal
"click" handlers, so you could use the built-in attachEvent() function in
javascript or set an onclick="" attribute in HTML -- or you could use a
mini-framework like Zepto (https://github.com/madrobby/zepto#readme).

As far as playing a sound, there are a couple ways to play a sound natively
(http://www.phon.ucl.ac.uk/home/mark/audio/play.htm), but most of these
techniques will only work for uncompressed WAV files (and of course the IE &
Java approaches won't work on iOS). The usual approach is to use a free
embeddable flash-based player
(http://designshack.co.uk/articles/html/four-quick-and-easy-ways-to-embed-mp
3-files-into-your-site), but this won't work on iOS either.

-- peter

--
You received this message because you are subscribed to the Google Groups
"iPhoneWebDev" group.
To post to this group, send email to iphone...@googlegroups.com.
To unsubscribe from this group, send email to
iphonewebdev...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/iphonewebdev?hl=en.

Giacomo Balli

unread,
Jan 6, 2011, 12:19:18 PM1/6/11
to iPhoneWebDev
add it to the onclick function.
otherwise, if you want a full solution, intercept all clicks and edit
the behavior.

Sudhakar

unread,
Jan 17, 2011, 5:42:00 AM1/17/11
to iPhoneWebDev
i have used xhtml method and html5 method in both cases when i tap on
the link the audio file does not play in the background instead the
audio file plays in an audio app and after playing the sound i have to
click on Done button and then the html page appears

following are the different methods i have used

1
<a href="#" onclick="playsound1('sound1')">Play Audio</a>

<embed src="audio.mp3" autostart="true" id="sound1" width="0"
height="0" hidden="true">

function playsound1(soundobj){
var thissound1=document.getElementById(soundobj);
thissound1.Play();
}

2
<a href="#" onclick="playsound2()">Play Audio</a>

<embed src="audio.mp3" autostart="true" id="sound2" width="0"
height="0" hidden="true">

function playsound2(){
var thissound2=document.getElementById("sound2");
thissound2.Play();
}

i have used html5 audio tag with zepto framework https://github.com/madrobby/zepto#readme

<div id="touch_test"><a href="#" id="audio">play audio</a></div>
<script>
$("#audio").tap(function(){
$("#touch_test").append('<audio src="success-mp3.mp3"
autoplay="autoplay"></audio> ');
});
</script>

what i need is when a link is clicked i want a background sound to
play immediately and this sound file is only few seconds and this
sound should play inside the html page and not open in a separate
audio app i want the html page to stay the way it is.

i want this feature only using html, css and javascript not in xcode

also i am using iphone 2g does 2g have limitations regarding the code
i am using

Rémi Grumeau

unread,
Jan 17, 2011, 6:13:45 AM1/17/11
to iphone...@googlegroups.com
Playing a video or music on an iPhone can only be done via QuickTime
afaik.
I can be done on an iPad (at least H264 videos for sure)

R.

Le 17 janv. 11 à 11:42, Sudhakar a écrit :

Stan Wiechers

unread,
Jan 17, 2011, 8:05:42 AM1/17/11
to iphone...@googlegroups.com
a=new Audio(URL);
a.load();
a.play();

That does on iOS and even the new blackberry 6.


Sent from my iPhone

Rémi Grumeau

unread,
Jan 17, 2011, 8:51:54 AM1/17/11
to iphone...@googlegroups.com
on Safari Desktop it works, but not in iPhone 3.x neither 4.x.
Can't test on real iPad btw

Am i doing something wrong?
http://we-are-gurus.com/labs/audio.html

R.

Le 17 janv. 11 à 14:05, Stan Wiechers a écrit :

Stan Wiechers

unread,
Jan 17, 2011, 8:55:39 AM1/17/11
to iphone...@googlegroups.com
This 100% works on my iPad, iPhone, Blackerry 9760.

Sent from my iPhone

Mogens Beltoft

unread,
Jan 17, 2011, 3:06:58 PM1/17/11
to iphone...@googlegroups.com
I can make it work on iOS 4.2.1 by making a div, adding an onClick
handler and calling your code from that handler.

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
/* Play music in the background */
function play()
{
var a=new Audio("Apoptygma Berzerk - Kathy's Song (VNV Nation
Remix).mp3");
a.load();
a.play();
}
</script>
</head>
<body>
<div onClick='play();' style='width:50px; height:50px;
background-color:red;'></div>
</body>
</html>

/Mogens

On 17-01-2011 14:05, Stan Wiechers wrote:
> a=new Audio(URL);
> a.load();
> a.play();
>
> That does on iOS and even the new blackberry 6.
>
>
> Sent from my iPhone
>

> On Jan 17, 2011, at 6:15 AM, "R�mi Grumeau"<remi.g...@gmail.com> wrote:
>
>> Playing a video or music on an iPhone can only be done via QuickTime afaik.
>> I can be done on an iPad (at least H264 videos for sure)
>>
>> R.
>>

>> Le 17 janv. 11 � 11:42, Sudhakar a �crit :

Rémi Grumeau

unread,
Jan 17, 2011, 3:32:44 PM1/17/11
to iphone...@googlegroups.com
WOW !
Works here too (on iPhone 4)

Le 17 janv. 11 à 21:06, Mogens Beltoft a écrit :

>> On Jan 17, 2011, at 6:15 AM, "Rémi

>> Grumeau"<remi.g...@gmail.com> wrote:
>>
>>> Playing a video or music on an iPhone can only be done via
>>> QuickTime afaik.
>>> I can be done on an iPad (at least H264 videos for sure)
>>>
>>> R.
>>>

>>> Le 17 janv. 11 à 11:42, Sudhakar a écrit :
>>>

Stan Wiechers

unread,
Jan 17, 2011, 3:33:45 PM1/17/11
to iphone...@googlegroups.com
I think they must have recently been adding that functionality. Its awesome!!

On Mon, Jan 17, 2011 at 3:32 PM, Rémi Grumeau <remi.g...@gmail.com> wrote:
WOW !
Works here too (on iPhone 4)

Le 17 janv. 11 à 21:06, Mogens Beltoft a écrit :
I can make it work on iOS 4.2.1 by making a div, adding an onClick handler and calling your code from that handler.

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
  /* Play music in the background */
  function play()
  {
      var a=new Audio("Apoptygma Berzerk - Kathy's Song (VNV Nation Remix).mp3");
      a.load();
      a.play();
  }
</script>
</head>
<body>
<div onClick='play();' style='width:50px; height:50px; background-color:red;'></div>



--
"Local color. Soak it up"
Virginia Vidaura

http://www.merkwelt.com/people/stan/

Jorge Chamorro

unread,
Jan 17, 2011, 3:37:41 PM1/17/11
to iphone...@googlegroups.com
audio
Embeds audio into a webpage.
Syntax
<audio src="url" autoplay="autoplay" <!-- Boolean attribute. Omit to prevent autoplay. --> start="00:00:00.00" loopstart="00:00:00.07" <!-- 7 seconds --> loopend="00:00:00.19" end="00:00:00.27" playcount="4" <!-- play 4x --> controls="true" >
Discussion
The audio element may contain fallback content for browers that do not support this element. Any content enclosed within the audio element is ignored by browsers that support the audio element (but it must be valid HTML).
The audio element supports inclusion of source elements to provide multiple versions of an audio clip encoded with different codecs, at different bit rates, and so on. These source elements must be the first elements inside the audio element before any fallback content. See “source” (page 42) for more information.
Availability
Available in Safari 3.1 and later. Available in iPhone OS 3.0 and later.

http://google.com/search?q=safari+html+filetype:pdf+site:developer.apple.com
--
Jorge.

Mogens Beltoft

unread,
Jan 17, 2011, 4:21:26 PM1/17/11
to iphone...@googlegroups.com
Is there a way to style the controls? I'm thinking of length. The
controls are very small in my test code.

If I leave out the controls attribute the controls are not visible, but
the audio element takes up the same amount of space whether the controls
are there or not.

/Mogens

On 17-01-2011 21:37, Jorge Chamorro wrote:
> audio
> Embeds audio into a webpage.
> Syntax
> <audio src="url" autoplay="autoplay"<!-- Boolean attribute. Omit to prevent autoplay. --> start="00:00:00.00" loopstart="00:00:00.07"<!-- 7 seconds --> loopend="00:00:00.19" end="00:00:00.27" playcount="4"<!-- play 4x --> controls="true">
> Discussion
> The audio element may contain fallback content for browers that do not support this element. Any content enclosed within the audio element is ignored by browsers that support the audio element (but it must be valid HTML).

> The audio element supports inclusion of source elements to provide multiple versions of an audio clip encoded with different codecs, at different bit rates, and so on. These source elements must be the first elements inside the audio element before any fallback content. See �source� (page 42) for more information.

RobG

unread,
Jan 17, 2011, 5:56:37 PM1/17/11
to iPhoneWebDev


On Jan 18, 6:06 am, Mogens Beltoft <mog...@beltoft.dk> wrote:
> I can make it work on iOS 4.2.1 by making a div, adding an onClick
> handler

Listener.

--
Rob
Reply all
Reply to author
Forward
0 new messages