首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在TableView中播放不同的声音

在TableView中播放不同的声音
EN

Stack Overflow用户
提问于 2016-11-14 05:29:06
回答 2查看 963关注 0票数 0

现在,当我单击这些表视图单元格中的按钮时,我想播放不同的声音。我在编程方面是个新手,我不知道该怎么做。我已经为我的声音文件创建了一个数组,但我不知道如何将它们分配给一行中的按钮。非常感谢您的帮助

这是我的代码

代码语言:javascript
运行
复制
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


    }


}
EN

回答 2

Stack Overflow用户

发布于 2016-11-14 15:30:13

首先,您需要向按钮添加操作,并为该按钮分配一些标记,以确定哪个按钮是cellForRowAt上clicked.do一些代码

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

func buttonAction(sender: UIButton!) 
{ 
print("Button tapped")
let tapedIndex = sender as! UIButton
let getUrl = buttonDataRifles[tapedIndex.tag] as! NSURL 
}
票数 0
EN

Stack Overflow用户

发布于 2016-11-14 15:59:11

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

Objective-C

  1. 导入框架

#通过创建SystemSoundID like为您的声音导入

  • make引用

@property (assign,非原子) soundIDOne;

  • Prepare SystemSoundID you'r sonds in the sonds

AudioServicesPlaySystemSound(_soundIDOne);

  • If %u需要完成-添加处理程序

AudioServicesAddSystemSoundCompletion(_soundIDOne,NULL,NULL,playSoundFinished,NULL);playSoundFinished playSoundFinished (SystemSoundID声音,空*数据){//在此处执行操作}

Swift 2.2

代码语言:javascript
运行
复制
    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,代码将如下所示:

代码语言:javascript
运行
复制
    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()

此外,如果你有短持续时间的文件,这个延迟可以是非常小的,甚至没有,无论如何,对于我来说,由于上述原因,这不是播放大量声音文件的首选方式

当然我不能告诉你关于AVFoundationQueuePlayer的事情,这也是一个很好的解决方案。Here是苹果的示例链接

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40579027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档