简体   繁体   中英

Type 'string' has no member 'playback'

While trying to build a xylophone in xcode 10.1, using swift 4.2 on iOS12, i used a button to play a .wav file and entered the following code but the following error shows up:

"type 'String' has no member 'playback'"

func playSound() {
        guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return }

        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
            try AVAudioSession.sharedInstance().setActive(true)

            /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
            player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)

            /* iOS 10 and earlier require the following line:
             player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3) */

            guard let player = player else { return }

            player.play()

        } catch let error {
            print(error.localizedDescription)
        }
    }

I was looking around StackOverflow and I borrowed this piece of code which works, I don't know if its the conventional way of doing it though.

Swift 4.2

import AVFoundation
var player = AVAudioPlayer()

let url = URL(fileURLWithPath: Bundle.main.path(forResource: "note1", ofType: "wav")!)
    do {
        player = try AVAudioPlayer(contentsOf: url)
        player.play()
    } catch {
        print("couldn't load file :(")
    }
func playSound() {

    let soundURL = Bundle.main.url(forResource: selectedSoundFileName, withExtension: "wav")
    do{
        audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
        audioPlayer.play()
    } catch {

    }
}

This is another variation that you might be able to try! Let me know if this format works for you. I built a xylophone app myself a long time ago inside of a Udemy course. Pretty much would be easier to put the sound files directly into your project and run them through there, than to pull them from anywhere in my opinion.

this is the most "Swifty" way I could implement this for you.

If you click jump to definition on AVAudioSession Xcode will open the raw source file for the class and from there you can view the structure of each method in the class. This is helpful for viewing the structure of each function and for determining the data parameters of each function. Additionally there are annotations above each variation of the class functions compatible for different iOS deployment targets, indicated by the @available(iOS 11.0, *) annotations. We're focusing on the open func setCategory function in this class.

Helpful information in the raw source file

 Allowed categories: AVAudioSessionCategoryPlayback
 Allowed modes: AVAudioSessionModeDefault, AVAudioSessionModeMoviePlayback, AVAudioSessionModeSpokenAudio
 Allowed options: None. Options are allowed when changing the routing policy back to Default

I edited the category and mode parameters in my function as follows:

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [.mixWithOthers, .allowAirPlay])
    print("Playback OK")
    try AVAudioSession.sharedInstance().setActive(true)
    print("Session is Active")
} catch {
    print(error)
}

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