AVAudioPlayer不会为一个mp3播放音频,但会为其他人播放音频?

时间:2017-08-15 15:24:27

标签: ios swift xcode audio avaudioplayer

我的所有其他音频文件都通过音频播放器完美播放。大多数文件的长度不到5秒,而仍然播放的最长文件长度为23秒。出于某种原因,我的1:15长文件“sortSong”在被调用时完全无声。

以下是我的音频播放器的代码:

import Foundation
import AVFoundation

class AudioPlayer {

    static let sharedInstance = AudioPlayer()

    enum Sound: String {
        case sortSongPlay = "sortSong"
        case centerButtonRelease = "centerButtonRelease"
        case buttonTap = "tapSound"
        static var all: [Sound] {
            return [.centerButtonRelease, .buttonTap, sortSongPlay]
        }

        var url: URL {
            return URL.init(fileURLWithPath: Bundle.main.path(forResource: self.rawValue, ofType: "mp3")!)
        }
    }

    private var soundMap = [String: SystemSoundID]()

    init() {
        for sound in Sound.all {
            let soundUrl = sound.url
            var soundId: SystemSoundID = 0
            AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId);
            soundMap[sound.rawValue] = soundId
        }
    }

    func play(sound: Sound) {
        AudioServicesPlaySystemSound(soundMap[sound.rawValue]!)

    }
}

在我的视图控制器中调用此功能时播放声音。

func successfulSort() {
    AudioPlayer.sharedInstance.play(sound: .sortSongPlay)
    rotateSortImageOne()
    rotateSortImageTwo()
}

这是调用successfulSort func

的动作
  if inputText == "hello" && seedArray == [1, 4, 1] {
    successfulSort()
}

如果我只是将sortSongPlay改为=“shortSortSong”(23秒版本),它就可以正常播放。

我的所有声音文件都为此项目文件检查了目标成员资格,并且所有文件都具有正确的路径。如果按下播放按钮,音频将在界面构建器中播放。没有编译器错误或警告,应用程序永远不会崩溃,sortSong的音频在应用程序中调用时根本无法播放。

这是一个链接,其中包含我在项目中尝试过的示例。前两个声音在应用程序中默默播放,而较短的声音完全播放。 https://soundcloud.com/spiffystache

是什么导致这种沉默?

1 个答案:

答案 0 :(得分:0)

你应该在你的问题上加上正确的标题。

您的代码不使用AVAudioPlayer,而是使用System Sound Services

文件明确说明了其局限性:

  

您可以使用系统声音服务播放短片(30秒或以上)   更短的声音。

我从未检查过它的限制是否恰好是30秒,但它与你问题中的描述相符。

考虑使用AVAudioPlayer(如标题中所示)播放更长的声音。