我的iphone应用程序中有简单的 AVAudioPlayer ,它可以工作,但输出应该是手机扬声器而不是耳机。我在这里找到similiar topic,但不知道如何将其“翻译”为swift( AudioSessionSetProperty 让我感到困惑)?
var audioPlayer = AVAudioPlayer(data: fileData, error: &playbackError)
//fileData is url to file
if let player = audioPlayer{
player.delegate = self
if(player.prepareToPlay() && player.play()){
println("Started playing the recorded audio")
} else {
println("Could not play the audio")
}
}
答案 0 :(得分:17)
我能理解这是多么令人困惑,因为我只是想出了答案。因此,在iOS 7中已弃用AudioSessionSetProperty
。
添加:
session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: nil)
请务必先拨打AVAudioSession.sharedInstance()
。
答案 1 :(得分:12)
在Swift 3或4中:
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
为避免OSStatus
错误-50(评论中的user462990提及),在设置会话类别(下面的代码)后必须调用overrideOutputAudioPort
。
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
print("setCategory error: \(error.localizedDescription)")
}
答案 2 :(得分:2)
尝试这些功能。他们很有魅力。
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Pusher from 'pusher-js/react-native';
export default class App extends React.Component {
componentWillMount() {
Pusher.logToConsole = true;
var pusher = new Pusher('*********', {
authEndpoint: 'http://app.pgm/api/authtest',
cluster: 'eu',
encrypted: true
});
const channel = pusher.subscribe('private-chat-1');
}
答案 3 :(得分:1)
迅速5
func setSessionPlayerOn() {
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
} catch _ {
}
}
答案 4 :(得分:1)
您可以在会话设置期间将音频会话默认设置为内置扬声器而不是接收器。
do {
// 1) Configure your audio session category, options, and mode
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: [.defaultToSpeaker])
// 2) Activate your audio session to enable your custom configuration
try session.setActive(true)
} catch let error as NSError {
print("Failed to set the audio session category and mode: \(error.localizedDescription)")
}
答案 5 :(得分:0)
Dim positiveString As String = "0C0020ACB1086886D8C2E4D2DEDC726A6"