AppleTV + Swift + MP4 + Xcode? How to show a .mp4 inside xcodeproj? - xcode

how would I go about using Swift & AppleTV4 to display a video that is inside the XcodeProj? Can I use the AVPlayer to also play an Mp3 later?
This is what I have:
import UIKit
import AVFoundation
import AVKit
import MediaPlayer
class ViewController: UIViewController {
let videoplayer = AVPlayer(URL: NSURL(string: "file.mp4")!)
let audioplayer = AVPlayer(URL: NSURL(string: "file.mp3")!)
#IBOutlet weak var Buttonizer: UIButton!
#IBAction func ButtonPressed(sender: AnyObject) {
videoplayer.play()
audioplayer.play()
// let videoplayer = AVPlayer(URL: NSURL(string: "https://clips.vorwaerts-gmbh.de/VfE_html5.mp4")!)
}
and the rest.. (viewDidLoad stuff)
I tried to run AVPlayer with an external mp4, but the videoplayer.play() simply crashes with a breakcode.

Try this to play a video in AVPlayer:
let path = NSBundle.mainBundle().pathForResource("test", ofType:"mp4")!
let videoURL = NSURL(fileURLWithPath: path)
let player = AVPlayer(URL: videoURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()

Related

Pass variable text to src in swift

Trying to pass the variable from a text input on viewcontroller.swift to a second view page (view2). Having trouble with string() as an input. Here's what I have:
import Foundation
import AVFoundation
import UIKit
import AVKit
class View2 : UIViewController {
var PlayerViewController = AVPlayerViewController()
var PlayerView = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
let fileURL = NSURL (fileURLWithPath: String())
PlayerView = AVPlayer (URL: srcURL)
PlayerViewController.player = PlayerView
self.presentViewController(PlayerViewController, animated: true){
self.PlayerViewController.player?.play()
}
}
Drag your TextView from storyboard to your View Controller swift file as an Outlet
and give it a name for example
#IBOutlet weak var texty: UITextField!
and then set a variable for example
var yourTexty: String!
and then you do
yourTexty = texty.text

Many errors when trying to play video in xcode 7 / Swift 2

I am having trouble playing a video in my view controller. I have a play button and when I press it, I want the video which I imported into xcode to play. Having some errors come up and I am stumped. Any ideas?
import UIKit
import MediaPlayer
import AVFoundation
class AuroraViewController: UIViewController {
var moviePlayer: AVPlayer?
#IBOutlet var AuroraViewController: UIView!
#IBAction func playButtonPressed(sender: AnyObject) {
playVideo()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func playVideo() {
if let path = NSBundle.mainBundle().pathForResource("Aurora", ofType:"mp4") {
let url = NSURL(fileURLWithPath: path)
Here I begin to get errors and need help with what it is complaining about
moviePlayer = AVPlayer(contentURL: url)
self.moviePlayer = moviePlayer
moviePlayer.view.frame = self.view.bounds
moviePlayer.prepareToPlay()
moviePlayer.scalingMode = .AspectFill
view.addSubview(moviePlayer.view)
} else {
debugPrint("oops, something wrong when playing video.m4v")
}
}
}

Swift - Can't Dismiss MPMoviePlayerViewController

I have a video which opens in an MPMoviePlayerController in my app. It all works great except for the Done button which should close the player. The first time that the video is played the Done button works great. But if you pause it when you are watching it, then hit Done the second time you try to play the video, the Done button doesn't work. I have made a screen recording here to make it a bit simpler to understand:
http://1drv.ms/1Jcdodc
Can anyone help?
This is my code that I am using:
import UIKit
import MediaPlayer
class MainContent: UIViewController {
//Movie Player variables
var movieViewController : MPMoviePlayerViewController?
var movieplayer : MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
//Video Player setup
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
movieViewController = MPMoviePlayerViewController(contentURL: url)
}
func doneButtonClick(sender:NSNotification?){
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
//when watch button is pressed, present the movie player
#IBAction func watchPressed(sender: AnyObject)
{self.presentMoviePlayerViewControllerAnimated(movieViewController)}
}
To fix this issue, I added the myMoviePlayerViewController.moviePlayer.stop() to the 'doneButtonClick' function. I then added the myMoviePlayerViewController.moviePlayer.play() again to the
#IBAction func watchPressed(sender: AnyObject)
{self.presentMoviePlayerViewControllerAnimated(movieViewController)}
}
So all in all a simple fix! Code is below:
import UIKit
import MediaPlayer
class MainContent: UIViewController {
//Movie Player variables
var movieViewController : MPMoviePlayerViewController?
var movieplayer : MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
//Video Player setup
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
movieViewController = MPMoviePlayerViewController(contentURL: url)
}
func doneButtonClick(sender:NSNotification?){
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
movieViewController?.moviePlayer.stop()
}
#IBAction func watchPressed(sender: AnyObject){
self.presentMoviePlayerViewControllerAnimated(movieViewController)
movieViewController?.moviePlayer.play()
}
}

swift multiple MPMoviePlayerController

