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

AVAudioPlayer will not play audio for one mp3 but will for others?

我的所有其他音频文件都可以通过音频播放器完美播放。大多数文件的长度都不到 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 函数的操作

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

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

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

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

是什么导致这里没有声音?

你应该给你的问题加上一个正确的标题。

您的代码没有使用 AVAudioPlayer,而是使用了 System Sound Services

文档清楚地说明了它的局限性:

You can use System Sound Services to play short (30 seconds or shorter) sounds.

我从来没有检查过它的限制是否恰好是 30 秒,但它符合你问题中的描述。

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