article

Tuesday, September 14, 2021

SwiftUI Simple Youtube Design with Video Player AVKit

SwiftUI Simple Youtube Design with Video Player AVKit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
//  ContentView.swift
//  swiftuidev
//
//  Created by Cairocoders
//
 
import SwiftUI
import AVKit
 
struct ContentView: View {
     
    var body: some View {
        GeometryReader { geo in
            ZStack {
                 
                if UIDevice.current.orientation.isLandscape {
                    Color.black
                }
                 
                VStack {
                    player().frame(height: UIDevice.current.orientation.isLandscape ? geo.size.height : geo.size.height / 3)
                        .edgesIgnoringSafeArea(.all)
                     
                    if !UIDevice.current.orientation.isLandscape {
                        Main()
                    }
                         
                         
                }
            }
        }
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView() 
    }
}
 
struct player : UIViewControllerRepresentable {
     
    func makeUIViewController(context: UIViewControllerRepresentableContext<player>) -> AVPlayerViewController {
         
        let player1 = AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!)
         
        let controller = AVPlayerViewController()
        controller.player = player1
        return controller
    }
     
    func updateUIViewController(_ uiViewController : AVPlayerViewController, context: UIViewControllerRepresentableContext<player>) {
         
         
    }
}
 
struct Main : View {
    var body: some View {
         
        ScrollView(.vertical, showsIndicators: false) {
            VStack {
                 
                subscriber()
                 
                ForEach(1..<5){i in
                     
                    VideoCard(image: "\(i)", name: "Pinoy Food Recipes \(i)")
                }
            }
        }.padding(10)
         
 
    }
}
 
struct subscriber : View {
    var body: some View {
         
        HStack {
            Image("photo1")
                .resizable()
                .clipShape(Circle())
                .frame(width: 50, height: 50)
             
            VStack (alignment: .leading, content: {
                Text("Cairocoders")
                    .fontWeight(.heavy).fixedSize()
                Text("5869 Subscribers")
                    .fixedSize()
            })
             
            Spacer()
             
            Button(action: {
                 
            }) {
                Text("Subscribe").fontWeight(.heavy)
            }.foregroundColor(.red)
        }
    }
}
 
struct VideoCard : View {
     
    var image = ""
    var name = ""
     
    var body: some View {
         
        HStack {
            Image(image).resizable().frame(width: 120, height: 80)
             
            VStack(alignment: .leading, content: {
                Text(name).fontWeight(.heavy)
                Text("Cairocoders")
                Text("5689 View")
            }).frame(width: UIScreen.main.bounds.width - 170, alignment: .leading)
             
            VStack {
                Button(action: {
                     
                }) {
                    Image(systemName: "list.dash").resizable().frame(width: 15, height: 15).foregroundColor(.black)
                    Spacer()
                }.padding(.top, 5)
            }
        }
    }
}

Related Post