hi I'm playing with the MPMoviePlayerController in swift
i have 2 MPMoviePlayerControllers I preload video in to each in view did load the second one plays from a button fine the first will play after the second has played? if I load the first one i get sound no picture but if play the second one then the first will then play fine?
here is a link to my testing
https://dl.dropboxusercontent.com/u/22927/videotest.zip
thanks for your help
import UIKit
import MediaPlayer
class ViewController: UIViewController
{
var moviePlayer : MPMoviePlayerController!
var moviePlayer2 : MPMoviePlayerController!
#IBOutlet weak var movieview: UIView!
#IBOutlet weak var movieview2: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
loadMovie()
loadMovie2()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func play2(sender: AnyObject)
{
moviePlayer2.play()
}
#IBAction func playbtn(sender: AnyObject)
{
moviePlayer.play()
}
func loadMovie()
{
let path = NSBundle.mainBundle().pathForResource("video1", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 0, y: 0, width: movieview.frame.size.width, height: movieview.frame.size.height )
moviePlayer.view.sizeToFit()
moviePlayer.scalingMode = MPMovieScalingMode.Fill
moviePlayer.fullscreen = false
moviePlayer.controlStyle = MPMovieControlStyle.None
moviePlayer.movieSourceType = MPMovieSourceType.File
moviePlayer.repeatMode = MPMovieRepeatMode.None
moviePlayer.prepareToPlay()
moviePlayer.pause()
movieview.addSubview( moviePlayer.view)
}
func loadMovie2()
{
let path = NSBundle.mainBundle().pathForResource("video2", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer2 = MPMoviePlayerController(contentURL: url)
moviePlayer2.view.frame = CGRect(x: 0, y: 0, width: movieview2.frame.size.width, height: movieview2.frame.size.height )
moviePlayer2.view.sizeToFit()
moviePlayer2.scalingMode = MPMovieScalingMode.Fill
moviePlayer2.fullscreen = false
moviePlayer2.controlStyle = MPMovieControlStyle.None
moviePlayer2.movieSourceType = MPMovieSourceType.File
moviePlayer2.repeatMode = MPMovieRepeatMode.None
moviePlayer2.prepareToPlay()
moviePlayer2.pause()
movieview2.addSubview( moviePlayer2.view)
}
}
import UIKit
import AVFoundation
import AssetsLibrary
import MediaPlayer
import CoreMedia
var player : AVPlayer? = nil
var playerLayer : AVPlayerLayer? = nil
var asset : AVAsset? = nil
var playerItem: AVPlayerItem? = nil
var loadingAssetOne = false
#IBOutlet weak var movieview: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func play2(sender: AnyObject)
{
loadingAssetOne = false
loadMovie()
}
#IBAction func playbtn(sender: AnyObject)
{
loadingAssetOne = true
loadMovie()
}
func loadMovie()
{
if loadingAssetOne
{
let path = NSBundle.mainBundle().pathForResource("video1", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
asset = AVAsset(URL: url) as AVAsset
playerItem = AVPlayerItem(asset: asset!)
player = AVPlayer (playerItem: self.playerItem!)
playerLayer = AVPlayerLayer(player: self.player)
movieview.frame = CGRectMake(38, 57, 220, 106)
playerLayer?.frame = movieview.frame
movieview.layer.addSublayer(self.playerLayer!)
player!.play()
}
else
{
let path = NSBundle.mainBundle().pathForResource("video1", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
asset = AVAsset(URL: url) as AVAsset
playerItem = AVPlayerItem(asset: asset!)
player = AVPlayer (playerItem: self.playerItem!)
playerLayer = AVPlayerLayer(player: self.player)
movieview.frame = CGRectMake(38, 206, 220, 106)
playerLayer?.frame = movieview.frame
movieview.layer.addSublayer(self.playerLayer!)
player!.play()
}
}
For more details,please go through the below source
Need to show multiple video using MPMoviePlayerController swift

One UIButton play multiple sounds randomly. Swift

I need to create a button where when I press it, just one sounds gets played. When I press it again, I want a different sound to get play. I have 4 sounds that I want to get played randomly. Therefor when I press the button let's say 8 times, I will hear 8 sounds, randomly.
I put the sound file names in an array and implemented the arc4random to randomise. I am working with AVFoundation and that's where I get stuck. I do not know how to play this array.
I know how to play 1 sound, but not how to implement this in the button. Anyone any idea?
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["sound_1.mp3", "sound_2.mp3"]
let randomIndex = Int(arc4random_uniform(UInt32(soundFilenames.count)))
let selectedFilename = soundFilenames[randomIndex]
#IBAction func tap(sender: UIButton) {
// Audio code?
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Edit :
#IBAction func tap(sender: UIButton) {
let soundFilenames = ["Sound1.mp3", "Sound2.mp3"]
let randomIndex = Int(arc4random_uniform(UInt32(soundFilenames.count)))
let selectedFilename = soundFilenames[randomIndex]
let sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("selectedFilename", ofType: "mp3")!)
var audioPlayer : AVAudioPlayer?
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer!.prepareToPlay()
audioPlayer!.play()
}
Edit 2 :
var audioPlayer : AVAudioPlayer?
#IBAction func tap(sender: UIButton) {
let soundFilenames = ["Sound1", "Sound2"]
let randomIndex = Int(arc4random_uniform(UInt32(soundFilenames.count)))
let selectedFilename = soundFilenames[randomIndex]
let sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("selectedFilename", ofType: "mp3")!)
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer!.prepareToPlay()
audioPlayer!.play()
}
If that is your exact copy/pasted code, your let sound statement could use a proofread.
let sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("selectedFilename", ofType: "mp3")!)
not
let sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("selectedFilemane", ofType: "mp3")!)
You mentioned you know how to play a sound, but have you done that yet with this code?

Resources