如果禁用音频/振动,AVPlayer不会发声:打开

时间:2016-10-17 15:09:02

标签: ios swift audio avfoundation avplayer

Hy,我试图了解即使你的iPhone设置为静音,是否有办法从播放器获取声音。是否有Swift代码启用音频?

//IMPOSRT FOUNDATION PER PLAYER
import AVFoundation

        let url = NSURL(string: "https://mywebsite.site/file/\(audio_r!)")
        playerItem = AVPlayerItem(URL: url!)
        player = AVPlayer(playerItem: playerItem!)
        let playerLayer = AVPlayerLayer(player: player!)
        playerLayer.frame = CGRectMake(0,0,300,50)
        self.view.layer.addSublayer(playerLayer)
        //imposto il volume
        player!.muted = false
        player!.volume = 1.0

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:8)

播放类别不应遵循静音开关

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
AVAudioSession.sharedInstance().setActive(true)