从 Apple Watch 扬声器播放声音
Play sound from Apple Watch speaker
有没有办法让 Apple Watch 的扬声器播放声音?我一直无法在网上找到任何文档。
无法从 Apple Watch 的扬声器播放声音,但您可以在 iPhone 上触发播放声音文件,这里是 thread
从 watchOS 2 开始可以使用 WKAudioFilePlayer
或 WKInterfaceMovie
。
NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"file" withExtension:@"wav"];
WKAudioFilePlayer
示例:
WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:assetURL];
WKAudioFilePlayerItem *playerItem = [WKAudioFilePlayerItem playerItemWithAsset:asset];
WKAudioFilePlayer *audioFilePlayer = [WKAudioFilePlayer playerWithPlayerItem:playerItem];
[audioFilePlayer play];
WKInterfaceMovie
示例:
[self presentMediaPlayerControllerWithURL:assetURL options:nil completion:nil];
- presentMediaPlayerControllerWithURL:选项:完成:
(watchOS 2.0 中的新功能)
URL
您要播放的媒体文件的URL。 URL 必须指定一个文件;不支持流媒体。该文件可能包含音频、视频或两者。
如果您为远程服务器上的文件指定 URL,此方法将首先下载文件并显示一个进度指示器,显示操作的进度。因为 WatchKit 从 Web 服务器下载文件时使用 App Transport Security (ATS),所以文件必须在安全服务器上,并且 URL 必须使用 https 方案。如果您的服务器不支持ATS级别的安全,请在播放前自行下载文件。
使用 sharedcontainer,watch extensions 来存储文件。
您使用此方法播放的任何音频都会路由到配对的蓝牙音频设备(如果有的话)。如果没有可用的蓝牙音频设备,音频将路由到 Apple Watch 扬声器。
对于 WatchOS3,原始问题的答案是 WKInterfaceInlineMovie
https://developer.apple.com/reference/watchkit/wkinterfaceinlinemovie
您可以隐藏小部件,这样它就不会改变您的界面设计。如果没有连接蓝牙扬声器,它会通过手表的扬声器播放音频文件。
import AVFoundation
var player: AVAudioPlayer?
if let path = Bundle.main.path(forResource: "siren", ofType: "wav") {
let fileUrl = URL(fileURLWithPath: path)
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: fileUrl)
guard let player = player else { return }
player.play()
}
catch
{
}
}
我用它从 Apple Watch(4.3) 扬声器播放自定义声音,效果很好。
不要忘记将音频文件目标成员设置为手表套件。
在InterfaceController.swift文件中
在模拟器和设备中也能正常工作
适用于最新的 WatchOS 5
import AVFoundation
var player = AVAudioPlayer()
let audioSession = AVAudioSession.sharedInstance()
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
do {
// Working Reroutes to headset
// try session.setCategory(AVAudioSession.Category.playback,
// mode: .default,
// policy: .longForm,
// options: [])
// Plays in watch speaker
try audioSession.setCategory(AVAudioSession.Category.playback,
mode: .default,
policy: .default,
options: [])
} catch let error {
fatalError("*** Unable to set up the audio session: \(error.localizedDescription) ***")
}
if let path = Bundle.main.url(forResource: "piano", withExtension: "mp3") {
let fileUrl = path
do{
player = try AVAudioPlayer(contentsOf: fileUrl)
}
catch
{
print("*** Unable to set up the audio player: \(error.localizedDescription) ***")
// Handle the error here.
return
}
}
}
使用此代码调用音频会话并在播放按钮操作内或您希望播放音频的任何情况下播放音频。
audioSession.activate(options: []) { (success, error) in
guard error == nil else {
print("*** error occurred: \(error!.localizedDescription)
***")
// Handle the error here.
return
}
if(success){
// Play the audio file.
self.player.play()
}
}
如果您仍然无法播放音频,那一定是因为您在音频文件中得到的是 nil 值,指定的音频文件不在您正在搜索的正确包中
有没有办法让 Apple Watch 的扬声器播放声音?我一直无法在网上找到任何文档。
无法从 Apple Watch 的扬声器播放声音,但您可以在 iPhone 上触发播放声音文件,这里是 thread
从 watchOS 2 开始可以使用 WKAudioFilePlayer
或 WKInterfaceMovie
。
NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"file" withExtension:@"wav"];
WKAudioFilePlayer
示例:
WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:assetURL];
WKAudioFilePlayerItem *playerItem = [WKAudioFilePlayerItem playerItemWithAsset:asset];
WKAudioFilePlayer *audioFilePlayer = [WKAudioFilePlayer playerWithPlayerItem:playerItem];
[audioFilePlayer play];
WKInterfaceMovie
示例:
[self presentMediaPlayerControllerWithURL:assetURL options:nil completion:nil];
- presentMediaPlayerControllerWithURL:选项:完成: (watchOS 2.0 中的新功能)
URL 您要播放的媒体文件的URL。 URL 必须指定一个文件;不支持流媒体。该文件可能包含音频、视频或两者。
如果您为远程服务器上的文件指定 URL,此方法将首先下载文件并显示一个进度指示器,显示操作的进度。因为 WatchKit 从 Web 服务器下载文件时使用 App Transport Security (ATS),所以文件必须在安全服务器上,并且 URL 必须使用 https 方案。如果您的服务器不支持ATS级别的安全,请在播放前自行下载文件。
使用 sharedcontainer,watch extensions 来存储文件。
您使用此方法播放的任何音频都会路由到配对的蓝牙音频设备(如果有的话)。如果没有可用的蓝牙音频设备,音频将路由到 Apple Watch 扬声器。
对于 WatchOS3,原始问题的答案是 WKInterfaceInlineMovie https://developer.apple.com/reference/watchkit/wkinterfaceinlinemovie
您可以隐藏小部件,这样它就不会改变您的界面设计。如果没有连接蓝牙扬声器,它会通过手表的扬声器播放音频文件。
import AVFoundation
var player: AVAudioPlayer?
if let path = Bundle.main.path(forResource: "siren", ofType: "wav") {
let fileUrl = URL(fileURLWithPath: path)
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: fileUrl)
guard let player = player else { return }
player.play()
}
catch
{
}
}
我用它从 Apple Watch(4.3) 扬声器播放自定义声音,效果很好。 不要忘记将音频文件目标成员设置为手表套件。
在InterfaceController.swift文件中
在模拟器和设备中也能正常工作 适用于最新的 WatchOS 5
import AVFoundation
var player = AVAudioPlayer()
let audioSession = AVAudioSession.sharedInstance()
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
do {
// Working Reroutes to headset
// try session.setCategory(AVAudioSession.Category.playback,
// mode: .default,
// policy: .longForm,
// options: [])
// Plays in watch speaker
try audioSession.setCategory(AVAudioSession.Category.playback,
mode: .default,
policy: .default,
options: [])
} catch let error {
fatalError("*** Unable to set up the audio session: \(error.localizedDescription) ***")
}
if let path = Bundle.main.url(forResource: "piano", withExtension: "mp3") {
let fileUrl = path
do{
player = try AVAudioPlayer(contentsOf: fileUrl)
}
catch
{
print("*** Unable to set up the audio player: \(error.localizedDescription) ***")
// Handle the error here.
return
}
}
}
使用此代码调用音频会话并在播放按钮操作内或您希望播放音频的任何情况下播放音频。
audioSession.activate(options: []) { (success, error) in
guard error == nil else {
print("*** error occurred: \(error!.localizedDescription)
***")
// Handle the error here.
return
}
if(success){
// Play the audio file.
self.player.play()
}
}
如果您仍然无法播放音频,那一定是因为您在音频文件中得到的是 nil 值,指定的音频文件不在您正在搜索的正确包中