相关疑难解决方法(0)

如何在后台Swift中播放音频?

如你所见,我正在播放音频广播.但是,当我按下主页按钮退出应用程序流停止或我听不到.如何在后台继续流式传输并从锁定屏幕中进行监听?

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)

audio avfoundation ios swift

45
推荐指数
3
解决办法
3万
查看次数

iOS AVAudioSession中断通知无法按预期工作

我想知道我何时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当应用程序在前台时发生中断时,我如何收到通知?

interrupt avfoundation avaudiorecorder ios avaudiosession

18
推荐指数
1
解决办法
1万
查看次数

如何在Swift 2/AVPlayer中恢复背景音频?

我正在学习Swift作为我的第一门编程语言.

中断后我已经挣了好几个小时才恢复背景音频播放(例如通话)

会发生什么:

  1. 当应用程序转到后台时,音频会继续播放(工作)
  2. 当被呼叫中断时,开始通知中断(工作)
  3. 通话结束时,获取中断结束通知(有效)
  4. 继续播放音频(不起作用 - 什么都听不到)

非常感谢任何帮助!谢谢

笔记:

  1. 该应用程序已注册为背景音频,并在中断前播放正常
  2. 我已经尝试过,没有时间延迟恢复播放 - 两个都没有工作

码:

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)

audio avfoundation ios avplayer swift

9
推荐指数
1
解决办法
6249
查看次数