# AudioPlayer **Repository Path**: pengkun/AudioPlayer ## Basic Information - **Project Name**: AudioPlayer - **Description**: 实现长时间后台运行,跑步过程中播报跑步信息 - **Primary Language**: Swift - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2017-11-28 - **Last Updated**: 2021-06-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AudioPlayer 1. 实现长时间后台运行采用其中一种方式:持续播放无声音乐 2. 跑步过程中播报跑步信息 ## 开始使用 #### 1. 场景 ```swift // 背景音乐 enum AudioPlayScene { case normal // 前台需要播放,后台不需要播放 case none // 前后台都不播放 case run // 前后台都需要播放 } ``` 根据自己的需求设置 **AudioManager.shared.audioPlayScene** #### 2. 启动保活 实现保活需要在**AppDelegate**中实现必要的两个回调函数 ```swift func applicationDidEnterBackground(_ application: UIApplication) { // 不在跑步页面才需要关闭 if AudioManager.shared.audioPlayScene != .run { AudioManager.shared.openBackgroundAudioAutoPlay = false } } func applicationDidBecomeActive(_ application: UIApplication) { AudioManager.shared.openBackgroundAudioAutoPlay = true } ``` #### 3.播报 ```swift func play() { var audioText = [[String: [String]]]() audioText.append(["altitude": ["3","2","1","relax","3","2","1","relax"]]) guard let audioModel = AudioModel(res: audioText) else{return} AudioManager.shared.playWithModel(res: audioModel) } ```