Search code examples
audioswiftuiaudio-streaming

How to play audio from an http data stream in swiftUI?


I develop now RadioStream application. I use the SwiftAudioPLayer framework, but after some seconds player stoped. So maybe it's a different way to play audio from an HTTP stream in swiftUI. Or do I need to migrate code from Uikit to swiftUI?

I am new in swiftUI and iOS development and appreciate any response in advance. ThankYou!

Code for SwiftAudioPlayer framework, code source: https://github.com/tanhakabir/SwiftAudioPlayer#realtime-audio-manipulation***

How can I play the radio stream URL file in swiftUI?

 
    import SwiftUI
    import SwiftAudioPlayer
    
    
     func playMusic() {
            let url = URL(string:"http://online.radiorecord.ru:8102/rus_128")!
            SAPlayer.shared.startRemoteAudio(withRemoteUrl: url)
            SAPlayer.shared.play()
        }
    }

Solution

  • Use https instead of the http you are using, because:

    "iOS 9.0 and macOS 10.11 and later use App Transport Security (ATS) for all HTTP connections made with URLSession. ATS requires that HTTP connections use HTTPS (RFC 2818)."

    however;

    "You can circumvent or augment these protections by adding the NSAppTransportSecurity key to your app’s Information Property List file and providing an ATS configuration dictionary as the value."

    That is adding exceptions in the Info.plist file.

    see "https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity"