2017-08-05 20 views
-1

我想在點擊第一個圖像時播放任何聲音,然後轉到第二個圖像頁面。 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() 

} 

} 
+0

我給我的學分,幫助我在代碼 –

+0

https://stackoverflow.com/users/6028575/jaydeep-vyas –

回答

0

首先,你需要導入AVFoundation播放聲音(也不要忘記在項目中添加https://stackoverflow.com/a/9864504/6028575

import UIKit 
    import AVFoundation 

    class ViewController: UIViewController , UIGestureRecognizerDelegate 
    { 

var player: AVAudioPlayer? 

      @IBOutlet weak var touchImageView: UIImageView! 

     override func viewDidLoad() 
     { 


      super.viewDidLoad() 
      let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(playSound(tapGestureRecognizer:))) 
      touchImageView.isUserInteractionEnabled = true 
      touchImageView.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:"1", 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 
     } 

    } 
+0

評論是不適合長時間討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/151205/discussion-on-answer-by-jaydeep-vyas-i-would-like-to-play-a-sound-當-I-抽頭上)。 –

0

您需要設置敲擊手勢您imageView,然後用AVAudioPlayer播放聲音後實現的AVAudioPlayerdelegate方法一旦你的聲音播放完畢,這將調用,然後打開另一view controller using perform segue or using Bundle

+0

如何實現AVAudioPlayer的委託方法,然後使用執行segue或使用Bundle打開另一個視圖控制器? –

+0

您需要先在您的控制器或您的其他類上遵守協議AVAudioPlayerDelegate,然後設置audioplayer.delegate = self,並在此之後實現將自動調用的方法func audioPlayerDidFinishPlaying(_ player:AVAudioPlayer, successful flag:Bool)。 –

+0

我是一個begginner –

相關問題