473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

(Mobile) Play sound when accelerometer hits certain coords

1
Hello everyone,

So I'm working on a university project where we're making innovative musical instruments. My idea is to have a device that you would attach to any playground toy (seesaw, swing etc.) and it would play a sound on each swing.

So after playing around with Flash, I found this preset cellphone accelerometer app, which is perfect for my project. It just misses the play sound component. Which brings me to my questions.

So the code for the app goes as follows:

Expand|Select|Wrap|Line Numbers
  1. import flash.events.Event;
  2.  
  3. var accelX:Number;
  4. var accelY:Number;
  5.  
  6. var fl_Accelerometer:Accelerometer = new Accelerometer();
  7. fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);
  8. function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void
  9. {
  10.     accelX = event.accelerationX;
  11.     accelY = event.accelerationY;
  12. }
  13.  
  14. ball.addEventListener(Event.ENTER_FRAME, moveBall);
  15. function moveBall(evt:Event){
  16.     ball.x -= accelX*30;
  17.     ball.y += accelY*30;
  18.  
  19.     if(ball.x > (480-ball.width/2)){
  20.         ball.x = 480-ball.width/2;
  21.     }
  22.     if(ball.x < (0+ball.width/2)){
  23.         ball.x = 0+ball.width/2;
  24.     }
  25.     if(ball.y > (800-ball.width/2)){
  26.        ball.y = 800-ball.width/2;
  27.     }
  28.     if(ball.y < (0+ball.width/2)){
  29.         ball.y = 0+ball.width/2;
  30.     }
  31. }
  32.  
I changed the form of "ball" to a long line. I want it to play a sound each time that line hits the middle of the screen. So we're only using the x-coordinates (I think).

I figured a simple if-function should do the trick. Which I think would look something like this:

Expand|Select|Wrap|Line Numbers
  1. if(ball.x = 0)
  2. {
  3. sound.play();
  4. }
  5.  
So I have several question:
- Is that code correct?
- Where do I put the code?
- Is it possible to have a delay? So it doesn't spam-play the sound when I put the phone flat on the table where the "ball" is constantly hitting that middle line.

I'd also like to be able to change the sound. But that's optional, I'll skip it if it's too complicated.

I'd appreciate it if anyone would wanna help me.
Dec 4 '13 #1
0 5861

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike Minor | last post by:
I have a request from a client to play a sound when a specific <input type=text> field has been updated with new data from a server side script. They have not specified the sound ( yet ), so I'm...
2
by: Alex K. | last post by:
I need to play particular sound when user presses a button. I found PlaySound function in the Platform SDK, it works fine, but it can only play external wav file. I'd like to load my sound file...
1
by: Lam | last post by:
how can I play sound file in a .aspx page written in C#? I try to use the code like the following. But whenI call the play function play("sound.wav", this.SND_ASYNC) my computer give out "be"...
10
by: CM | last post by:
Hi, I used to use the following code to play sound in VB6: Public Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, ByVal uFlags As...
16
by: Chris Ianson | last post by:
Hi, is there a way to play a small sound snippet when a user clicks a certain hyperlink hotspot on an image? Thanks in advance.
3
by: illmagination | last post by:
Hi, I recently received a task to have a .wav file play only once when home page is 1st loaded and only play again when the user re-visits the page by opening a new browser. I am using...
1
by: jamieboy | last post by:
I am trying to find out a possible resolution for my and my colleagues' common issue. we have a tool at office which we monitor the transport facilities. whenever there is an update this tool will...
1
by: tinmaung | last post by:
hi please help me i want to know how to play sound in java application i don't mean java applet if you can, please send me how to write coding to play sound in application.
3
by: jamessarvs | last post by:
Windows Media Player cannot play the file because there is a problem with your sound device. There might not be a sound device installed on your computer, it might be in use by another program, or it...
1
by: nicolecastel | last post by:
Hello I want to play sound in three external sound cards at the same time,I mean when I click in a button I can hear sound from three speakers which are related to my three sound cards. I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.