简体   繁体   中英

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

All of my other audio files play perfectly through the audio player. Most of the files are less than 5 seconds long, and the longest file that still plays is 23 seconds long. For some reason my 1:15 long file "sortSong" is completely silent when it is called.

Here is the code for my audio player:

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]!)

    }
}

The sound is played when this function is called in my view controller.

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

This is the action that calls the successfulSort func

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

If I simply change the case sortSongPlay to = "shortSortSong" (the 23 second version) it plays just fine.

All of my sound files have their target memberships checked for this project file, and all of the files have the correct path. The audio will play in the interface builder if I press the play button. There are no compiler errors or warnings, the app never crashes, the audio for sortSong simply isn't playing when it is called in the app.

This is a link containing examples I have tried on my project. The first two sounds play silently in the app while the shorter sounds all play perfectly. https://soundcloud.com/spiffystache

What is causing this to be silent?

You should put a correct title on your question.

Your code does not use AVAudioPlayer , but uses System Sound Services .

The documentation clearly states its limitation:

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

I have never checked if its limitation is exactly 30 seconds, but it matches the description in your question.

Consider using AVAudioPlayer (as in the title) to play longer sound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM