设备在静音模式下播放音频-iOS Swift [英] Play Audio when device in silent mode - ios swift

查看:439
本文介绍了设备在静音模式下播放音频-iOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xcode 7.1创建应用程序,很快.我想播放音频.一切都好.现在我的问题是,当设备处于静音模式或静音时,我想听到声音.我该怎么办?

I am creating an application using xcode 7.1, swift. I want to play an audio. Everything is fine. Now my problem I want to hear sound when the device in silent mode or muted. How can I do it?

我正在使用以下代码播放音频

I am using the following code to play audio

currentAudio!.stop()
            currentAudio = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample_audio", ofType: "mp3")!));

            currentAudio!.currentTime = 0

            currentAudio!.play();

推荐答案

在调用AVPlayer的 play()方法之前放入此行.

Put this line before calling play() method of AVPlayer.

在目标C中

[[AVAudioSession sharedInstance]
            setCategory: AVAudioSessionCategoryPlayback
                  error: nil];

在Swift中

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
 }
 catch {
    // report for an error
 }

快捷键5

do {
      try AVAudioSession.sharedInstance().setCategory(.playback)
   } catch(let error) {
       print(error.localizedDescription)
   }

这篇关于设备在静音模式下播放音频-iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