Play a short sound in iOS

前端 未结 13 835
悲哀的现实
悲哀的现实 2020-11-27 10:23

I guess I could use AVAudioPlayer to play a sound, however, what I need is to just play a short sound and I don\'t need any loops or fine-grained control over t

13条回答
  •  萌比男神i
    2020-11-27 11:00

    My answer is Bill's answer, but I use it without init or dealloc and release the sound after it's played:

    - (void)playSound:(NSURL *)url
        SystemSoundID ssID = 0;
        AudioServicesCreateSystemSoundID((CFURLRef)url, &ssID);
        AudioServicesAddSystemSoundCompletion(ssID, NULL, NULL, (AudioServicesSystemSoundCompletionProc)MyAudioServicesSystemSoundCompletionProc, NULL);
        AudioServicesPlaySystemSound(ssID);
        //AudioServicesDisposeSystemSoundID(ssID);
    }
    
    void MyAudioServicesSystemSoundCompletionProc (SystemSoundID  ssID, void *clientData) {
        AudioServicesDisposeSystemSoundID(ssID);
    }
    

提交回复
热议问题