App crashes when playing audio on iOS13.1

前端 未结 3 398
无人及你
无人及你 2020-12-29 06:41

I am building an app that runs sound files from within the main bundle with a url. When I tested this on iOS 13, everything is fine. But with the new update of 13.1 I am get

相关标签:
3条回答
  • 2020-12-29 07:06

    Change this:

    var backgroundMusicPlayer = AVAudioPlayer()
    

    To this:

    var backgroundMusicPlayer : AVAudioPlayer!
    
    0 讨论(0)
  • 2020-12-29 07:17

    Add below code in AppDelegate.swift

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
    
        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSession.Category.playback)
        }
        catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed.")
        }
        return true
    }
    
    0 讨论(0)
  • 2020-12-29 07:18

    AVAudioPlayer doesn't have an init so it should be removed.

    Solution for swift

    If you initialise your AVAudioPlayer like:

    var musicPlayer: AVAudioPlayer = AVAudioPlayer() 
    

    or

    musicPlayer = AVAudioPlayer() 
    

    in any method then remove it and declare like:

    var musicPlayer: AVAudioPlayer!
    

    Solution for Objective C

    If you initalise like

    AVAudioPlayer *musicPlayer = [[AVAudioPlayer alloc] init];
    

    REMOVE the init part of [[AVAudioPlayer alloc] init] to look like

    AVAudioPlayer *musicPlayer = [AVAudioPlayer alloc];
    

    EDIT: If after this, your app pauses at that line, like you set the breakpoint there(but you didn't), but app run without problems after you click play/run, you shouldn't worry because it is some c level issue that doesn't affect the app. You can read more in this thread So solution for that is to edit the breakpoint for All Exception, change the exception type from "All" to "Objective-C exceptions"

    1. Go to the Breakpoint navigator in Xcode.
    2. Control-click on the 'All Exceptions' line.
    3. Select the 'Edit Breakpoint...' option.
    4. Change the Exception from All to Objective-C.

    0 讨论(0)
提交回复
热议问题