2017-09-06 3 views
2

오디오를 중지하고 한 번 탭하면 오디오가 중지됩니다. 다시 탭하면 오디오가 중단 된 지점부터 오디오가 재개됩니다. 이 swift를 사용하여 오디오 도움말 중지

} 
// This Action allows users to stop the audio 
@IBAction func stop(_ sender: UIButton) { 
if audioPlayer.isPlaying { 
    audioPlayer.stop() 
} else { 
    self.audioPlayer.play() 
} 

내가 할 수있는 오디오 I 버튼을 누르면 중지 할 수 있습니다 원하는 것은, 그것은 아래 코드도 그 부분에서 코드입니다 그래서 아무리 많은 시간 I 버튼을 눌러 오디오를 정지 시키면 오디오가 다시 재생되지 않습니다. 그런 다음 버튼을 눌러 사운드를 재생하면 사운드가 다시 재생됩니다.

다음

내 코드는,

// 
// ViewController.swift 
// app21 
// 
// Created by Jared Evan Miller on 8/14/17. 
// Copyright © 2017 Jared Evan Miller. All rights reserved. 
// 

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

let soundFilenames = ["5","8","7","4","6","1","3","2","9"] 
var audioPlayers = [AVAudioPlayer]() 
var lastAudioPlayer = 0 
var audioPlayer = AVAudioPlayer() 
var audioPlayer1 = AVAudioPlayer() 
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 { 
      // Try to do somerhing 
      let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "wav")!); 
      let audioPlayer = try AVAudioPlayer(contentsOf:url) 

      audioPlayers.append(audioPlayer) 
     } 
     catch { 

      // Catch the error that is thrown 
      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; 
    audioPlayer = audioPlayers[sender.tag] 
    audioPlayer.currentTime = 0; 
    audioPlayer.play() 
} 

@IBAction func buttonTapped2(_ sender: UIButton) { 
    let lastPlayer = audioPlayers[lastAudioPlayer] 
    lastPlayer.stop(); 
    lastAudioPlayer = sender.tag; 
    lastPlayer.currentTime = 0; 
    audioPlayer = audioPlayers[sender.tag] 
    audioPlayer.currentTime = 0; 
    audioPlayer.play() 
} 
// This Action allows users to stop the audio 
@IBAction func stop(_ sender: UIButton) { 
    if audioPlayer.isPlaying { 
     audioPlayer.stop() 
    } else { 
     self.audioPlayer.play() 
} 
} 

답변

0

아래에서 확인하십시오 :

또한
import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

let soundFilenames = ["5","8","7","4","6","1","3","2","9"] 
var audioPlayers = [AVAudioPlayer]() 
var lastAudioPlayer = 0 
var audioPlayer = AVAudioPlayer() 
var audioPlayer1 = AVAudioPlayer() 
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 { 
      // Try to do somerhing 
      let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "wav")!); 
      audioPlayer = try AVAudioPlayer(contentsOf:url) 

      audioPlayers.append(audioPlayer) 
     } 
     catch { 
      // Catch the error that is thrown 
      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; 
    audioPlayer = audioPlayers[sender.tag] 
    audioPlayer.currentTime = 0; 
    audioPlayer.play() 
} 

@IBAction func buttonTapped2(_ sender: UIButton) { 
    let lastPlayer = audioPlayers[lastAudioPlayer] 
    lastPlayer.stop(); 
    lastAudioPlayer = sender.tag; 
    lastPlayer.currentTime = 0; 
    audioPlayer = audioPlayers[sender.tag] 
    audioPlayer.currentTime = 0; 
    audioPlayer.play() 
} 
// This Action allows users to stop the audio 
@IBAction func stop(_ sender: UIButton) { 
    if audioPlayer.isPlaying { 
     audioPlayer.stop() 
    } 
} 
+0

내가보기 컨트롤러를 표시합니다 다음 사운드를 재생하고 버튼을 만들고 싶어 문제가 그 내가 원한다 –

+0

당신의 메신저 이름은 무엇입니까 –

+0

그래서 코드 문제로 더 많은 것을 도울 수 있습니다 –