Ich erstelle eine Anwendung mit xcode 7.1, Swift. Ich möchte ein Audio abspielen. Alles ist gut. Nun mein Problem, ich möchte Ton hören, wenn das Gerät stumm geschaltet oder stummgeschaltet ist. Wie kann ich es tun?
Ich verwende den folgenden Code zum Abspielen von Audio
currentAudio!.stop()
currentAudio = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample_audio", ofType: "mp3")!));
currentAudio!.currentTime = 0
currentAudio!.play();
Setzen Sie diese Zeile vor dem Aufruf von play () von AVPlayer.
In Ziel C
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: nil];
In Swift
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch {
// report for an error
}
Swift 3.0 und Xcode> 8
Geben Sie den Ton im Video wieder, wenn sich das Gerät im RINGER-Modus und im SLIENT-Modus befindet
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
//print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
//print("AVAudioSession is Active")
} catch _ as NSError {
//print(error.localizedDescription)
}
} catch _ as NSError {
//print(error.localizedDescription)
}
Swift 4
verwenden Sie diese Zeile, bevor Sie das Video abspielen
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
Schnell 4.2
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
} catch let error {
print("Error in AVAudio Session\(error.localizedDescription)")
}
Sie können das durchgehen, es wird Ihnen helfen
Wenn Sie die folgenden Audiositzungskategorien verwenden, werden die Sounds unter iOS nicht stummgeschaltet: AVAudioSessionCategoryPlayback,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayAndRecord
Beispiel
func playSound (Sound: String, Type: String) {
//Prepare the sound file name & extension
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(Sound, ofType: Type)!)
//Preparation to play
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
//Play audio
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
Sie können AppDelegate
class verwenden.
Um Ton (für Audio oder Video) zu aktivieren, wenn sich das Gerät im unbeaufsichtigten Modus befindet, verwenden Sie AVAudioSessionCategoryPlayback :
func applicationDidBecomeActive(_ application: UIApplication) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
print("AVAudioSessionCategoryPlayback not work")
}
}
Um den Ton zu deaktivieren, wenn sich das Gerät im unbeaufsichtigten Modus befindet (zum Beispiel, wenn wir den Anruf entgegennehmen), verwenden Sie AVAudioSessionCategorySoloAmbient :
func applicationWillResignActive(_ application: UIApplication) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
} catch {
print("AVAudioSessionCategorySoloAmbient not work")
}
}
import AVKit
Fügen Sie dies dem Abschnitt applicationDidBecomeActive
Ihres AppDelegate hinzu
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
} catch {
print(error.localizedDescription)
}
} catch {
print(error.localizedDescription)
}
}
Swift 5.0.1
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)