ios - 如何在播放时实现与歌曲对应的搜索?

标签 ios swift uitableview avaudioplayer

这是我的代码。当我从搜索栏播放歌曲时,它与我打算播放的实际歌曲不对应。如有任何建议,我们将不胜感激。

import UIKit
import AVFoundation

var thisSong4 = 0
var audioStuffed4 = false
var filteredArray = [String]()

let songsArray4 = ["1", "2", "3", "4", "5", "6"]

class SongsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UITextFieldDelegate {
    @IBOutlet var searchBar: UISearchBar!
    @IBOutlet var myTableView: UITableView!

    var isSearching = false

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

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

        cell.textLabel?.text = filteredArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if isSearching == false{
            do{
                let audioPath = Bundle.main.path(forResource: songsArray4[indexPath.row], ofType: ".m4a")
                try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
                audioPlayer.play()
                thisSong4 = indexPath.row
                audioStuffed4 = true
            }
            catch{
                print("ERROR")
            }
        }else {
           //what should I fill in here?
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.delegate = self
        searchBar.returnKeyType = UIReturnKeyType.done
        myTableView.dataSource = self
        filteredArray = songsArray4
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true;
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
        if searchBar.text == nil || searchBar.text == "" {
            isSearching = false

            self.view.endEditing(true)

            searchBar.perform(#selector(self.resignFirstResponder), with: nil, afterDelay: 0.1)

            filteredArray = songsArray4

            myTableView.reloadData()
        } else {
            isSearching = true

            filteredArray = searchText.isEmpty ? songsArray4: songsArray4.filter{(item: String) -> Bool in return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
            }

            if searchText != "" {
                isSearching = true
                myTableView.reloadData()
            }
        }
    }
}

最佳答案

除了从 filteredArray 而不是 songsArray4 获取路径外,您在不进行搜索时会执行相同的操作。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let array = isSearching ? filteredArray : songsArray4

    do {
        let audioURL = Bundle.main.url(forResource: array[indexPath.row], withExtension: ".m4a")!
        try audioPlayer = AVAudioPlayer(contentsOf: audioURL)
        audioPlayer.play()
        thisSong4 = indexPath.row
        audioStuffed4 = true
    } catch {
        print("ERROR")
    }
}

你真的需要移动这些行:

var thisSong4 = 0
var audioStuffed4 = false
var filteredArray = [String]()
let songsArray4 = ["1", "2", "3", "4", "5", "6"]

到你类(class)的内部。这些应该是类属性,而不是全局属性。

关于ios - 如何在播放时实现与歌曲对应的搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50196804/

相关文章:

ios - 出于某种原因搜索栏未调用 textDidChange

ios - 如何根据Apple ID在iOS中检查用户是否已购买或未使用应用内购买

ios - ios 中的 mapbox direction api 错误路线

json - 转换 JSON 对象以从中提取数据时出错

ios - 如何在 View 出现之前滚动到 iPhone 上 UITableView 的底部

iphone - ViewDidLoad 方法不保留变量?

objective-c - setFetchBatchSize 似乎无法正常工作

swift - url.appendPathComponent 产生错误不能在不可变值 : function call returns 上使用变异成员

ios - 如何从 UiTableViewCell 更改 View Controller 中字典的值

ios - 如何仅在 swift 中重新加载 tableViewCell?