gpt4 book ai didi

ios - 使用 swift 停止音频帮助

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:10 24 4
gpt4 key购买 nike

当我制作一个停止音频的按钮并点击一次时,它会停止音频。当我再次点击它时,它会从它停止的地方恢复音频。这是那部分的代码,也在下面的代码中,

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

最佳答案

请检查以下内容:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com