AVPlayerItemDidPlayToEndTimeNotification issue in tvOS - swift2

I am developing a app on tvOS platform. And facing a strange problem with AVPlayerItemDidPlayToEndTimeNotification. When i am playing the video and disconnect from the internet connection, the Notification called before the video end. i am not understand, how is it happen.
override func viewDidLoad() {
super.viewDidLoad()
self.playVideo(videoUrl as String)
}
func playVideo(videoURL:String) {
let asset = AVAsset(URL: NSURL(string: videoURL)!) as AVAsset
let playerItem = AVPlayerItem(asset: asset)
let playerObj = AVPlayer(playerItem: playerItem)
self.player = playerObj
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyPlayerViewController.playerItemDidReachEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: playerObj.currentItem)
playerObj.play()
}
func playerItemDidReachEnd(infoNoti:NSNotificationCenter){
print("==============Hi it is problem==============")
}

Related

SwifUI MacOS App AVPlayer not working with Video URL

I gave permission in the info.plist file for http request on the ios side. I have allowed the same for the macOS app but cannot view the video.
Error:
Code=-1003 "A server with the specified hostname could not be found."
AVPlayer
import AVKit
struct AVPlayerControllerRepresented : NSViewRepresentable {
var player = AVPlayer()
#Binding var playerStatus: PlayerStatus
func makeNSView(context: Context) -> AVPlayerView {
let view = AVPlayerView()
view.controlsStyle = .none
view.player = player
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { _ in
self.player.seek(to: CMTime.zero)
self.player.play()
}
return view
}
func updateNSView(_ nsView: AVPlayerView, context: Context) {
switch playerStatus {
case .start:
return player.play()
case .end:
return player.pause()
}
}
}
enum PlayerStatus {
case start
case end
}
AVPlayer Using
AVPlayerControllerRepresented(player: AVPlayer(url: URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!), playerStatus: $playerStatus)
I solved my problem this way.
I have allowed incoming and outgoing connections under the App Sandbox tab.

Error with NSBundle.mainBundle when adding sound

I've trying to create a simple sound board for when a button is pressed a sound is made, however when I used this code
let url = NSURL(fileURLWithPath: Bundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
I get an error with "Bundle.mainBundle" orginally it told me to change "Bundle" from "NSBundle" because it has be renamed but when I correct that I get another error saying "Cannot call value for non-function type 'Bundle' "
my entire code:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["60gs", "check", "dada", "danceforme", "eat", "gods", "irelandbaby", "ko'd", "lefthand", "littlewerp", "nocalls", "precision", "sundaymorning", "surprise", "whothefuckisthatguy", "youlldonothing"]
var audioPlayers = [AVAudioPlayer]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for sound in soundFilenames {
do {
let url = Bundle.main.url(forResource:sound, withExtension: "mp3")!
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
audioPlayer.append(audioPlayer())
}
catch {
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonTapped(_ sender: UIButton) {
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.play()
}
}
can anyone help me with this please.
First of all in Swift 3 mainBundle() became main but why not the real short way:
let url = Bundle.main.url(forResource:sound, withExtension: "mp3")!
And the init method has been renamed to
let audioPlayer = try AVAudioPlayer(contentsOf: url)
and change the next line to:
audioPlayers.append(audioPlayer)

CoreLocation can't use with Estimotes Beacon

I build a app with Estimotes indoor Location ,any I use CoreLocation to find our beacon, and my app will change label.text. App can run,but didn't work ,label.text can't change.
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("location", ofType: "json")
do {
let content = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
let locationSetup = ESTLocationBuilder.parseFromJSON(content)
//set up delegate
manager.delegate = self
locationManager.delegate = self
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}
locationManager.startRangingBeaconsInRegion(region)
}
func locationManager(manager: CLLocationManager, didRangeBeacons beacons:[CLBeacon], inRegion region: CLBeaconRegion){
let knownBeacons = beacons.filter{ $0.proximity != CLProximity.Unknown }
if (knownBeacons.count > 0){
let closestBeacon = knownBeacons[0] as CLBeacon
if(closestBeacon.minor.integerValue == 41016)
{
self.snow.text = "find"
}
}
}
First thing to try:
I would move the initialization of locationManger = CLLocationManager() outside the class instance declaration and inside viewDidLoad. I have seen problems initializing the location manager during object construction.

Sent email, without confirmation using swift (OSX)

I cannot manage to send an email using Swift on OSX.
Its almost do the work, but don't sent the email, and on my personal computer, its opening Google Chrome for some raison.
Here is my code
import Cocoa
class ViewController: NSViewController, NSSharingServiceDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func mag(sender: AnyObject) {
let body = "BODY EMAIL"
let shareItems = [body] as NSArray
var service = NSSharingService(named: NSSharingServiceNameComposeEmail)
service?.delegate = self
service?.recipients = ["email#address.com"]
let subject = "Subject!"
service?.subject = subject
service?.performWithItems(shareItems as [AnyObject])
}
}

Adding interstitial ads into SpriteKit game swift

I'm trying to implement interstitial ads with iAd into my spriteKit game. My code is as follows:
class MainViewController: UIViewController, ADInterstitialAdDelegate {
var interAd = ADInterstitialAd()
var interAdView: UIView!
var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
override func viewDidAppear(animated: Bool) {
closeButton.frame = CGRectMake(10, 10, 20, 20)
closeButton.layer.cornerRadius = 10
closeButton.setTitle("x", forState: .Normal)
closeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
closeButton.backgroundColor = UIColor.whiteColor()
closeButton.layer.borderColor = UIColor.blackColor().CGColor
closeButton.layer.borderWidth = 1
closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown)
}
func close(sender: UIButton) {
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
func loadAd() {
println("load ad")
interAd = ADInterstitialAd()
interAd.delegate = self
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
println("ad did load")
interAdView = UIView()
interAdView.frame = self.view!.frame
view!.addSubview(interAdView)
interAd.presentInView(interAdView)
UIViewController.prepareInterstitialAds()
interAdView.addSubview(closeButton)
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
println("failed to receive")
println(error.localizedDescription)
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
This code delivers me more problems than it does results. Unfortunately I am a beginner at implementing any sort of ads into an app and I don't know how I can change any of this in my favour.
One issue I get is that output traces ad did load (as you can see thats supposed to be traced once the ad is presented) however when ad did load is traced no ad is presented. I wait for at least 3 minutes but nothing happens.
Another issue I have is this WARNING: More than 10 instances of ADBannerView or ADInterstitialView currently exist. This is a misuse of the iAd API, and ad performance will suffer as a result. This message is printed only once., but this is printed after the function to load the ad is called once or twice.
Any help is more than appreciated.

Resources