You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. First, Confirm the Stream’s Format is iOS-Compatible

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.

2. Fix ATS Restrictions (The Most Common Culprit)

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:

  1. Open your project’s Info.plist file
  2. Add a new dictionary called NSAppTransportSecurity
  3. Inside that, add another dictionary called NSExceptionDomains
  4. Add a key for yesstreaming.net, then set these values:
    • NSIncludesSubdomains → Boolean → YES
    • NSTemporaryExceptionAllowsInsecureHTTPLoads → 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.

3. Implement 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 AVFoundation in 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
4. Quick Test with 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.

5. Check Network and Port Access
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:00:29