SRGSSR / srgmediaplayer-apple

An advanced media player library, simple and reliable
MIT License
159 stars 33 forks source link

Getting started docs in Swift #140

Open janwirth opened 2 hours ago

janwirth commented 2 hours ago

I'm fresh to Swift Development and have no ObjC experience (I have some web dev experience).

Would be great to get just a very basic working example.

I want to replace AVAudioPlayer

janwirth commented 2 hours ago

I managed to import this but I'm struggling to render the controller.view (UIView) inside of a content view.

janwirth commented 1 hour ago

struct PlayerView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> SRGMediaPlayerViewController {

        // Configure your player with content here if necessary
        playerController.showsPlaybackControls = true
//        playerController.publ
        return playerController
    }

    func updateUIViewController(_ uiViewController: SRGMediaPlayerViewController, context: Context) {
        // Handle updates, if needed
    }
}

struct ContentView: View {
    func onAppear () {
        print("playing")
        let url = "https://tuna.uber.space/01%20Chicago%20MF.mp3"
        var asset = AVURLAsset(url: URL(string: url)!)
        playerController.controller.playURLAsset(asset)

    }
    var currentTime: CMTime = playerController.controller.currentTime;
    var body: some View {

        VStack {
            Text("Player")

            PlayerView()
                .frame(height: 300)  // Adjust frame size as needed
        }
        .onAppear {
            print("ContentView appeared!")
            onAppear()
        }
        .onDisappear {
            print("ContentView disappeared!")
        }
    }
}```

Managed to do it with the above code, but now it's not clear how I customize the player to just be an audio player.
janwirth commented 1 hour ago

I think I will swift to ExAudioPlayer as it fits my use case (audio only) better.

defagos commented 42 minutes ago

Hi @janwirth,

SRG Media Player is no longer under active development. It was designed in a world where SwiftUI didn't exist and therefore requires extra work for proper integration (UI design with UIKit, wrapping into SwiftUI with representables).

If all you need is a way to play audio content and you are happy with the default system player UI, achieving what you want is pretty straightforward:

import AVFoundation
import AVKit
import SwiftUI

struct ContentView: View {
    @State private var player = AVPlayer(
        url: URL(string: "https://tuna.uber.space/01%20Chicago%20MF.mp3")!
    )

    var body: some View {
        VideoPlayer(player: player)
            .ignoresSafeArea()
            .onAppear(perform: player.play)
    }
}

#Preview {
    ContentView()
}

If you want to be able to create a custom audio playback user interface, though, things become much more involved.

During the past two years we have been working on a modern reactive media player, intended to be a first-class SwiftUI citizen. This player is currently part of our company ecosystem, called Pillarbox, and is available from one of its sub-packages. We sadly hadn't the time to separate this sub-package to make it standalone, but it can be seen as the true modern successor of SRG Media Player. Our goal is to make it standalone in the future, but we cannot promise anything since we are a small team with limited bandwidth.

I don't encourage you to consider adding dependencies to your project if your needs are simple (in which case the snippet above is fine), but if you are interested you should have a look at our documentation, available on Swift Package Index. Creating a custom UI with this new player should be a piece of cake.

Hope that helps.