Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS swift streaming app does not play music in background mode

My app is running fine but as soon the screen safe is on or the doing something else on the iphone the stream stops. I activated the background modes "is playing audio" but it does not helps.

This is my ViewController.swift

import UIKit
import MediaPlayer

class ViewController: UIViewController {
    let player: MPMoviePlayerViewController = MPMoviePlayerViewController(contentURL: NSURL(string: "http://url to my stream"))

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        player.moviePlayer.movieSourceType = .Streaming
        self.presentViewController(player, animated: true, completion: nil)

        loadAddressURL()
    }

    func stop() {
        player.moviePlayer.stop()
    }
    @IBAction func Hitplay(sender: AnyObject) {
        player.moviePlayer.play()
    }

    @IBAction func Hitpause(sender: AnyObject) {
        player.moviePlayer.stop()
    }

    @IBOutlet var Nowplay: UIWebView!
    var URLPath = "http://url to on air now"

    func loadAddressURL() {
        let requestURL = NSURL (string:URLPath)
        let request = NSURLRequest (URL: requestURL!)
        Nowplay.loadRequest(request)
    }
}

and here is my info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-      1.0.dtd">
<plist version="1.0">
<dict>
 <key>CFBundleDevelopmentRegion</key>
 <string>en</string>
 <key>CFBundleExecutable</key>
 <string>$(EXECUTABLE_NAME)</string>
 <key>CFBundleIdentifier</key>
<string>com.product name.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</dict>
like image 361
Frédérique Colin Avatar asked Dec 03 '14 19:12

Frédérique Colin


People also ask

How do you play music in the background on iOS?

Go to Settings > Accessibility > Audio/Visual > Background Sounds, then turn on Background Sounds. Set any of the following: Sound: Choose a sound; the audio file downloads to your iPhone.


1 Answers

Setting the audio background mode is correct, but I think you also need to set the audio category for the audio session.

Try adding this to your app delegate's didFinishLaunchingWithOptions:

var activeError: NSError? = nil
AVAudioSession.sharedInstance().setActive(true, error: &activeError)

if let actError = activeError {
  NSLog("Error setting audio active: \(actError.code)")
}

var categoryError: NSError? = nil
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &categoryError)

if let catError = categoryError {
  NSLog("Error setting audio category: \(catError.code)")
}
like image 199
Erik Tjernlund Avatar answered Nov 14 '22 23:11

Erik Tjernlund