Playing system sound without importing your own

后端 未结 10 2124
情深已故
情深已故 2020-11-27 02:26

Is it possible to play already existing system sounds without importing your own?

相关标签:
10条回答
  • 2020-11-27 03:23

    adapted from @yannc2021

    http://iphonedevwiki.net/index.php/AudioServices

    if you want to use system sound in Swift

    // import this
    import AVFoundation
    
    // add this method
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // declared system sound here
    let systemSoundID: SystemSoundID = 1104
    
    // to play sound
    AudioServicesPlaySystemSound (systemSoundID)
    
    0 讨论(0)
  • 2020-11-27 03:26

    This cocoa-only solution uses existing audio files to play sounds. This method can be used to play any sound file. AVFoundation.framework will have to be added to your frameworks. You will have to define or remove the macros I use which are self explanatory.

    I added a category to AVAudioPlayer as follows:

    AVAudioPlayer+.h

     #import <AVFoundation/AVAudioPlayer.h>
    
    @interface AVAudioPlayer ( CapSpecs )
    + (AVAudioPlayer*) click;
    + (AVAudioPlayer*) tink;
    + (AVAudioPlayer*) tock;
    + (AVAudioPlayer*) withResourceName: (NSString*) aName;
    @end
    

    AVAudioPlayer+.m

     #import "AVAudioPlayer+.h"
    
    @implementation AVAudioPlayer ( CapSpecs )
    + (AVAudioPlayer*) click {
        StaticReturn ( [AVAudioPlayer withResourceName: @"iPod Click"] );
    }
    + (AVAudioPlayer*) tink {
        StaticReturn ( [AVAudioPlayer withResourceName: @"Tink"] );
    }
    + (AVAudioPlayer*) tock {
        StaticReturn ( [AVAudioPlayer withResourceName: @"Tock"] );
    }
    + (AVAudioPlayer*) withResourceName: (NSString*) aName {
        NSBundle* zBundle = [NSBundle bundleWithIdentifier: @"com.apple.UIKit"];
        NSURL* zURL = [zBundle URLForResource: aName withExtension: @"aiff"];
        (void) RaiseIfNil ( nil, zURL, ([SWF @"URL for %@",aName]) );
        NSError* zError = nil;
        AVAudioPlayer* zAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: zURL error: &zError];
        RaiseError ( nil, zError, @"AVAudioPlayer init error" );
    
    #ifdef DEBUG
        //  Apple records the console dump which occurs as a bug in the iOS simulator
        //  all of the following commented code causes the BS console dump to be hidden
        int zOldConsole = dup(STDERR_FILENO);   // record the old console
        freopen("/dev/null", "a+", stderr); // send console output to nowhere
        (void)[zAudio prepareToPlay];       // create the BS dump
        fflush(stderr);             // flush the console output
        dup2(zOldConsole, STDERR_FILENO);   // restore the console output
    #endif
    
        return zAudio;
    }
    @end
    
    0 讨论(0)
  • 2020-11-27 03:29

    This code plays apple system sound "Tock.aiff"..I believe you can play different system sounds using this

    NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
    AudioServicesPlaySystemSound(soundID);
    AudioServicesDisposeSystemSoundID(soundID);
    

    See this thread

    Apple's documentation on System sounds

    https://developer.apple.com/documentation/audiotoolbox/system_sound_services

    0 讨论(0)
  • 2020-11-27 03:29

    List of all system sounds: iOSSystemSoundsLibrary

    After you import AVKit, you can play all this sounds with:

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