Looping Sound in Cocoa
Does anyone have experience playing sounds in Cocoa? Specifically, I'm trying to make the sound loop, but I'm having no luck. Here is my current strategy:
1. Declare and create the sound. Play it. No problem.
NSSound* mySound;
mySound = [NSSound soundNamed:@"SomeSound"];
[mySound play];
2. Check to see if the sound is still playing. If not, play it again (Sam).
if (![mySound isPlaying]){
[mySound play];
}
This actually sort of works except that there is a short pause after the sound is finished and before it starts playing again. My sound is a drum track, so obviously this is not acceptable.
CAN ANYBODY HELP ME!?
Thanks.
1. Declare and create the sound. Play it. No problem.
NSSound* mySound;
mySound = [NSSound soundNamed:@"SomeSound"];
[mySound play];
2. Check to see if the sound is still playing. If not, play it again (Sam).
if (![mySound isPlaying]){
[mySound play];
}
This actually sort of works except that there is a short pause after the sound is finished and before it starts playing again. My sound is a drum track, so obviously this is not acceptable.
CAN ANYBODY HELP ME!?
Thanks.
How often do you check if your sound is playing? You could setup a timer to fire at 0.001 to check it or just check in drawRect (or any other method that gets called often).
NSMovieView has a mode where you can set the movie (sound) to loop, but it doesn't seem to work.
NSMovieView has a mode where you can set the movie (sound) to loop, but it doesn't seem to work.
well NSSound sends a
sound:didFinishPlaying:
message to its delegate when it finishes playing. So you could use that to replay the sound. Im guessing its still not going to be a flawless loop. But much better than using a timer.
hth,
Codemattic
PS - it does seem a little wierd that it would send a BOOL tho. I mean that BOOL is always going to be YES since the message is sent only when the sound finishes playing - so whats the point?
sound:didFinishPlaying:
message to its delegate when it finishes playing. So you could use that to replay the sound. Im guessing its still not going to be a flawless loop. But much better than using a timer.
hth,
Codemattic
PS - it does seem a little wierd that it would send a BOOL tho. I mean that BOOL is always going to be YES since the message is sent only when the sound finishes playing - so whats the point?
Thanks for the response guys. I do check every 30 miliseconds or so, so it is getting checked often. Also, I read a post somewhere else about someone using the delegate method and they didn't have any luck either.
I don't know what to do then. I guess no drum track. Maybe a very SLOW drum track.
I don't know what to do then. I guess no drum track. Maybe a very SLOW drum track.
Have you made sure the drum loop has no 'whitespace' before or after it? That could cause a problem like this.
Nope. No white space, unless it is hidden somehow. I used Sound Studio to crop the sound, so unless there is space before or after that isn't shown, it isn't there. Good suggestion, though!
im not very experienced in the cocoa language, (if you can call it that..) but doesn't it use quicktime to play sounds? if thats true, it would take a significant amount of time to access the sound, anyways, my two cents.
Good luck,
Tyrtael
Good luck,
Tyrtael
You are right. It does use Quicktime to play sounds. I found out that it didn't used to, though. Maybe not enough people have complained so far. I might have to send an e-mail to their technical staff to change it. Quicktime lets you loop (command-L), so why not NSSound?
This was one of those things buried in the 10.2 AppKit release notes. The fact you now need the run loop active has caught several people so far.
Blb,
Thanks for finding that paragraph in the AppKit release notes. They aren't clear in there exactly what would be required to make the sound loop indefinitely. I think the loop they were referring to is something else. Some sort of logical loop perhaps.
Thanks for finding that paragraph in the AppKit release notes. They aren't clear in there exactly what would be required to make the sound loop indefinitely. I think the loop they were referring to is something else. Some sort of logical loop perhaps.
I think I will try two things. First, mix Carbon with my Cocoa (Yummy!). Carbon has a function called SndPlayDoubleBuffer. Second, I might try using a quicktime movie that is sound only. That might work.
Thanks for all of your help!
Thanks for all of your help!
Code:
/*
* SndPlayDoubleBuffer()
*
* Availability:
* Mac OS X: not available
* CarbonLib: not available
* Non-Carbon CFM: in InterfaceLib 7.1 and later
*/
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Trouble with Time and GLUT looping mechanisms | Jones | 22 | 6,771 |
Aug 14, 2006 02:02 PM Last Post: ermitgilsukaru |
|

