iOS如何播放直播电台流媒体URL?我的电台URL在Xcode项目无法播放
Hey there, let’s work through getting your live radio stream http://s2.yesstreaming.net:7091/stream playing in your Xcode project. I’ve debugged plenty of iOS streaming issues, so here’s a step-by-step breakdown of what’s likely going wrong and how to fix it:
iOS’s AVPlayer (the standard framework for media playback) supports formats like MP3, AAC, and HLS out of the box. To check what format your stream uses, run this command in Terminal (you’ll need ffmpeg installed):
ffprobe http://s2.yesstreaming.net:7091/stream
Look for the Audio section—if it’s MP3 or AAC, you’re good to go. If it’s a less common format (like Ogg Vorbis), you’ll need a third-party library to handle decoding.
Your stream uses HTTP, but iOS’s App Transport Security (ATS) blocks unencrypted HTTP requests by default. To allow access to this specific domain, add an exception to your Info.plist:
- Open your project’s
Info.plistfile - Add a new dictionary called
NSAppTransportSecurity - Inside that, add another dictionary called
NSExceptionDomains - Add a key for
yesstreaming.net, then set these values:NSIncludesSubdomains→ Boolean → YESNSTemporaryExceptionAllowsInsecureHTTPLoads→ Boolean → YES
Here’s the raw XML if you prefer to paste it directly:
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>yesstreaming.net</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> </dict> </dict> </dict>
This is safer than enabling arbitrary HTTP loads for your entire app.
AVPlayer with Error Handling A basic implementation with status tracking will help you debug why playback fails. Here’s a Swift example you can drop into a view controller:
import UIKit import AVFoundation class RadioPlayerViewController: UIViewController { private var player: AVPlayer? private var playerItem: AVPlayerItem? override func viewDidLoad() { super.viewDidLoad() setupRadioPlayer() } private func setupRadioPlayer() { guard let streamURL = URL(string: "http://s2.yesstreaming.net:7091/stream") else { print("Oops, invalid stream URL") return } playerItem = AVPlayerItem(url: streamURL) player = AVPlayer(playerItem: playerItem) // Add observer to track playback status playerItem?.addObserver(self, forKeyPath: "status", options: .new, context: nil) // Start playback player?.play() } // Handle status updates to debug issues override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { guard keyPath == "status", let item = object as? AVPlayerItem else { return } switch item.status { case .readyToPlay: print("Stream is rolling!") case .failed: print("Playback failed: \(item.error?.localizedDescription ?? "Unknown error")") case .unknown: print("Waiting for stream to connect...") @unknown default: break } } // Clean up observers when the view controller is deallocated deinit { playerItem?.removeObserver(self, forKeyPath: "status") } }
Key notes here:
- Make sure you’ve imported
AVFoundationin your file - The status observer will tell you exactly why playback fails (e.g., network error, unsupported format)
- Test on a real device if possible—simulators sometimes have weird network or media decoding quirks
AVPlayerViewController To rule out issues with your custom player code, try using Apple’s built-in AVPlayerViewController:
import AVKit // Call this from your view controller (e.g., a button tap) private func showSystemPlayer() { guard let streamURL = URL(string: "http://s2.yesstreaming.net:7091/stream") else { return } let player = AVPlayer(url: streamURL) let playerVC = AVPlayerViewController() playerVC.player = player present(playerVC, animated: true) { player.play() } }
If this works, the problem is in your custom player setup. If not, it’s likely an ATS, format, or network issue.
- Open the stream URL in Safari on your iOS device—if it doesn’t play there, the issue is with the stream itself or your network
- Some networks block non-standard ports (your stream uses port 7091). Try switching to a different network (like cellular) to test.
内容的提问来源于stack exchange,提问作者Jaydip Godhani

