Ios 从存储在从结构创建的数组中的选定表单元格播放音频
我有一个表中显示的环境声音列表,当您单击其中一个时,它会将您带到第二个视图控制器,该控制器显示声音标题,显示图像,给出环境声音的描述,并显示播放按钮。我从结构中获取数组中的所有数据,但在播放音频时遇到问题,下面是我的代码示例: 我的结构:Ios 从存储在从结构创建的数组中的选定表单元格播放音频,ios,arrays,swift,audio-player,Ios,Arrays,Swift,Audio Player,我有一个表中显示的环境声音列表,当您单击其中一个时,它会将您带到第二个视图控制器,该控制器显示声音标题,显示图像,给出环境声音的描述,并显示播放按钮。我从结构中获取数组中的所有数据,但在播放音频时遇到问题,下面是我的代码示例: 我的结构: import UIKit import AVFoundation import Foundation struct Audio { var title: String var imageName: UIImage var descr:
import UIKit
import AVFoundation
import Foundation
struct Audio {
var title: String
var imageName: UIImage
var descr: String
var audioURL: String
}
我的视图控制器:
import UIKit
import AVFoundation
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let audios = [Audio(title: "Bubbling Pools", imageName: UIImage(named: ("1.png"))!, descr: "The stench of life fills the air. Viscous fluid bubbles to the surface in great exhortations of gas and moisture. Something about these pools is familiar..", audioURL: "2_Bubbling_Pools.mp3"),
Audio(title: "March Of Faith", imageName: UIImage(named: ("2.png"))!, descr: "The devoted stream into the distance. The dust from their sandaled feet blocks out the sun for miles around. There's no stopping them. They believe.", audioURL: "3_The_March_of_the_Faithful.mp3"),
Audio(title: "Solemn Vow", imageName: UIImage(named: ("3.png"))!, descr: "When death is near, and retreat is not an option. Only the bravest have any purchase on the word \"honor\".", audioURL: "4_Solemn_Vow-a.mp3")
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return audios.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let audio = audios[indexPath.row]
cell.textLabel?.text = audio.title
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let audio = audios[indexPath.row]
performSegue(withIdentifier: "segue", sender: audio)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let audio = sender as! Audio
let destinationVC = segue.destination as! SecondViewController
destinationVC.selectedAudio = audio
}
}
@IBOutlet weak var tableView: UITableView!
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
import UIKit
import AVFoundation
class SecondViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var artworkImage: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var playButton: UIButton!
var selectedAudio: Audio!
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
titleLabel.text = selectedAudio.title
artworkImage.image = selectedAudio.imageName
descriptionLabel.text = selectedAudio.descr
}
@IBAction func playButtonPressed(_ sender: Any) {
playAudio()
}
var player: AVAudioPlayer?
func playAudio() {
let url = Bundle.main.url(forResource: selectedAudio.audioURL, withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
}
}
我的第二个视图控制器:
import UIKit
import AVFoundation
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let audios = [Audio(title: "Bubbling Pools", imageName: UIImage(named: ("1.png"))!, descr: "The stench of life fills the air. Viscous fluid bubbles to the surface in great exhortations of gas and moisture. Something about these pools is familiar..", audioURL: "2_Bubbling_Pools.mp3"),
Audio(title: "March Of Faith", imageName: UIImage(named: ("2.png"))!, descr: "The devoted stream into the distance. The dust from their sandaled feet blocks out the sun for miles around. There's no stopping them. They believe.", audioURL: "3_The_March_of_the_Faithful.mp3"),
Audio(title: "Solemn Vow", imageName: UIImage(named: ("3.png"))!, descr: "When death is near, and retreat is not an option. Only the bravest have any purchase on the word \"honor\".", audioURL: "4_Solemn_Vow-a.mp3")
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return audios.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let audio = audios[indexPath.row]
cell.textLabel?.text = audio.title
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let audio = audios[indexPath.row]
performSegue(withIdentifier: "segue", sender: audio)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let audio = sender as! Audio
let destinationVC = segue.destination as! SecondViewController
destinationVC.selectedAudio = audio
}
}
@IBOutlet weak var tableView: UITableView!
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
import UIKit
import AVFoundation
class SecondViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var artworkImage: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var playButton: UIButton!
var selectedAudio: Audio!
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
titleLabel.text = selectedAudio.title
artworkImage.image = selectedAudio.imageName
descriptionLabel.text = selectedAudio.descr
}
@IBAction func playButtonPressed(_ sender: Any) {
playAudio()
}
var player: AVAudioPlayer?
func playAudio() {
let url = Bundle.main.url(forResource: selectedAudio.audioURL, withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
}
}
尝试从
audios
中的URL中删除.mp3
URLForResource:withExtension:
单独使用扩展名。您是否已到达调用player.play()的位置(确保通过输出或断点)?连接到上面:您是否已确保url是您喜欢的(仅使用文件名似乎有问题)并且文件确实存在?