What is the best way to play sound quickly upon fast button presses Xcode?

前端 未结 5 1273
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 12:59

I have a soundboard it\'s just a screen with about 8 buttons. each individual button will have its own sound which will be played upon button press There are a couple of way

相关标签:
5条回答
  • 2020-12-15 13:21

    Just going to add for the benefit of others, even though this is 2 years after the issue.

    I've found that if they are short sounds, you would be ok (ish) on your application load. buffering all your tracks by running an instance of prepare to play for every sound (in a for loop) (then releasing - just enough for your processor to remember where the file is)

    This takes away the initial loading time next time you come round to it and lets the sounds start more promptly

    0 讨论(0)
  • 2020-12-15 13:25

    22/03/2014 - Update: Got rid of my overly excited post (which was written a long time ago) and updated to portray a more appropriate answer.

    I had to achieve fine control over audio so opted to use OpenAL. WOW is one way to sum it up. As soon as I took it upon myself to use OpenAL which required a bit of extra leg work, getting all the required methods implemented and setting everything up; I found myself with practically no noticeable latency at all. I was absolutely pleased with the excellent results.

    I was able to achieve both fine control and no latency at all. I remember jumping in joy at the time when I had it working.

    Here are the resources I used that helped ease the endeavor of implementing OpenAL for the first time:

    1. OpenAL on the iPhone
    2. It was because of this link this video tutorial that I was able to create an excellent sound manager singleton. This allowed me to play all the sounds that I needed from any class that I wanted with little sound management, the singleton took care of everything for me.

    I humbly suggest everyone to use OpenAL if you require fine control over your audio, and more importantly if you require a low-latency on-demand audio, specially for games when you need to make sure sound is played there and then when you require and expect it to, an audio based application for example.

    0 讨论(0)
  • 2020-12-15 13:31

    you could create AVAudioPlayer for each sound and then just pause/play it.

    0 讨论(0)
  • 2020-12-15 13:34

    Speaking from experience, AVAudioPlayer works quite well at playing multiple sounds at the same time or very quickly one after the other. The best way to use it is to just create one method that you feed in an NSString to play some sound with the name held in that NSString... in that way, you will create a new player for each sound file that you play. Be careful about releasing the allocated players though unless you know you are finished with them.

    Unless you have very large sound files which might take a short second to buffer (you'll have to figure out for yourself if you can live with any latency or not), I've never had any issues with it being slow. If it's slow, you're probably doing something against Apple's recommendations in regards to decoding certain files (i.e. for multiple sounds at once, Apple recommends the CAF format since it is hardware decoded versus software decoded): http://developer.apple.com/iphone/library/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

    Update

    Here is how I encode my files with a shell script. Put this in a file called batchit.sh in it's own directory. Then place whatever .WAV files you want to encode as a .CAF file in that directory. Open up the Terminal on your Mac, cd to that directory and type sh batchit.sh and let it do it's thing. It will batch convert all the files... Here's the code:

    for f in *; do
    if  [ "$f" != "batchit.sh" ]
    then
        /usr/bin/afconvert -f caff -d ima4 $f 
        echo "$f converted"
    fi
    done
    

    Notice I didn't do anything in the audioPlayerDidFinishPlaying method which is a delegate method of AVAudioPlayer. You will need to add code to properly release the players after each is finished playing (hey, I can't do all the work for you :) so that all the memory is allocated correctly. Otherwise you will eventually run out if you indefinitely keep creating players without releasing them. Since it seems like you're having a rough day.. see below for a big hint.

    That's all the help I can give though. You gotta learn & earn the rest

    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    
        // do something here if you want.. you know like maybe release the old players by uncommenting the line below :)
    
        //[player release];
    
    }
    
    0 讨论(0)
  • 2020-12-15 13:43

    Have you tried playing back each sound in its own thread? You could try a basic NSOperation

    0 讨论(0)
提交回复
热议问题