gpt4 book ai didi

ios - 在TableView中播放不同的声音

转载 作者:行者123 更新时间:2023-11-30 12:54:32 25 4
gpt4 key购买 nike

First, this is how my app looks like

现在,当我单击这些表格 View 单元格内的按钮时,我想播放不同的声音。我对编程非常陌生,我不知道如何做到这一点。我已经为我的声音文件创建了一个数组,但我不知道如何将它们分配给连续的这些按钮。非常感谢您的帮助

这是我的代码

import UIKit
import AVFoundation

class ViewController6: UIViewController, UITableViewDataSource, UITableViewDelegate {

var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


//Array of Rifles

var arrayOfRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"]
var buttonDataRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"]

var soundsArray: [NSURL] = [
NSURL(fileURLWithPath: Bundle.main.path(forResource: "AK47", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "AUGA1", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "FAMAS", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "G36K", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "K1A", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "K2", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M4A1", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M4Silencer", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M16", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M60", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "MicroGALIL", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "QBZ95", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "RPK", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "ScarHeavy", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "ScarLight", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "SG552", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "TAR21", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "Tommy", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "Type89", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "XM8", ofType: "mp3")!)
]




//Functions for tableView

//Cell - For Rifles

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (arrayOfRifles.count)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell6 = tableView.dequeueReusableCell(withIdentifier: "cell6", for: indexPath) as! ViewControllerTableViewCell6

cell6.myImage.image = UIImage(named: arrayOfRifles[indexPath.row] + ".jpg")
cell6.myButton.setTitle(buttonDataRifles[indexPath.row], for: UIControlState.normal)
return (cell6)

cell6.myButton.tag = indexPath.row
cell6.myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)}

@IBAction func buttonAction(_ sender: UIButton) {

print("Button tapped")
let tapedIndex = sender as UIButton
let getUrl = soundsArray[tapedIndex] as! NSURL


}


}

最佳答案

如果你的声音不超过 30 秒,我建议使用 AudioToolbox。该框架为此目的专门设计了功能。

Objective-C

  1. 导入框架

    #import <AudioToolbox/AudioToolbox.h>
  2. 通过创建 SystemSoundID 为您的声音提供引用

    @property (assign, nonatomic) SystemSoundID soundIDOne;
  3. 从一开始就准备好你的儿子

    AudioServicesCreateSystemSoundID ( (__bridge CFURLRef)([[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Swish" ofType:@"wav"]]), &_soundIDDone);
  4. 播放声音

    AudioServicesPlaySystemSound(_soundIDOne);
  5. 如果您需要完成 - 添加处理程序

    AudioServicesAddSystemSoundCompletion(_soundIDOne, NULL, NULL, playSoundFinished, NULL);


    void playSoundFinished (SystemSoundID sound, void *data) {
    //do stuff here
    }

swift 2.2

    import AudioToolbox

private var soundID:SystemSoundID?

if let filePath = NSBundle.mainBundle().pathForResource("name", ofType: "type") {
let pathToResource = NSURL(fileURLWithPath: filePath)
soundID = 0
if var soundID = soundID {
AudioServicesCreateSystemSoundID(pathToResource, &soundID)
}
}

if var soundID = soundID {
AudioServicesPlaySystemSound(soundID)
}

//or create extension

extension SystemSoundID {
static func playSound(soundName: String, fileExtension: String) {
var soundID: SystemSoundID = 0
if let filePath = NSBundle.mainBundle().pathForResource(soundName, ofType: fileExtension) {
let pathToResource = NSURL(fileURLWithPath: filePath)
AudioServicesCreateSystemSoundID(pathToResource, &soundID)
AudioServicesPlaySystemSound(soundID)
}
}
}

//and usage

SystemSoundID.playSound("soundFileName", fileExtension: "extension")

更多请查看here

如果你想使用 AVFoundation 代替,代码如下:

    import AVFoundation

var audioPlayer = AVAudioPlayer() //create player

//create all path to y'r resources

var path:[NSURL] = []

if let filePath = NSBundle.mainBundle().pathForResource("soundName", ofType: "fileExtension") {
let pathToResource = NSURL(fileURLWithPath: filePath)
path.append(pathToResource)
}

//in the very start
do {
if let selectedPath = path.first {
audioPlayer = try AVAudioPlayer(contentsOfURL: selectedPath)
audioPlayer.prepareToPlay()
}
} catch {
print(error)
}

audioPlayer.play()

在这种情况下,由于 AVAudioPlayer 需要 prepareToPlay,因此在播放声音之前您可能会出现一些延迟,为了确保您可以播放声音,您需要对 AVPlayer 进行更复杂的设置并观察玩家status,喜欢的可以引用this link 。另外,如果您的文件持续时间较短,那么这种延迟可能会非常小甚至不存在,无论如何,对于我来说,由于上述原因,这不是播放大量声音文件的首选方式

因为我不能告诉你AVFoundationQueuePlayer,这也是一个很好的解决方案。 Here是苹果示例链接

关于ios - 在TableView中播放不同的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40579027/

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