代码之家  ›  专栏  ›  技术社区  ›  Jared Miller

使用swift停止音频帮助

  •  2
  • Jared Miller  · 技术社区  · 8 年前

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

    我希望音频能够在我按下按钮时停止,这样无论我按下多少次按钮停止音频,它都不会恢复音频。但是如果我按下按钮播放声音,他们就会再次播放声音。

    这是我的密码,

    //
    //  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()
    }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Vini App    8 年前

    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()
        }
    }