xcode에 추가 한 폴더에서 일부 노래를 재생하고 싶습니다.swift : 인덱스가 범위를 벗어났습니다.
나는 탭 응용 프로그램을 사용하고 코드는 다음과 같다 :
func playThis(thisOne:String)
{
do
{
let audioPath = Bundle.main.path(forResource: thisOne, ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
audioPlayer.play()
}
catch
{
print ("ERROR")
}
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = songs[thisSong] //error index out of range
}
을하지만 그것을 실행할 때, 노래는 처음 뷰 컨트롤러에 표시되지 나는 두 번째보기 컨트롤러 탭 응용 프로그램 충돌을 클릭하면 상기 이유는 다음
인덱스 범위 밖에
첫 번째 뷰 컨트롤러의 코드는 다음과 같다 :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
do
{
let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
audioPlayer.play()
thisSong = indexPath.row
audioStuffed = true
}
catch
{
print ("ERROR")
}
}
두 번째 뷰 컨트롤러 코드 :
override func viewDidLoad()
{
super.viewDidLoad()
gettingSongNames()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//FUNCTION THAT GETS THE NAME OF THE SONGS
func gettingSongNames()
{
let folderURL = URL(fileURLWithPath:Bundle.main.resourcePath!)
do
{
let songPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
//loop through the found urls
for song in songPath
{
var mySong = song.absoluteString
if mySong.contains(".mp3")
{
let findString = mySong.components(separatedBy: "/")
mySong = findString[findString.count-1]
mySong = mySong.replacingOccurrences(of: "%20", with: " ")
mySong = mySong.replacingOccurrences(of: ".mp3", with: "")
songs.append(mySong)
}
}
myTableView.reloadData()
}
catch
{
print ("ERROR")
}
}
import UIKit
import AVFoundation
var audioPlayer = AVAudioPlayer()
var songs:[String] = []
var thisSong = 0
var audioStuffed = false
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return songs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = songs[indexPath.row]
return cell
}
내 방법은 상세 뷰 컨트롤러에 노래를 전달하는
는'songs'? 채워지는 곳은 어디입니까? 'thisSong'은 어디에서 정의 되는가? – Larme
첫 번째보기 컨트롤러에서 온 것입니다. 변경되었습니다 .. 더보기 –
노래보기 및 노래를 자세히보기 컨트롤러에 전달하는 방법은 무엇입니까? –