使用swift在iOS中播放mp3时没有声音

时间:2023-01-24 08:31:19

There is no error message when playing the sound. I put a print statement for testing, and the url is getting the right path. The play button clicks, and I hear a faint click sometimes, but that's it. No other sound comes out. I've tried different mp3 files. I get an error message if I delete the mp3 file. I've checked the volume.

播放声音时没有错误信息。我输入一个print语句进行测试,url得到了正确的路径。播放按钮咔哒一声,我有时会听到微弱的咔哒声,但仅此而已。没有其他声音出来。我试过不同的mp3文件。如果我删除mp3文件,我得到一个错误信息。我已经检查了体积。

The play button is in a second view controller. The main view controller has a table view. When user taps on the cell, it takes him to this view controller.

play按钮在第二个视图控制器中。主视图控制器有一个表视图。当用户点击单元格时,它会带他到这个视图控制器。

//  DetailVC.swift

import AVFoundation
import UIKit

class DetailVC: UIViewController {

    var duaPlayer = ""  //is this correct???


    override func viewDidLoad() {
        super.viewDidLoad()

    }



    @IBAction func playbutton(sender: AnyObject) {

        do {
        let url = NSBundle.mainBundle().URLForResource("track1", withExtension: "mp3")

      var duaPlayer = try AVAudioPlayer(contentsOfURL: url!)

            duaPlayer.prepareToPlay()
            print (url)
            duaPlayer.volume = 0.5

            duaPlayer.play()
        }

        catch
        { fatalError("err")}

    }

1 个解决方案

#1


2  

i think that AVAudioPlayer do not support streaming. For live streaming use AVPlayer

我认为AVAudioPlayer不支持流媒体。对于实时流媒体,请使用AVPlayer

//try this, it should work
var player = AVPlayer()
func configureView() {
    let url = "http://yoururl.com"
    let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
    player = AVPlayer(playerItem:playerItem)
    player.rate = 1.0;
    player.play()
}

in the other hand , with AVAudioPlayer you can download the mp3 file first than play it -->

另一方面,使用AVAudioPlayer你可以先下载mp3文件,而不是播放它——>

let url = "http://YourURL/music/music.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()

PS: Please keep in mind since iOS 9: App Transport Security (ATS) must be configured to accept non ssl connection. add this code to your info.plist->

请记住,iOS 9:应用程序传输安全(ATS)必须配置为接受非ssl连接。将此代码添加到info.plist->中

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

#1


2  

i think that AVAudioPlayer do not support streaming. For live streaming use AVPlayer

我认为AVAudioPlayer不支持流媒体。对于实时流媒体,请使用AVPlayer

//try this, it should work
var player = AVPlayer()
func configureView() {
    let url = "http://yoururl.com"
    let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
    player = AVPlayer(playerItem:playerItem)
    player.rate = 1.0;
    player.play()
}

in the other hand , with AVAudioPlayer you can download the mp3 file first than play it -->

另一方面,使用AVAudioPlayer你可以先下载mp3文件,而不是播放它——>

let url = "http://YourURL/music/music.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()

PS: Please keep in mind since iOS 9: App Transport Security (ATS) must be configured to accept non ssl connection. add this code to your info.plist->

请记住,iOS 9:应用程序传输安全(ATS)必须配置为接受非ssl连接。将此代码添加到info.plist->中

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>