Avplayer音频仅在铃声打开时起作用

发布于 2025-02-04 05:53:17 字数 1833 浏览 1 评论 0原文

使用Swiftui,我有一个自动播放和循环视频的自定义Avplayer。问题是我是否特别告诉Avplayer取消静音,它仍然被静音。物理体积按钮无效。打开/关的静音的唯一方法是将铃声实际切换到静音(静音)或不静音(取消静音)。

这是父景观:

struct VideoCacheView: View {
    @State private var avPlayer: AVPlayer? = nil
    public let url: String
    public let thumbnailURL: String
    var body: some View {
        if self.avPlayer != nil {
            CustomVideoPlayer(player: Binding(self.$avPlayer)!)
                .onAppear {
                    self.avPlayer?.isMuted = false
                    self.avPlayer?.play()
                }
        }
    }
}

孩子:

struct CustomVideoPlayer: UIViewControllerRepresentable {
    @EnvironmentObject var cvm: CameraViewModel
    @Binding var player: AVPlayer
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    func makeUIViewController(context: Context) -> AVPlayerViewController {
        let controller = AVPlayerViewController()
        controller.player = self.player
        controller.showsPlaybackControls = false
        controller.videoGravity = self.cvm.videoGravity
        player.actionAtItemEnd = .none
        
        NotificationCenter.default.addObserver(context.coordinator, selector: #selector(context.coordinator.restartPlayback), name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
        
        return controller
    }
    
    func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) { }
    
    class Coordinator: NSObject {
        public var parent: CustomVideoPlayer
        init(_ parent: CustomVideoPlayer) {
            self.parent = parent
        }
        
        @objc func restartPlayback () {
            self.parent.player.seek(to: .zero)
        }
    }
}

为什么我的Avplayer拥有的唯一卷控制是物理静音开关?

With SwiftUI, I have a custom avplayer that auto plays and loops the video. The problem is whether or not I specifically tell avplayer to unmute, it is still muted. The physical volume buttons have no effect. The only way to toggle mute on/off is to physically switch the ringer to silent (muted) or not silent (unmute).

Here is the parent view:

struct VideoCacheView: View {
    @State private var avPlayer: AVPlayer? = nil
    public let url: String
    public let thumbnailURL: String
    var body: some View {
        if self.avPlayer != nil {
            CustomVideoPlayer(player: Binding(self.$avPlayer)!)
                .onAppear {
                    self.avPlayer?.isMuted = false
                    self.avPlayer?.play()
                }
        }
    }
}

and the child:

struct CustomVideoPlayer: UIViewControllerRepresentable {
    @EnvironmentObject var cvm: CameraViewModel
    @Binding var player: AVPlayer
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    func makeUIViewController(context: Context) -> AVPlayerViewController {
        let controller = AVPlayerViewController()
        controller.player = self.player
        controller.showsPlaybackControls = false
        controller.videoGravity = self.cvm.videoGravity
        player.actionAtItemEnd = .none
        
        NotificationCenter.default.addObserver(context.coordinator, selector: #selector(context.coordinator.restartPlayback), name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
        
        return controller
    }
    
    func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) { }
    
    class Coordinator: NSObject {
        public var parent: CustomVideoPlayer
        init(_ parent: CustomVideoPlayer) {
            self.parent = parent
        }
        
        @objc func restartPlayback () {
            self.parent.player.seek(to: .zero)
        }
    }
}

Why is the only volume control my avplayer has is with the physicaly silent switch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彡翼 2025-02-11 05:53:17

这可以通过将avauioSession类别设置为.playback

try? AVAudioSession.sharedInstance().setCategory(.playback)
player.play()

另请参阅 avauiosession 类别来实现这一点。当设备处于静音模式时播放音频时,设备内部模式 - 模式 - ios-swift-ios swift

This can be accomplished by setting the AVAudioSession category to .playback:

try? AVAudioSession.sharedInstance().setCategory(.playback)
player.play()

See also Play Audio when device in silent mode - ios swift

望她远 2025-02-11 05:53:17

https://developer.apple.apple.com/documenter.com/documentation/documentation/avplayation/avflayation/avflayation/avflayer/139990127-13990127-1390127-音量

事实证明,当Ringer处于静音模式时,音量设置为0.0。默认情况下,将卷设置为1.0,始终存在卷。

添加了以下内容:

self.player?.volume = 1.0

在Videogravity系列下方的儿童视图内部

https://developer.apple.com/documentation/avfoundation/avplayer/1390127-volume

Turns out that the volume is set to 0.0 when ringer is in silent mode. By setting the volume to 1.0 by default, there is volume all the time.

Added this:

self.player?.volume = 1.0

inside of the child view below the videoGravity line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文