gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:59:45 24 4
gpt4 key购买 nike

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

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/

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