我想在點擊第一個圖像時播放任何聲音,然後轉到第二個圖像頁面。 This is the storyboard image of the specific part where I want the code to be based off of. 有人可以幫助我。 如何將此代碼添加到我的代碼 其他代碼中。代碼有人給我幫助:當我點擊一個圖像,然後轉到另一個頁面時,我想播放聲音
import UIKit
import AVFoundation
class ViewController: UIViewController , UIGestureRecognizerDelegate
{
@IBOutlet weak var imgPhoto: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(playSound(tapGestureRecognizer:)))
imgPhoto.isUserInteractionEnabled = true
imgPhoto.addGestureRecognizer(tapGestureRecognizer) /*Adding Gesture to image*/
}
/*
This method is called when user tap on gesture and sound is played
*/
func playSound(tapGestureRecognizer: UITapGestureRecognizer)
{
print("play")
guard let url = Bundle.main.url(forResource:"a", withExtension: "mp3") else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
/*
This method is used to notify when audio is completed
*/
NotificationCenter.default.addObserver(self, selector: #selector(self.soundPlayComplete(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: url)
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
print("hhh")
} catch let error {
print(error.localizedDescription)
}
}
func soundPlayComplete(sender: Notification) {
print("Complete")
//After completion audio you can perform segue here
}
}
我的代碼我想要添加其他代碼。
import UIKit
import AVFoundation
class ViewController: UIViewController {
class ViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var touchImageView: UIImageView!
}
let soundFilenames = ["5","8","7","4","6","1","3","2"]
var audioPlayers = [AVAudioPlayer]()
var lastAudioPlayer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set up audio players
for sound in soundFilenames {
do {
let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "wav")!)
let audioPlayer = try AVAudioPlayer (contentsOf:url)
audioPlayers.append(audioPlayer)
}
catch {
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime=0;
audioPlayer.play();
}
@IBAction func tbuttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime=0;
audioPlayer.play()
}
}
我給我的學分,幫助我在代碼 –
https://stackoverflow.com/users/6028575/jaydeep-vyas –