1
Runt
2025-06-27 bf9e4680eb466bcb7c9cb1bef567252bb1f2bb7d
LiveProject/activity/stream/LiveActivity.swift
@@ -7,19 +7,119 @@
import Foundation
import SwiftUI
import WrappingHStack
struct LiveActivity: View {
    @State private var pixelBuffer: CVPixelBuffer?
    @State private var showDeviceDialog = false
    @State private var streamRate = Float(9/16.0);
    @State private var mainSize: CGSize = .init(width: 100, height: 100)
    @State private var devices = [DeviceInfo(name: "相机", type: .CAMERA(), deviceId: "相机"),
                                  DeviceInfo(name: "话筒", type: .MICROPHONE(),deviceId: "话筒"),
                                  DeviceInfo(name: "系统", type: .SYSTEM(),deviceId : "系统")]
    var body: some View {
        NavigationView{
            ZStack{
            Color.clear
                .ignoresSafeArea() // 填满全屏
            VStack{
                VideoRendererView(renderer: MetalRenderer()).background(Color.black).frame(width: mainSize.width,height:mainSize.height)
                Spacer()
            }.border(Color.blue)
            VStack{
                Spacer()
                BottomBtns().frame(alignment: .bottom).border(Color.green)
            }
            if showDeviceDialog {
                DialogDevices()
            }
        }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
            .background(
                GeometryReader { geometry in
                    Color.clear
                        .onAppear {
                            updateWindowSize(width: Int(geometry.size.width), height: Int(geometry.size.height))
                        }
                        .onChange(of: geometry.size) { newSize in
                            updateWindowSize(width: Int(newSize.width), height: Int(newSize.height))
                        }
                })
            .border(Color.red)
            .onDisappear {
                print("onDisappear 视图消失了!")
                
            }
        }
            }.onAppear {
                print("onAppear 视图出现了!")
    }
}
struct Bottombtns:View {
    var body: some View {
    func updateWindowSize(width:Int,height:Int){
        var rate : Float = Float(width)/Float(height);
        if(rate != streamRate) {
            var mainWidth = 0;
            var mainHeight = 0;
            if(rate < streamRate){
                mainWidth = width;
                if(9.0/16 == streamRate){
                    mainHeight = (mainWidth / 9 * 16);
                }else{
                    mainHeight = (mainWidth / 16 * 9);
                }
            }else{
                mainHeight = height;
                if(9.0 / 16 == streamRate){
                    mainWidth = (mainHeight / 16 * 9);
                }else{
                    mainWidth = (mainHeight / 9 * 16);
                }
            }
            if(mainSize.width != CGFloat(mainWidth) || mainSize.height != CGFloat(mainHeight)){
                mainSize = .init(width: mainWidth, height: mainHeight);
            }
        }else{
            if(mainSize.width != CGFloat(width) || mainSize.height != CGFloat(height)){
                mainSize = .init(width: width, height: height);
            }
        }
        //Log.w(TAG , "onSizeChanged: ${mainWindowSize.value}" , )
    }
    func DialogDevices() -> some View{
        ZStack{
            Color.black.opacity(0.4)
                .edgesIgnoringSafeArea(.all)
                .onTapGesture {
                    withAnimation {
                        showDeviceDialog = false
                    }
                }
            VStack {
                Spacer()
                VStack(spacing: 20) {
                    Spacer().frame(height:40)
                    FlowLayout(devices){ device in
                        MButton(text:device.name){
                        }
                    }
                    .padding()
                    .animation(.default, value: devices)
                }
                .frame(maxWidth: .infinity)
                .padding()
                .background(Color.white)
                .cornerRadius(20)
                .transition(.move(edge: .bottom))
            }
            .zIndex(1)
        }
    }
    func BottomBtns() -> some View{
        VStack{
            
            HStack(){
@@ -35,14 +135,17 @@
            }
            HStack{
                LButton(text: "设备"){
                    print("Click 设备 button")
                    withAnimation{
                        showDeviceDialog.toggle()
                    }
                }
                LButton(text: "RTMP"){
                    
                }
                LButton(text: "文件"){
                /*flLButton(text: "文件"){
                    
                }
                }*/
                LButton(text: "文本"){
                    
                }
@@ -56,9 +159,10 @@
    }
}
struct LiveActivity_Previews: PreviewProvider{
struct LiveActivity_BottomBtns_Previews: PreviewProvider{
    static var previews: some View {
        Bottombtns();
        LiveActivity();
    }
    
}