Je ne suis pas un programmeur IOS mais j'ai besoin d'une application simple pour lire un flux audio à partir d'un serveur wowza. Voici mon code, je n’obtiens aucune erreur mais également aucun son. Des conseils ou de l'aide?
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var fileURL: NSURL = NSURL(string: "http://url----.com/playlist.m3u8")
var error:NSError?
player = AVAudioPlayer(contentsOfURL: fileURL, error: &error)
if player.prepareToPlay() && player.play(){
println("yes")
} else {
println("no")
}
}
Cela a fonctionné pour moi:
var player = AVPlayer()
func configureView() {
let url = "http://stream.your.url.com"
let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
player = AVPlayer(playerItem:playerItem)
player.rate = 1.0;
player.play()
}
Dans Swift 2.0 et iOS 9
do {
let url = "http://yourdomain.com/file.mp3"
let fileURL = NSURL(string:url)
let soundData = NSData(contentsOfURL:fileURL!)
self.audioPlayer = try AVAudioPlayer(data: soundData!)
audioPlayer.prepareToPlay()
audioPlayer.volume = 1.0
audioPlayer.delegate = self
audioPlayer.play()
} catch {
print("Error getting the audio file")
}
Cette réponse m'a aidé: AVAudioPlayer avec une URL externe vers * .m4p
J'ai dû convertir l'URL à NSData:
let url = "http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"
let fileURL = NSURL(string:url)
let soundData = NSData.dataWithContentsOfURL(fileURL, options: nil, error: nil)
var error: NSError?
self.player = AVAudioPlayer(data: soundData, error: &error)
if player == nil {
if let e = error {
println(e.localizedDescription)
}
}
player.prepareToPlay()
player.volume = 1.0
player.delegate = self
player.play()
S'il vous plaît garder à l'esprit depuis iOS 9: App Transport Security (ATS) configuré?
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
Swift 2:
let url = "http://yourserver.com/source.mp3"
let playerItem = AVPlayerItem(URL:NSURL(string:url)!)
audio = AVPlayer(playerItem:playerItem)
audio.rate = 1.0;
audio.play()
utilisez AVPlayer:
var player:AVPlayer!
var steamingURL:NSURL = NSURL(string:playerURL)
player = AVPlayer(URL: steamingURL)
player.play()
// Créer une instance d'AVPlayer globalement
var avPlayer:AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"
let url = NSURL(string: urlString)!
avPlayer = AVPlayer(url: url as URL)
print("About to play...")
avPlayer.play()
print("isplaying")
}
Pour rechercher une erreur dans Swift, utilisez:
if let errorDetails = error {
println("Error creating player: \(errorDetails.localizedDescription)")
} else {
if player.prepareToPlay() && player.play(){
println("yes")
} else {
println("no")
}
}