• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 使用 swift 停止音频帮助

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:38:12 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

}
// 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-推荐答案


请检查以下内容:

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap