2017-08-15 56 views
0

我的所有其他音頻文件都可以通過音頻播放器完美播放。大多數文件長度不到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

這是什麼導致沉默?

+0

我遇到過問題,在文件的編碼問題阻止播放。我重新編碼了這個文件,之後它對我有效。可能值得一試? – CodeBender

+0

幾個小時後回家,我會試試這個。 – SpiffyStache

+0

我剛剛嘗試添加2個類似長度的音頻文件,並用它們替換sortSong,它們都不會播放:(。我也嘗試使用相同的編碼並且播放的音頻文件更短(4秒)。 – SpiffyStache

回答

0

你應該在你的問題上寫上正確的標題。

您的密碼不使用AVAudioPlayer,但使用System Sound Services

的文件明確指出其侷限性:

您可以使用系統聲音服務播放短(30秒或 較短)聲音。

我從來沒有檢查過,如果它的限制恰好是30秒,但它符合你的問題中的描述。

考慮使用AVAudioPlayer(如在標題中)播放更長的聲音。