i have three buttons and each are supposed to play a different sound which they do. My problem is that the second button once clicked also plays the first sound.
is there something i could do to fix this?
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
var audioPlayer1 = AVAudioPlayer()
var audioPlayer2 = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "OhNo", ofType: "mp3")! ))
audioPlayer.prepareToPlay()
}
catch{
print(error)
}
do {
audioPlayer1 = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "beep2", ofType: "mp3")! ))
audioPlayer1.prepareToPlay()
}
catch{
print(error)
}
do {
audioPlayer2 = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "beep10", ofType: "mp3")! ))
audioPlayer2.prepareToPlay()
}
catch{
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func catSound(_ sender: UIButton) {
audioPlayer.play()
}
#IBAction func beep(_ sender: UIButton) {
audioPlayer1.play()
}
#IBAction func beep10(_ sender: UIButton) {
audioPlayer2.play()
}
}
Check your storyboard and make sure you haven't connected the second button to both the first and second #IBAction.
Do this buy right-clicking on the second button in the storyboard. On the Touch up Inside-event there should be only one connection, the beep. On the one where it says catSound, click the little x to remove it.
Can you try stopping the other AudioPlayers?
#IBAction func beep(_ sender: UIButton) {
audioPlayer.stop()
audioPlayer2.stop()
audioPlayer1.play()
}
Related
Here's my code do not suggest to remove IBAction I'm a beginner making a browser. I'm also
pretty new to this program:
import Cocoa
import WebKit
class ViewController: NSViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
}
}
#IBAction func didEnterKeyTapped( sender: NSTextField){
let urlString = sender.stringValue
guard let url = URL(string: urlString) else {return}
let urlRequest = URLRequest(url: url)
webView.load(urlRequest)
#IBAction func didnavigationButtonTapped ( sender: NSSegmentedControl){
if sender.selectedSegment == 0 {
webView.goBack()
}else {
webView.Forward()
}
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
let currentUrl = webView.url?.absoluteString ?? ""
guard let windowController = view.window?.windowController as? WindowController else {return}
let textField = windowController.urlTextField
textField?.stringValue = currentUrl
}
}
}
You declared the #IBActions outside of the ViewController class. An IBAction must be a member of a class. Move the IBActions inside the class to fix the compiler error.
Another problem in your code is you declared the didNavigationButtonTapped IBAction inside the didEnterKeyTapped IBAction. The two IBActions should be separate functions, such as the following:
#IBAction func didEnterKeyTapped( sender: NSTextField){
let urlString = sender.stringValue
guard let url = URL(string: urlString) else {return}
let urlRequest = URLRequest(url: url)
webView.load(urlRequest)
}
#IBAction func didnavigationButtonTapped ( sender: NSSegmentedControl) {
if sender.selectedSegment == 0 {
webView.goBack()
}else {
webView.Forward()
}
}
I have a view with a series of action buttons. All these buttons are connected to a function that allows me to play a video. I retrieve the name of the video in the button title. The problem is that I need to press the button twice to get the right video, otherwise the last video is playing
Here is the code that is associated.
import UIKit
import AVKit
import AVFoundation
class levresViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var varVue2="toto"
var playerController = AVPlayerViewController()
var player:AVPlayer?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let videoString:String? = Bundle.main.path(forResource: varVue2, ofType: ".mp4")
if let url = videoString {
let videoURL=NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
self.playerController.player = self.player
}
// Do any additional setup after loading the view.
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playVideo(_ sender: UIButton) {
varVue2=sender.currentTitle!
self.present(self.playerController,animated:true,completion:{
self.playerController.player?.play()
})
}
}
how to show or hide a image in Swift by taping on a Button?
For example:
i have ImageA and ImageB and one Button which i want to use to move ImageA to ImageB and back to ImageA and so on..
It works perfect to move from ImageA to ImageB, but how can i move back to ImageA?
My code is:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var Bild1: UIImageView!
#IBOutlet weak var Bild2: UIImageView!
#IBAction func pressedButton(sender: AnyObject) {
Bild1.hidden = true
Bild2.hidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
Bild1.hidden = false
Bild2.hidden = true
// 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.
}
}
override func viewDidLoad()
{
super.viewDidLoad()
Bild1.isHidden = false
Bild2.isHidden = true
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func pressedButton(sender: AnyObject)
{
Bild1.isHidden = !Bild1.isHidden
Bild2.isHidden = !Bild2.isHidden
}
#IBAction func pressedButton(sender: AnyObject) {
Bild1.isHidden = !Bild1.isHidden
Bild2.isHidden = !Bild2.isHidden
}
This will toggle the image property between show and hidden.
Syntax is Swift4 compatible
try replacing with:
#IBAction func pressedButton(sender: AnyObject) {
if Bild1.hidden {
Bild1.hidden = false
Bild2.hidden = true
} else {
Bild1.hidden = true
Bild2.hidden = false
}
}
#IBAction func pressedButton(sender: AnyObject) {
Bild1.hidden = !Bild1.hidden
Bild2.hidden = !Bild2.hidden
}
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")
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
can anyone help me get this to work I am a noob programer and I could not find a way to do this.
Code below
import UIKit
class ViewController: UIViewController {
#IBAction func pistolButton(sender: AnyObject) {
}
override func viewDidLoad() { // I want my audio file to play when button is tapped
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
First drag your sound to your project and choose to copy for your destination if needed and check "add to target" to your app. Create a function to play your sound and you need also to add import AVFoundation at the beginning of your code as bellow:
import AVFoundation
let beepSoundURL = NSBundle.mainBundle().URLForResource("beep", withExtension: "aif")!
var beepPlayer = AVAudioPlayer()
func playMySound(){
beepPlayer = AVAudioPlayer(contentsOfURL: beepSoundURL, error: nil)
beepPlayer.prepareToPlay()
beepPlayer.play()
}
#IBAction func pistolButton(sender: AnyObject) {
playMySound()
}
There is a lot of pretty good code on Stack Overflow:
Playing a sound with AVAudioPlayer
Here is some example code:
import AVFoundation
var audioPlayer: AVAudioPlayer?
if let path = NSBundle.mainBundle().pathForResource("mysound", ofType: "aiff") {
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path), fileTypeHint: "aiff", error: nil)
if let sound = audioPlayer {
sound.prepareToPlay()
sound.play()
}
}
from this location:
http://www.reddit.com/r/swift/comments/28izxl/how_do_you_play_a_sound_in_ios/
Swift 3
the syntax is now as follows:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// address of the music file
let music = Bundle.main.path(forResource: "Music", ofType: "mp3")
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music! ))
}
catch{
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func play(_ sender: AnyObject) {
audioPlayer.play()
}
#IBAction func stop(_ sender: AnyObject) {
audioPlayer.stop()
}
}
Swift 5.5 version syntax.`
import AVFoundation
let beepSoundURL = Bundle.main.url(forResource: "gosound", withExtension: "wav")
var beepPlayer = AVAudioPlayer()
func playMySound(){
if let soundGo = beepSoundURL {
do {
try beepPlayer = AVAudioPlayer(contentsOf: soundGo, fileTypeHint: nil)
}
catch {
print ("error")
}
}
beepPlayer.prepareToPlay()
beepPlayer.play()
}
#IBAction func pistolButton(sender: AnyObject) {
playMySound()
}