Search code examples
iosswiftsdkbundle

In my own SDK, how do I play a mp3 of this SDK?


I use swift to write the SDK myself. There is an mp3 file in the SDK, and I want to play the mp3 in the SDK. I realized that it is no longer possible to use:

Bundle.main.url(forResource: "sound_tap", withExtension: "mp3")

On the following sites I found it seems to work:

Bundle.module.url(forResource: "sound_tap", withExtension: "mp3")

apple developer

But:

"Bundle.module" cannot be used because "Type 'Bundle' has no member 'module'".

So how do I play the mp3 of this SDK in my own SDK? Thanks for any help.

Swift 5, iOS11


Solution

  • You need to create a Bundle target for files like video, images or xib, then link your mp3 file to this bundle target.

    How to create a bundle target

    Use following code to access the file:

    let bundlePath = Bundle.main.path(forResource: "YOUR-BUNDLE-NAME", ofType: "bundle")
    let bundle = Bundle(path: bundlePath)
    let filePath = bundle.path(forResource: "YOUR-FILE-NAME", ofType:"mp3")
    

    Play video file:

    let player = AVPlayer(url: URL(fileURLWithPath: filePath))
    ....
    player.play()