Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 5, 2013, 07:11 PM   #1
xArtx
macrumors 6502a
 
Join Date: Mar 2012
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
Now what happens is, it works, but every so often when I play the 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.
xArtx is offline   0 Reply With Quote
Old Jan 5, 2013, 11:34 PM   #2
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
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.
xStep is offline   0 Reply With Quote
Old Jan 6, 2013, 12:21 AM   #3
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
Whoops..
Should have mentioned that the code above is only executed once,
then I just use:
Code:
[shot play];
Still, I'll look into your other suggestions.
Funny how it seems to only occur more if the sample hasn't played for a while.
xArtx is offline   0 Reply With Quote
Old Jan 6, 2013, 11:53 AM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by xArtx View Post
Funny how it seems to only occur more if the sample hasn't played for a while.
Which suggests there's a cache of audio samples, and a Least Recently Used cache algorithm has ejected the audio you want, so it has to be reloaded into RAM.
chown33 is offline   0 Reply With Quote
Old Jan 6, 2013, 06:15 PM   #5
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
Quote:
Originally Posted by chown33 View Post
Which suggests there's a cache of audio samples, and a Least Recently Used cache algorithm has ejected the audio you want, so it has to be reloaded into RAM.
It could look that way, but this is the only sample in the program at the time I wrote the first post.
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;
}
xArtx is offline   0 Reply With Quote
Old Jan 6, 2013, 08:53 PM   #6
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
Quote:
Originally Posted by xArtx View Post
It could look that way, but this is the only sample in the program at the time I wrote the first post.
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;
}
Your code doesn't support your statement of simultaneous playback. More importantly, the Apple docs clearly show how to play more than one sample in sync, if that is what you are referring to.
xStep is offline   0 Reply With Quote
Old Jan 7, 2013, 08:56 AM   #7
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
Quote:
Originally Posted by xStep View Post
Your code doesn't support your statement of simultaneous playback. More importantly, the Apple docs clearly show how to play more than one sample in sync, if that is what you are referring to.
At the time I wrote the first post,
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.
xArtx is offline   0 Reply With Quote
Old Jan 7, 2013, 09:10 AM   #8
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Have you considered using AudioServicesPlaySystemSound instead of AVAudioPlayer?
__________________
dejo is offline   0 Reply With Quote
Old Jan 7, 2013, 06:45 PM   #9
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
Quote:
Originally Posted by dejo View Post
Have you considered using AudioServicesPlaySystemSound instead of AVAudioPlayer?
I have used it in my very first iPhone program, and it was suitable for that,
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
xArtx is offline   0 Reply With Quote
Old Jan 7, 2013, 06:54 PM   #10
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by xArtx View Post
I have used it in my very first iPhone program, and it was suitable for that,
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.
Understood. Just thought I'd check.
__________________
dejo is offline   0 Reply With Quote
Old Jan 13, 2013, 11:52 PM   #11
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
This seems to help.. every time you play the sample:

Code:
        [shoot play];
        [shoot prepareToPlay];
xArtx is offline   0 Reply With Quote
Old Jan 14, 2013, 07:00 AM   #12
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
Quote:
Originally Posted by xArtx View Post
This seems to help.. every time you play the sample:

Code:
        [shoot play];
        [shoot prepareToPlay];
Shouldn't play come after prepareToPlay?
xStep is offline   0 Reply With Quote
Old Jan 14, 2013, 04:16 PM   #13
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
Quote:
Originally Posted by xStep View Post
Shouldn't play come after prepareToPlay?
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.
xArtx is offline   0 Reply With Quote
Old Jan 14, 2013, 06:17 PM   #14
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
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.
xStep is offline   0 Reply With Quote
Old Jan 14, 2013, 07:10 PM   #15
xArtx
Thread Starter
macrumors 6502a
 
Join Date: Mar 2012
Quote:
Originally Posted by xStep View Post
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.
I understand, and If you are wondering I'll be able to update soon
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.
xArtx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Audio not working w. iOS 5.1.1 on iPad 1 [AVFoundation] Danneman101 iPhone/iPad Programming 2 Feb 9, 2014 05:19 PM
Noob Objective-C / Framework / AVFoundation question Golgafrincham Mac Programming 0 Jan 30, 2014 02:54 AM
AVFoundation video capture. anandcta123 Mac Programming 2 Apr 4, 2013 11:37 PM
rMBP - Safari is the culprit for the general UI lag (not just scroll lag) Valkyre MacBook Pro 6 Dec 10, 2012 03:43 AM
Strange crash with AVFoundation rukasu01 iPhone/iPad Programming 5 Jul 9, 2012 09:40 PM

Forum Jump

All times are GMT -5. The time now is 07:41 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC