当我制作一个停止音频的按钮并点击一次时,它会停止音频。当我再次点击它时,它会从停止的点恢复音频。
这是那部分的代码,它也在下面的代码中,
}
// 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()
}
}
Best Answer-推荐答案 strong>
请检查以下内容:
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()
}
}
关于ios - 使用 swift 停止音频帮助,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/46083686/
|