我的所有其他音频文件都可以通过音频播放器完美播放。大多数文件长度不到5秒,而最长的文件仍然播放的时间为23秒。出于某种原因,我的1:15长文件“sortSong”在被调用时完全没有声音。AVAudioPlayer不会为一个mp3播放音频,但会为其他人播放音频吗?
这里是我的音频播放器的代码:
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
这是什么导致沉默?
我遇到过问题,在文件的编码问题阻止播放。我重新编码了这个文件,之后它对我有效。可能值得一试? – CodeBender
几个小时后回家,我会试试这个。 – SpiffyStache
我刚刚尝试添加2个类似长度的音频文件,并用它们替换sortSong,它们都不会播放:(。我也尝试使用相同的编码并且播放的音频文件更短(4秒)。 – SpiffyStache