簡體   English   中英

AVAudioPlayer不會播放一個mp3的音頻,但是會播放其他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()
}

這是調用成功排序函數的動作

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

如果我只是簡單地將sortSongPlay更改為=“ shortSortSong”(23秒版本),它就可以正常運行。

我所有的聲音文件都為此項目文件檢查了其目標成員身份,並且所有文件都具有正確的路徑。 如果按下播放按鈕,音頻將在界面構建器中播放。 沒有編譯器錯誤或警告,應用程序永不崩潰,在應用程序中調用sortSong時,音頻根本沒有播放。

這是一個包含我在項目中嘗試過的示例的鏈接。 前兩個聲音在應用程序中靜音播放,而較短的聲音則完美播放。 https://soundcloud.com/spiffystache

是什么導致這種沉默?

您應該在問題上輸入正確的標題。

您的代碼不使用AVAudioPlayer ,而是使用系統聲音服務

該文檔明確指出了其局限性:

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

我從未檢查過它的限制是否恰好是30秒,但它與您問題中的描述相符。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM