web-dev-qa-db-de.com

IOS 8 Swift AVAudioPlayer zur Wiedergabe von Remote-Audio (Wowza-Server)

Ich bin kein IOS -Programmierer, aber ich brauche eine einfache App, um einen Audio-Stream von einem Wowza-Server abzuspielen. Hier ist mein Code, ich bekomme keine Fehler aber auch kein Audio. Irgendwelche Anleitungen oder Hilfe?

    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")
            }
        }
15
David Turton

Das hat für mich funktioniert:

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()

}
20
2075

In Swift 2.0 und 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")
}
9
Jsonras

Diese Antwort hat mir geholfen: AVAudioPlayer mit externer URL zu * .m4p

Ich musste die URL in NSData konvertieren:

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()
2
Billy Coover

Bitte denken Sie daran, seit iOS 9: App Transport Security (ATS) konfiguriert?

<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()
1
Just van Bloom

aVPlayer verwenden:

var player:AVPlayer!
var steamingURL:NSURL = NSURL(string:playerURL)
player = AVPlayer(URL: steamingURL)
player.play()
0
Ramesh

// AVPlayer-Instanz global erstellen

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")
}
0

Um nach Fehlern in Swift zu suchen, verwenden Sie:

if let errorDetails = error {
    println("Error creating player: \(errorDetails.localizedDescription)")
} else {
    if player.prepareToPlay() && player.play(){
        println("yes")
    } else {
        println("no")
    }
}
0
Raptor