Couldn't play system sound after switching to iOS 5

前端 未结 4 1359
失恋的感觉
失恋的感觉 2020-12-16 00:57

This is how I played a beep sound effect in iOS 4:

SystemSoundId beepOnSoundId;

CFURLRef soundUrl = CFBundleCopyResourceURL(
    CFBundleGet         


        
4条回答
  •  时光取名叫无心
    2020-12-16 01:37

    I have almost the same code and it works for me in iOS5 with a video and an audio media sessions on. If you have all the code in a function to play the sound the problem may be that the SystemSoundID object gets deallocated before the sound can be played, or the problem may be with the URL. But I guess it is the first one.

    See, the following code works:

    The SystemSoundID declaration is in the header of the object and the object lives long enough to play the sound.

        SystemSoundID soundID;
    

    The AudioServicesCreateSystemSoundID is in the object initialization.

        AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                                                       pathForResource: @"sound" ofType:@"wav"]], &soundID);
    

    Then you can use AudioServicesPlaySystemSound wherever you want inside the object.

        AudioServicesPlaySystemSound(soundID);
    

    Try putting this pieces codes in the appropriate places and you should hear the sound.

提交回复
热议问题