|
|
#1 |
|
AVFoundation sound lag
Hi
![]() I'm having a small problem with a sound sample I'm playing over & over in a program. It's a wav file set up like this: Code:
NSURL *jurl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/shot.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *jerror;
shot = [[AVAudioPlayer alloc] initWithContentsOfURL:jurl error:&jerror];
shot.numberOfLoops = 0;
shot.volume = 0.20;
[shot play]; // shot sound
there is about a half second pause in all the graphics when the sound is about to play. The action resumes just as the sample begins. Any ideas what might be happening? Cheers, Art. Last edited by xArtx; Jan 13, 2013 at 11:52 PM. |
|
|
|
0
|
|
|
#2 |
|
I don't know, but from parsing the documentation, I'd try the prepareToPlay method.
I'd also look to see how playAtTime: might work into this issue. If your actual code is inline like that, I suggest trying the allocation at some earlier point and making shot an ivar. Based on the name I'm wondering if this is a short sample sound like a gun shot. Perhaps there are better ways to execute such a thing and have it timed with the a graphic such as a gun shooting. Describe what you are trying to do as that might trigger someone with the experience to pop in on your question.
__________________
My App: CameraTime - Time lapse photography for novice and advanced users.
|
|
|
|
0
|
|
|
#3 |
|
Whoops..
Should have mentioned that the code above is only executed once, then I just use: Code:
[shot play]; Funny how it seems to only occur more if the sample hasn't played for a while. |
|
|
|
0
|
|
|
#4 | |
|
Quote:
|
||
|
|
0
|
|
|
#5 | |
|
Quote:
Usually I use at least two instances of the same sample in case they have to be played simultaneously though. Code:
if (swap == 0) {
[shota play];
swap = 1;
} else {
[shotb play];
swap = 0;
}
|
||
|
|
0
|
|
|
#6 | |
|
Quote:
|
||
|
|
0
|
|
|
#7 | |
|
Quote:
I'm only referring to a program that has one instance of one sample. Problem is the same if I load another instance of it though. It makes no difference to the problem. Still, it's only minor. |
||
|
|
0
|
|
|
#8 |
|
Have you considered using AudioServicesPlaySystemSound instead of AVAudioPlayer?
__________________
|
|
|
|
0
|
|
|
#9 | |
|
Quote:
but the problem is if the user has their ringer volume and normal (iPod) volume set to different levels, system sound plays at the ringer volume level. This means the user has to go into settings to adjust the ringer volume to change the volume level of that particular app, which is going to be undesirable. Documentation suggested you can't play sounds simultaneously, but I did find that you can at least play (different) samples simultaneously, which means the little routine I posted above could likely play different instances of the same sound simultaneously. It was fun cycling through all of the stock system sounds though. You can cycle and play their IDs very fast, so the next sounds starts before the other has finished, and quite a few overlap each other. I was going to record it and use it as a ringtone
|
||
|
|
0
|
|
|
#10 | |
|
Quote:
__________________
|
||
|
|
0
|
|
|
#11 |
|
This seems to help.. every time you play the sample:
Code:
[shoot play];
[shoot prepareToPlay];
|
|
|
|
0
|
|
|
#12 |
|
|
0
|
|
|
#13 |
|
I'm using prepareToPlay at the start when the sound files are loaded.
From there it's: Code:
[shoot play]; // play the sound instantly
[shoot prepareToPlay]; // prepare to play for the next time
So play is always called after prepareToPlay. |
|
|
|
0
|
|
|
#14 |
|
Ok, I get it. You. Have an initial prep and then a prep right after the play command. I wonder of there might be a better way. My concern would be that if the next play occurs after a long period if time that the prep buffersogjt bee purged.
|
|
|
|
0
|
|
|
#15 | |
|
Quote:
with one more little experiment. I was going to post & say the problem is about 90% solved, but before posting this, I went through my source, and there were a couple of instances where I forgot to put the prepareToPlay after the play. Now everything is running smoothly ![]() Note that this is an action game where the shot sample can (and should) be called several times per second. If it turns out the prep command keeps the file in RAM for the long term, I guess it would be good to use it when the program shows a menu or something. Then the button push or whatever triggers the play. I have some other samples in the game that are called not so often. One can be called up to 50 seconds into the game, and it's still fine. I might test a longer period though, with a sample used only once or something like that. |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 01:12 PM.








Linear Mode
