Play Radio link within the App

I am usign Xcode 15.2. I have been following CodewithChris for a while on youtube. I have managed to crete iunterface for my radio app but i have failed on how to play the radio station link to the app. I need help.

///. import SwiftUI
import AVFoundation
import UIKit

struct ContentView: View {
    var body: some View {
        
        ZStack {

            Image("RadioB2")
                .resizable()
                .ignoresSafeArea()
            
                .background(Rectangle()
                    .foregroundColor(.black))
                .ignoresSafeArea()
            
            VStack {
                
                
                Menu(/*@START_MENU_TOKEN@*/"Menu"/*@END_MENU_TOKEN@*/)
                {
                    /*@START_MENU_TOKEN@*/Text("Menu Item 1")/*@END_MENU_TOKEN@*/
                    /*@START_MENU_TOKEN@*/Text("Menu Item 2")/*@END_MENU_TOKEN@*/
                    /*@START_MENU_TOKEN@*/Text("Menu Item 3")
                        .padding(.trailing)/*@END_MENU_TOKEN@*/
                    
                }
                .padding(.trailing, 250.0)
                .foregroundColor(.white)
                
                Image("logo")
                    .imageScale(.small)
                    .foregroundStyle(.tint)
                
                Text("Global Evangelical Church")
                    .bold()
                    .foregroundColor(.white)
                    .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                
                Spacer()
                
                VStack {
                    
                    
                    HStack {
                        
                        Link(destination: URL(string: "https://www.youtube.com/channel/UC4tYNnpAyq8qBTqTajJkc3w")!, label: {
                            Text("Youtube")
                        })
                        
                        .padding(.trailing, 80.0)
                        .bold()
                        .foregroundColor(.white)
                        .navigationBarBackButtonHidden()
                        
                        Link(destination: URL(string: "https://www.facebook.com/globalchurchus")!, label: {
                            Text("FaceBook")
                        })
                        
                        .padding(.leading, 80.0)
                        .bold()
                        .foregroundColor(.white)

                        
                    }

                    Button {
                      
                        func playbutton(){
                        let urlString = Bundle.main.url(forResource: "https://noasrv.caster.fm:2199/start/opztdowk%22", withExtension: "https")
                        }
                    }
                
                label: {
                        
                        Image("PLAYRADIO")
                    }
                    
                    
                }
            }
            .padding()
        }
    }
}

#Preview {
    ContentView()
}

@dbway091

Hi David,

Welcome to the community.

When you use Bundle.main..., it refers to resources that are loaded into the Application and remain as static data as part of the Application Bundle. For instance let’s say you had an MP3 file that you wanted to play from within the App. You would drag it into the Bundle and then access it using, for example, if the filename was yourMusicFile.mp3 then you would do this:

let url = Bundle.main.url(forResource: "yourMusicFile", withExtension: "mp3")

and then use the AVAudioPlayer with the appropriate code to play that file.

What you are trying to do it play a resource that is external to the Application so Bundle.main will not help you there. Probably what you should do is use the Link facility that opens the destination in Safari where you can then tap on the play button.

ie: instead of your Button where you have the PLAYRADIO image use the link like this:

Link(destination: URL(string: "https://noasrv.caster.fm:2199/start/opztdowk")!,
     label: {
    Image("PLAYRADIO")
})

That takes you to this website which I assume is the correct one.

Thank you, Chris - For the correction. I appreciate it and that has helped me. what I am trying to do is to have the link play within the app. I have a few links and from my research, i think the one with .pls would work?

////Winamp, iTunes|https://noasrv.caster.fm:2199/tunein/opztdowk.pls|