Reputation: 19
I've been working on an audio player project and recently discovered a memory leak. After spending several days debugging without success, I decided to create a simplified project using AVPlayer to analyze the issue further. `
import SwiftUI
import AVKit
struct ContentView: View { @State private var player: AVPlayer? = nil
var body: some View {
VStack {
Text("Audio Player")
.font(.title)
.padding()
if player != nil {
Button(action: {
if player?.timeControlStatus == .playing {
player?.pause()
} else {
player?.play()
}
}) {
Text(player?.timeControlStatus == .playing ? "Pause" : "Play")
.font(.headline)
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
} else {
Text("Loading Audio...")
}
}
.onAppear {
let url = URL(string: "https://d12wklypp119aj.cloudfront.net/track/86d38d9a-2f4b-44ae-a77f-a72e034f6d54.mp3")!
player = AVPlayer(url: url)
}
.onDisappear {
player?.pause()
}
}
}
Interestingly, I found that even this smaller project is experiencing memory leaks! Now, I'm wondering if this is a bug with AVPlayer, or if there's something wrong with my implementation.
Has anyone else encountered similar issues with AVPlayer? What steps would you recommend for identifying and fixing the leak?
Any insights would be greatly appreciated. Thanks in advance! enter image description here enter image description here
Upvotes: 0
Views: 71