【问题标题】:Swift- Simplest way to play system default sounds in Cocoa(macOS,OS X)Swift - 在 Cocoa (macOS,OS X) 中播放系统默认声音的最简单方法
【发布时间】:2016-07-18 16:27:04
【问题描述】:

我正在学习 Cocoa 编程。

我只需要在我自己的 Cocoa 项目中完成或失败异步任务时播放声音。

所以我想知道什么是最简单的方法。

虽然它应该很容易,但我在 Swift 中没有找到它。

在此先感谢

【问题讨论】:

    标签: swift macos cocoa


    【解决方案1】:

    可能,最简单的方法是使用NSSound。例如:

    NSSound(named: "Purr")?.play()
    

    来自 Apple 文档:

    如果没有已知的带有 soundName 的 NSSound 对象,这个方法会尝试 通过在应用程序的主目录中搜索声音文件来创建一个 捆绑包(有关捆绑包内容的描述,请参见 NSBundle 被搜索)。如果应用程序主目录中没有声音文件 bundle,依次搜索以下目录:

    • ~/库/声音

    • /图书馆/声音

    • /Network/Library/Sounds

    • /System/Library/Sounds

    如果要播放系统提示音,请使用NSBeep函数。

    【讨论】:

    • 这很好而且很简单!我不想仅仅为了播放默认声音而导入任何类。谢谢。
    • 请注意,在 Swift 3.0、macOS High Sierra 中,您必须改用:NSSound(named: NSSound.Name(rawValue: "Purr"))?.play()
    • 这已经被 Swift 4 改变了。该行现在是NSSound(named: NSSound.Name("Purr"))?.play() EDIT kitsune 是正确的,但不需要 rawValue 描述符。
    • 我应该导入什么来使用 NSSound? Swift 显示使用未解析的标识符“NSSound”
    • @bodich 如果你是google NSSound,第一个结果是the Apple documentation for it。在那里,你会看到它在 AppKit 中。
    【解决方案2】:

    最简单?将其放入项目中的 Swift 文件中:

    Swift 4.2 到 5.3

    import AppKit
    
    
    
    public extension NSSound {
        static let basso     = NSSound(named: .basso)
        static let blow      = NSSound(named: .blow)
        static let bottle    = NSSound(named: .bottle)
        static let frog      = NSSound(named: .frog)
        static let funk      = NSSound(named: .funk)
        static let glass     = NSSound(named: .glass)
        static let hero      = NSSound(named: .hero)
        static let morse     = NSSound(named: .morse)
        static let ping      = NSSound(named: .ping)
        static let pop       = NSSound(named: .pop)
        static let purr      = NSSound(named: .purr)
        static let sosumi    = NSSound(named: .sosumi)
        static let submarine = NSSound(named: .submarine)
        static let tink      = NSSound(named: .tink)
    }
    
    
    
    public extension NSSound.Name {
        static let basso     = NSSound.Name("Basso")
        static let blow      = NSSound.Name("Blow")
        static let bottle    = NSSound.Name("Bottle")
        static let frog      = NSSound.Name("Frog")
        static let funk      = NSSound.Name("Funk")
        static let glass     = NSSound.Name("Glass")
        static let hero      = NSSound.Name("Hero")
        static let morse     = NSSound.Name("Morse")
        static let ping      = NSSound.Name("Ping")
        static let pop       = NSSound.Name("Pop")
        static let purr      = NSSound.Name("Purr")
        static let sosumi    = NSSound.Name("Sosumi")
        static let submarine = NSSound.Name("Submarine")
        static let tink      = NSSound.Name("Tink")
    }
    

    Swift 3.2 到 4.1

    import AppKit
    
    
    
    public extension NSSound {
        
        #if !swift(>=4)
        private convenience init?(named name: Name) {
            self.init(named: name as String)
        }
        #endif
        
        public static let basso     = NSSound(named: .basso)
        public static let blow      = NSSound(named: .blow)
        public static let bottle    = NSSound(named: .bottle)
        public static let frog      = NSSound(named: .frog)
        public static let funk      = NSSound(named: .funk)
        public static let glass     = NSSound(named: .glass)
        public static let hero      = NSSound(named: .hero)
        public static let morse     = NSSound(named: .morse)
        public static let ping      = NSSound(named: .ping)
        public static let pop       = NSSound(named: .pop)
        public static let purr      = NSSound(named: .purr)
        public static let sosumi    = NSSound(named: .sosumi)
        public static let submarine = NSSound(named: .submarine)
        public static let tink      = NSSound(named: .tink)
    }
    
    
    
    public extension NSSound.Name {
        
        #if !swift(>=4)
        private convenience init(_ rawValue: String) {
            self.init(string: rawValue)
        }
        #endif
        
        public static let basso     = NSSound.Name("Basso")
        public static let blow      = NSSound.Name("Blow")
        public static let bottle    = NSSound.Name("Bottle")
        public static let frog      = NSSound.Name("Frog")
        public static let funk      = NSSound.Name("Funk")
        public static let glass     = NSSound.Name("Glass")
        public static let hero      = NSSound.Name("Hero")
        public static let morse     = NSSound.Name("Morse")
        public static let ping      = NSSound.Name("Ping")
        public static let pop       = NSSound.Name("Pop")
        public static let purr      = NSSound.Name("Purr")
        public static let sosumi    = NSSound.Name("Sosumi")
        public static let submarine = NSSound.Name("Submarine")
        public static let tink      = NSSound.Name("Tink")
    }
    

    然后您可以非常简单地播放任何系统声音,如下所示:

    NSSound.glass?.play()
    

    注意,你也可以让系统播放默认的错误声音如下:

    Swift 4.0 及更高版本(通过 5.3 测试)

    NSSound.beep()
    

    Swift 1-3 和 Objective-C

    NSBeep()
    

    【讨论】:

      【解决方案3】:

      我发现这适用于版本 8.3.3 Playground:

      import AudioToolbox
      AudioServicesPlaySystemSound(1209) //plays a beep tone
      

      【讨论】:

      • 我怎么知道给这个函数调用什么幻数?
      • This website 有一个声音编号列表,但它适用于 iOS,因此并非所有这些都适用于 macOS。 1209 似乎在播放“dtmf-9.caf”。
      【解决方案4】:

      在 Swift 4 中,您可以使用 NSSound.beep() 来播放系统偏好设置中设置的系统提示音。

      【讨论】:

        【解决方案5】:

        在这里,导入 macOS 10.9+ (Xcode 9.0+):https://developer.apple.com/documentation/appkit/nssound/2903487-beep

        import AppKit
        
        NSSound.beep()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-22
          • 2023-02-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多