2016-07-20 12 views
0

私のアプリの実行中にこのコードを書いたが、別のアプリに行くと音楽が止まる。アプリがバックグラウンドのときに音楽が再生されないSwift Xcode

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    do { 
     try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
     print("AVAudioSession Category Playback OK") 
     do { 
      try AVAudioSession.sharedInstance().setActive(true) 
      print("AVAudioSession is Active") 
     } catch let error as NSError { 
      print(error.localizedDescription) 
     } 
    } catch let error as NSError { 
     print(error.localizedDescription) 
    } 
    playBackgroundMusic("anti.mp3") 


} 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

import AVFoundation 

var backgroundMusicPlayer = AVAudioPlayer() 

func playBackgroundMusic(filename: String) { 
    let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil) 
    guard let newURL = url else { 
     print("Could not find file: \(filename)") 
     return 
    } 
    do { 
     backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: newURL) 
     backgroundMusicPlayer.numberOfLoops = -1 
     backgroundMusicPlayer.prepareToPlay() 
     backgroundMusicPlayer.play() 
    } catch let error as NSError { 
     print(error.description) 
    } 
} 

また、必要なバックグラウンドモードでinfo.plistにオーディオ文字列を追加しました。

私はこの出力も得ているので、他のアプリケーションを使用している間はバックグラウンドで再生を続けると思っていましたが、そうではありません。

AVAudioSession Category Playback OK 
AVAudioSession is Active 
AVAudioSession Category Playback OK 
AVAudioSession is Active 

答えて

1

スクリーンショット

enter image description here

をチェック

project setuptargetAudio,Airplay and picture in picturecapabilitiesbackground modesにチェックされていることを確認してください
関連する問題