0

使用 Swift5.3.2、iOS14.4.1、Xcode 12.4、

我在 SwiftUI 中成功运行了 VideoPlayer。

我正在使用以下代码调用 Player 视图:VideoPlayer(player: AVPlayer(url: url)).

问题是,只要 VideoPlayer 的父视图更新(即重新渲染),视频就会停止播放。

由于在 SwiftUI 中我无法控制何时发生这种重新渲染时刻,所以我不知道如何克服这个问题。

有任何想法吗 ?

这是整个代码:

VideoPlayer 视图是这样调用的:

struct MediaTabView: View {
    
    @State private var url: URL
    
    var body: some View {
        
        // CALL TO VIDEOPLAYER IS HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        VideoPlayer(player: AVPlayer(url: url))
    }
}

MediaTabView 是这样调用的:

import SwiftUI

struct PageViewiOS: View {
    
    var body: some View {
        
        ZStack {
            
            Color.black
            
            // CALL TO MEDIATABVIEW IS HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!d
            MediaTabView(url: URL(string: "https://someurel.com"))
                
            CloseButtonView()
        }
    }
}

PageViewiOS 视图是这样调用的:

struct MainView: View {
    
    @EnvironmentObject var someState: AppStateService
    
    var body: some View {
        NavigationView {
            Group {                                                    
                if someState = .stateOne {

                    // CALL TO PAGEVIEWIOS IS HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    PageViewiOS()
                } else {
                    Text("hello")
                }
            }
        }
    }
}
4

3 回答 3

1

这是对我们对另一个答案的评论线程的回应:

class PlayerViewModel: ObservableObject {
    
    @Published var avPlayer: AVPlayer?
    
    func loadFromUrl(url: URL) {
        avPlayer = AVPlayer(url: url)
    }
}

struct CustomPlayerView: View {
    
    var url : URL
    @StateObject private var playerViewModel = PlayerViewModel()

    var body: some View {
        ZStack {
            if let avPlayer = playerViewModel.avPlayer {
                VideoPlayer(player: avPlayer)
            }
        }.onAppear {
            playerViewModel.loadFromUrl(url: url)
        }
    }
}

我不确定这是否确实更好,因此值得测试。但是,它确实控制何时AVPlayer创建并避免在父级的每个渲染上重新创建PlayerViewModel

于 2021-03-18T01:52:17.920 回答
0

我找到了解决方案。

调用以下命令:

CustomPlayerView(url: url)

...代替 :

VideoPlayer(player: AVPlayer(url: url))

不知道为什么这有效,很难。

也许有人可以进一步解释?

这是 CustomVideoPlayer 代码:

struct CustomPlayerView: View {

    private let url: URL

    init(url: URL) {
        self.url = url
    }

    var body: some View {
        VideoPlayer(player: AVPlayer(url: url))
    }
}

通过这个微小的变化,视频继续播放,即使父视图重新渲染也很困难。还是不知道为什么???

------------ 用@jnpdx 的提示回答--------

我更改变了 CustomVideoPlayer :

CustomPlayerView(playerViewModel: PlayerViewModel(avPlayer: AVPlayer(url: url)))
import SwiftUI
import AVKit

class PlayerViewModel: ObservableObject {
    
    @Published var avPlayer: AVPlayer
    
    init(avPlayer: AVPlayer) {
        self.avPlayer = avPlayer
    }
}

struct CustomPlayerView: View {
    
    @StateObject var playerViewModel: PlayerViewModel

    var body: some View {
        VideoPlayer(player: playerViewModel.avPlayer)
    }
}
于 2021-03-18T00:46:53.767 回答
0

使用@jnpdx 的解决方案,现在一切正常。

这是最终的解决方案(完全归功于@jnpdx):

import SwiftUI
import AVKit

class PlayerViewModel: ObservableObject {
    
    @Published var avPlayer: AVPlayer?
    
    func loadFromUrl(url: URL) {
        avPlayer = AVPlayer(url: url)
    }
}

struct CustomPlayerView: View {
    
    var url : URL
    @StateObject private var playerViewModel = PlayerViewModel()

    var body: some View {
        ZStack {
            if let avPlayer = playerViewModel.avPlayer {
                VideoPlayer(player: avPlayer)
            }
        }.onAppear {
            playerViewModel.loadFromUrl(url: url)
        }
    }
}

有了这些,像这样调用 CustomPlayerVideo 就足够了:

CustomPlayerView(url: url)

备注:我需要使用ZStack而不是Group在我的 CustomPlayerView 中才能使其工作。

于 2021-03-18T02:21:22.877 回答