如你所见,我正在播放音频广播.但是,当我按下主页按钮退出应用程序流停止或我听不到.如何在后台继续流式传输并从锁定屏幕中进行监听?
ViewController.Swift
import UIKit
import AVFoundation
import MediaPlayer
import GoogleMobileAds
class ViewController: UIViewController, GADInterstitialDelegate {
@IBOutlet weak var exitMapButton: UIButton!
@IBOutlet weak var radarMap: UIWebView!
var interstitial: GADInterstitial!
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-5378899862041789/2782958552")
interstitial.delegate = self
interstitial.loadRequest(GADRequest())
return interstitial
}
func getAd(){
if (self.interstitial.isReady)
{
self.interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadInterstitial()
}
}
@IBOutlet weak var ataturkButton: UIButton!
@IBOutlet weak var sabihaButton: UIButton!
@IBOutlet weak var esenbogaButton: UIButton!
@IBOutlet weak var weatherButton: UIButton!
@IBOutlet weak var statusLabel: …
Run Code Online (Sandbox Code Playgroud) 我想知道我何时AVAudioRecorder
无法进入(例如音乐开始播放时).
正如audioRecorderEndInterruption
将在iOS 9中弃用的那样,我专注于AVAudioSession
中断通知(但两者都没有按预期工作).
问题是,如果应用程序在中断发生时仍然在前台,则永远不会调用中断通知.
例如:用户启动和停止播放音乐而不将应用程序移动到后台.
要检测我正在使用的任何中断:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionWasInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
...
- (void)audioSessionWasInterrupted:(NSNotification *)notification {
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"Interruption notification");
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"InterruptionTypeBegan");
} else {
NSLog(@"InterruptionTypeEnded");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我InterruptionTypeBegan
按预期得到了,但InterruptionTypeEnded
如果应用程序仍在前台,则不会被调用(意味着在将应用程序放置在后台并返回到前台之前不会调用它).
InterruptionTypeEnded
当应用程序在前台时发生中断时,我如何收到通知?
我正在学习Swift作为我的第一门编程语言.
中断后我已经挣了好几个小时才恢复背景音频播放(例如通话)
会发生什么:
非常感谢任何帮助!谢谢
笔记:
码:
import UIKit
import AVFoundation
var player: AVQueuePlayer!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true, withOptions: .NotifyOthersOnDeactivation)
} catch { }
let songNames = ["music"]
let songs = songNames.map { AVPlayerItem(URL:
NSBundle.mainBundle().URLForResource($0, withExtension: "mp3")!) }
player = AVQueuePlayer(items: songs)
let theSession = AVAudioSession.sharedInstance()
NSNotificationCenter.defaultCenter().addObserver(self,
selector:"playInterrupt:",
name:AVAudioSessionInterruptionNotification,
object: theSession)
player.play()
}
func playInterrupt(notification: NSNotification) …
Run Code Online (Sandbox Code Playgroud)