gpt4 book ai didi

Swift- 在 Cocoa 中播放系统默认声音的最简单方法(macOS、OS X)

转载 作者:可可西里 更新时间:2023-10-31 23:54:46 25 4
gpt4 key购买 nike

我正在学习 Cocoa 编程。

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

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

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

提前致谢

最佳答案

最简单?将其放入项目的 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()

关于Swift- 在 Cocoa 中播放系统默认声音的最简单方法(macOS、OS X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38441742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com