I am trying to create a view like Audio message view as shows in WhatsApp.
Player code.
struct AudioPlayerControlsView: View {
private enum PlaybackState: Int {
case waitingForSelection
case buffering
case playing
}
let player: AVPlayer
let timeObserver: PlayerTimeObserver
let durationObserver: PlayerDurationObserver
let itemObserver: PlayerItemObserver
@State private var currentTime: TimeInterval = 0
@State private var currentDuration: TimeInterval = 0
@State private var state = PlaybackState.waitingForSelection
var body: some View {
VStack {
if state == .waitingForSelection {
Text("Select a song below")
} else if state == .buffering {
Text("Buffering...")
} else {
Text("Great choice!")
}
Slider(value: $currentTime,
in: 0...currentDuration,
onEditingChanged: sliderEditingChanged,
minimumValueLabel: Text("\(Utility.formatSecondsToHMS(currentTime))"),
maximumValueLabel: Text("\(Utility.formatSecondsToHMS(currentDuration))")) {
// I have no idea in what scenario this View is shown...
Text("seek/progress slider")
}
.disabled(state != .playing)
}
.padding()
// Listen out for the time observer publishing changes to the player's time
.onReceive(timeObserver.publisher) { time in
// Update the local var
self.currentTime = time
// And flag that we've started playback
if time > 0 {
self.state = .playing
}
}
// Listen out for the duration observer publishing changes to the player's item duration
.onReceive(durationObserver.publisher) { duration in
// Update the local var
self.currentDuration = duration
}
// Listen out for the item observer publishing a change to whether the player has an item
.onReceive(itemObserver.publisher) { hasItem in
self.state = hasItem ? .buffering : .waitingForSelection
self.currentTime = 0
self.currentDuration = 0
}
// TODO the below could replace the above but causes a crash
// // Listen out for the player's item changing
// .onReceive(player.publisher(for: \.currentItem)) { item in
// self.state = item != nil ? .buffering : .waitingForSelection
// self.currentTime = 0
// self.currentDuration = 0
// }
}
// MARK: Private functions
private func sliderEditingChanged(editingStarted: Bool) {
if editingStarted {
// Tell the PlayerTimeObserver to stop publishing updates while the user is interacting
// with the slider (otherwise it would keep jumping from where they've moved it to, back
// to where the player is currently at)
timeObserver.pause(true)
}
else {
// Editing finished, start the seek
state = .buffering
let targetTime = CMTime(seconds: currentTime,
preferredTimescale: 600)
player.seek(to: targetTime) { _ in
// Now the (async) seek is completed, resume normal operation
self.timeObserver.pause(false)
self.state = .playing
}
}
}
}
Now I am trying to add it to cells of list in SwiftUI.
struct Audiomessage : Identifiable {
var id: UUID
var url : String
var isPlaying : Bool
}
struct ChatView : View {
let player = AVPlayer()
private let items = [ Audiomessage(id: UUID(), url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", isPlaying: false),Audiomessage(id: UUID(), url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3", isPlaying: false)]
var body: some View {
VStack{
LazyVStack {
ForEach(items) { reason in
AudioPlayerControlsView(player: player,
timeObserver: PlayerTimeObserver(player: player),
durationObserver: PlayerDurationObserver(player: player),
itemObserver: PlayerItemObserver(player: player)).onTapGesture {
guard let url = URL(string: reason.url) else {
return
}
let playerItem = AVPlayerItem(url: url)
self.player.replaceCurrentItem(with: playerItem)
self.player.play()
}
}
}
.padding(.bottom,37)
.padding()
}
.padding(.horizontal,10)
}
}
The out for this is like below:

Both starts playing on Tap. I want to play one song at a time. How Can I achieve this in SwiftUI?