应用程序在后台Swift Xcode中播放音乐

时间:2016-07-20 11:59:16

标签: ios 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 个答案:

答案 0 :(得分:2)

确保在Audio,Airplay and picture in picture下的background modes capabilitiestargetproject setup检查tools:context="com.example.hacker.guidekyu.Home"> <!-- TODO: Update blank fragment layout --> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text=" WELCOME TO KYU" /> <ViewFlipper android:id="@+id/bckgrndViewFlipper1" android:layout_width="fill_parent" android:layout_height="200dp"> <ImageView android:id="@+id/bckgrndImageView8" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerCrop" android:src="@mipmap/one" /> <ImageView android:id="@+id/bckgrndImageView7" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerCrop" android:src="@mipmap/two" /> <ImageView android:id="@+id/bckgrndImageView6" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerCrop" android:src="@mipmap/three" /> <ImageView android:id="@+id/bckgrndImageView5" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerCrop" android:src="@mipmap/two" /> <ImageView android:id="@+id/bckgrndImageView4" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerCrop" android:src="@mipmap/one" /> </ViewFlipper> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff0" android:layout_margin="8dp"> <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:background="#58a3c1" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_weight="50" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> <LinearLayout android:layout_weight="50" android:layout_width="fill_parent" android:orientation="vertical" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff0" android:layout_margin="8dp"> <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:background="#58a3c1" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_weight="50" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> <LinearLayout android:layout_weight="50" android:layout_width="fill_parent" android:orientation="vertical" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff0" android:layout_margin="8dp"> <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:background="#58a3c1" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_weight="50" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> <LinearLayout android:layout_weight="50" android:layout_width="fill_parent" android:orientation="vertical" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ff" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff0" android:layout_margin="8dp"> <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:background="#58a3c1" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_weight="50" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> <LinearLayout android:layout_weight="50" android:layout_width="fill_parent" android:orientation="vertical" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff0" android:layout_margin="8dp"> <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:background="#58a3c1" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_weight="50" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> <LinearLayout android:layout_weight="50" android:layout_width="fill_parent" android:orientation="vertical" android:layout_marginLeft="20dp" android:gravity="center" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Administration" /> </LinearLayout> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> {/ 1}}

检查屏幕截图

enter image description here