How can I play a sound using Cocoa-Applescript in Xcode?
I need to play a sound file called "click.aiff" whenever a button is clicked, that is bundled with my application. How can I do this without using QuickTimePlayer?
I need to play a sound file called "click.aiff" whenever a button is clicked, that is bundled with my application. How can I do this without using QuickTimePlayer?
Check out the NSSound Class Reference. You can add a property for the sound and initialize it with something like:
set mySound to current application's NSSound's soundNamed_("your sound name")
The system will check for an installed sound file in the Sounds folders, and then in your application bundle if it is not found (note that the extension is not used). You can then play it by using the play method:
mySound's play()
Also note that if you are going to use one of the pre-installed sounds, you can assign a sound for the button in the Attributes Inspector in the Interface Editor.
Check out the NSSound Class Reference. You can add a property for the sound and initialize it with something like:
set mySound to current application's NSSound's soundNamed_("your sound name")
The system will check for an installed sound file in the Sounds folders, and then in your application bundle if it is not found (note that the extension is not used). You can then play it by using the play method:
mySound's play()
Also note that if you are going to use one of the pre-installed sounds, you can assign a sound for the button in the Attributes Inspector in the Interface Editor.
If you could use one of the built-in sounds it would be easier, just
on playSound_(sender)
tellclass"NSSound"of current application
set soundInstance to its soundNamed_("Pop")
end
soundInstance's setVolume_(1.0)
soundInstance's |play|()
end
Just put the sound name you want to use in place of Pop
If you have to use the sound file then first you need to know where it is. The easiest is to have it as part of the bundle then you can use the pathToResource:ofType: method that finds files of a given type in the Resourse folder of the applications bundle.
So
tell class "NSBundle" of current application
tell its mainBundle
set soundFilePath to its pathForResource_ofType_("click", "aiff")
end tell
end tell
gets the sound file name in soundFilePath
then to play it first load it
tell class "NSSound" of current application
set soundInstance to its alloc()'s initWithContentsOfFile_byReference_(soundFilePath, true)
end tell
then play it as above
soundInstance's setVolume_(1.0)
soundInstance's |play|()
I beleive that should do it.
regards
red_menace
The system will check for an installed sound file in the Sounds folders, and then in your application bundle if it is not found (note that the extension is not used).
Never realized the system would check in the application's bundle for a non-standard sound file name, That sure does simplify things!
thanks
Yep - it looks like it only searches the /Library/Sounds and ~/Library/Sounds folders, but using the soundNamed: method replaces the statement(s) to get the resource in the bundle. You can still use paths or URLs if you want, though.
I put both lines of code in one handler:
on PlayClick_()
set clicksound to current application's NSSound's soundNamed_("click")
clicksound's play()
end PlayClick_
But then I get the error:
unrecognized function play.
What am I doing wrong? Thanks.
clicksound is not being set, most likely it is not finding the click.aiff file.
As a test change "click" to "Pop" and see if it works. Pop is one of the default sounds. If it works then you know the problem is not being able to find your sound file.
In cases like this testiog to make sure the class was set by doing somehting like
if clicksound is equal to missing value then
display alert "missing"
else
clicksound's play()
end
is a good habit to get into.
Message was edited by: Frank Caggiano
You might want to check if the sound is actually being found. Also, what kind of sound file is it? The documentation specifies that the sound file needs to be .wav or .aiff (with two 'f's).
So, strangely enough, using ".aiff" doesn't work, but ".wav" does. Anyways thank you both for the help, I spent 2 hours searching it on Google without any success. If you're wondering what this was all for, you can view my app here. ;3
How can I play a sound using Cocoa-Applescript in Xcode?