Whenever I run the project, it pulls up the video player, but the video doesn't play
.
How do I fix this? In case the code in the picture isn't clear enough, here is the code:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
let avPlayerViewController = AVPlayerViewController()
var avPlayer:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from nib.
let urlPathString = Bundle.main.path(forResource: "small", ofType: "mp4")
if urlPathString != nil {
let movieUrl = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
self.avPlayer = AVPlayer(url: movieUrl as URL)
self.avPlayerViewController.player = self.avPlayer
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playButtonTapped(_ sender: AnyObject) {
//Trigger the video to play
self.present(self.avPlayerViewController, animated: true) { () -> Void in self.avPlayerViewController.player?.play() }
}
}
The solutions suggested in the comment, and the one #Guan has answered are both correct.
Your urlPathString is actually nil here,
To make it work -
1- Either add the Target Membership, as suggested in the comment and the answer above or
Do the following
1.1- Delete the small.mp4 from bundle, make sure you move it to trash
2.1 - Go to trash and drag and drop your mp4 file into the project again, you will get a window like this -
While adding it back make sure you -
Check the projectTarget checkbox like the screenshot below -
Make sure the file name is small.mp4
Make sure your urlPathString is not nil.
I don't know what the following code mean:
let movieUrl = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
I think the simulator can not get the path of file which in your mac.(I'm not sure, I haven't do this before).
You can check the file's target membership.
The code may work like this:
var urlPaht = Bundle.main.url(forResource: "small", withExtension:"mp4")
if urlPaht == nil {
urlPaht = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
}
self.avPlayer = AVPlayer(url: movieUrl as URL)
self.avPlayerViewController.player = self.avPlayer
Related
I am very new to Swift and Xcode and I was trying to use a video as a transition from one scene to another. I have most of the code for this figured out but the size of the video does not match the size of the screen, meaning when the transition occurs the background changes size. I could fix this by changing the AVLayerVideoGravity but the commands in the documentation give me errors, and I haven't found a way to set them properly. I'm sure this is really simple I just can't find the syntax I need anywhere, especially in the newest versions of swift and Xcode so I thought I would ask. Here is the code I have.
import UIKit
import AVKit
class MainRoom: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func Backpack(_ sender: Any) {
if let path = Bundle.main.path(forResource: "OpenBackpack", ofType: "avi", inDirectory: "SegueAnimations")
{
let OpenBackpack = AVPlayer(url: URL(fileURLWithPath: path))
let animPlayer = AVPlayerViewController()
animPlayer.player = OpenBackpack
animPlayer.showsPlaybackControls = true
let avPlayerLayer = AVPlayerLayer(player: OpenBackpack)
//Set avlayervideogravity to resizeaspectfill
present(animPlayer, animated: true, completion:
{
OpenBackpack.play()
})
}
}
}
Thank you for your help!
Found it! I misunderstood the code in another post, this works well
animPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
I was following this tutorial on YouTub: https://www.youtube.com/watch?v=qC6DzF_ACpQ&t=714s , and at 12.18 when he clicks the button, it plays audio. I followed all the exact same steps as he did in the video and it is not working for me.
I am pretty new to coding in swift so I did not try anything else other than restarting the whole project again, but it did not work.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func aTapped(_ sender: Any) {
let url = Bundle.main.url(forResource: "a", withExtension: "mp3")
// Make sure that weve got the url, otherwise abord
guard url != nil else{
return
}
do{
audioPlayer = try AVAudioPlayer(contentsOf: url!)
audioPlayer?.play()
}
catch{
print("error")
}
}
I just want the audio to play when I click the button assigned to the code, but when I click the button, no audio plays at all.
Please Help.
We are trying to play local GIF using AVPlayer. Does AVPlayer supports GIF format? This is what we tried:
guard let gifUrl = Bundle.main.url(forResource: "myGif", withExtension: "gif") else {
return
}
let gifPlayer = AVPlayer(url: gifUrl)
self.videoContainer.player = gifPlayer
self.videoContainer.player?.play()
Is it possible to play GIF with AVPlayer?
Any help or advice will be highly appreciated. Thank you.
Best Regards, Roi
You can try these steps to play local video using AVPlayer in swift:-
1) First of all add the video into your xcode project.
2) Check your added video in Bundle. Select your project your Target -> Build Phases -> Copy Bundle Resources.
If video is not available in Copy Bundle Resources then you can add by tapping on Plus button.
3) Use the following code in you project.
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
private func playVideo() {
guard let path = Bundle.main.path(forResource: "video", ofType:"m4v") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
present(playerController, animated: true) {
player.play()
}
}
}
Hope this will help you :)
I'm new in Swift. I am trying to Run this code but got an error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) a.
Error appears here:
self.player.play()
Can anyone help in this issue?
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
do {
if let audioPath = Bundle.main.path(forResource: "music", ofType: "mp3") {
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath))
}
} catch {
print("ERROR")
}
self.player.play()
}
What you doing with your code is:
Creating an instance of AVAudioPlayer without any data
Trying to create a new instance with some data from music.mp3 file
Playing it
If the audioPath is not correct, then the player is not correctly created: application will use the one without valid data, leading to a crash during playback.
Your code can be written making the player optional, which helps preventing the crash:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
player = initializePlayer()
player?.play()
}
private func initializePlayer() -> AVAudioPlayer? {
guard let path = Bundle.main.path(forResource: "music", ofType: "mp3") else {
return nil
}
return try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
}
}
Some changes performed to the code:
player is now optional: there is no need to initialize it twice. Plus if the initialization goes well you can use it without problems, otherwise your app won't crash.
The play method is now in an optional chain
I've moved the initialization of the player in a separate private methods, to maintain the code readable. Feel free to change it, it is just a suggestion.
I've got it. The issue is that the file isn't being coping to my app bundle. To fix it:
Click your project
Click your target
Select Build Phases
Expand Copy Bundle Resources
Click '+' and select your file.
Thanks Denis Hennessy
I am making a sound effects application, and I am currently trying to get the sounds to play at the push of a button, but I cannot build it out until I can fix the errors. This is an error that I came across, but I don't know how to fix it.
This is my code:
class ViewController: UIViewController {
let noiseMaker = NoiseMaker()
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
#IBAction func curse(_ sender: AnyObject) {
noiseMaker.play(sender.tag)
if let url = Bundle.main.url(forResource: getName(tag: sender.tag), withExtension: "wav"){
player = try! AVAudioPlayer
The error it gave me was this:
Cannot assign value of type 'AVAudioPlayer.Type' to type 'AVAudioPlayer?'
Use like this:
player = try AVAudioPlayer(contentsOfURL: url)
player.delegate = self
player.prepareToPlay()
player.play()