0

I've got some problems with video on my app's background.

Here is my program code:

import UIKit
import AVKit
import AVFoundation

class IntroViewController: UIViewController {

var player: AVPlayer?

override func viewDidLoad() {
    super.viewDidLoad()

    let videoURL: NSURL = NSBundle.mainBundle().URLForResource("intro", withExtension: "mov")!

    player = AVPlayer(URL: videoURL)
    player?.actionAtItemEnd = .None
    player?.muted = false

    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    playerLayer.zPosition = -1

    playerLayer.frame = view.frame

    view.layer.addSublayer(playerLayer)

    player?.play()

    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: "loopVideo",
        name: AVPlayerItemDidPlayToEndTimeNotification,
        object: nil)
 }

func loopVideo() {
    player?.seekToTime(kCMTimeZero)
    player?.play()
}

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

}

After launching my program, the Music app is muted. How do I fix it?

And one more question: after returning to this screen from home screen, without closing the app, video stops. Where is the problem?

Ermiar
  • 1,078
  • 13
  • 20
Ivan A
  • 193
  • 1
  • 1
  • 13
  • 1
    Possible duplicate of [iPhone AVAudioPlayer stopping background music](http://stackoverflow.com/questions/1672602/iphone-avaudioplayer-stopping-background-music) – Matt Le Fleur Jan 28 '16 at 16:37
  • It doesn't working for me – Ivan A Jan 28 '16 at 16:46
  • 1
    What did you try? In your *AppDelegate.swift*, add `import AVFoundation` and in `applicationDidFinishLaunchingWithOptions`, add the line `try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)` – Matt Le Fleur Jan 28 '16 at 16:51
  • Thnx! Now thats working! – Ivan A Jan 28 '16 at 18:07

0 Answers0