从存储在从结构创建的数组中的选定 table 单元格播放音频
Playing audio from a selected table cell stored in an array created from a Struct
我在 table 中显示了一个环境声音列表,当您单击其中一个时,它会将您带到第二个视图控制器,其中显示声音标题、图像并给出环境声音的描述并显示一个播放按钮,我将所有数据都放在一个结构数组中,但在播放音频时遇到问题,这是我的代码示例:
我的结构:
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)
}
}
}
尝试从 audios
中的 URL 中删除 .mp3
。 URLForResource:withExtension:
单独使用扩展名。
我在 table 中显示了一个环境声音列表,当您单击其中一个时,它会将您带到第二个视图控制器,其中显示声音标题、图像并给出环境声音的描述并显示一个播放按钮,我将所有数据都放在一个结构数组中,但在播放音频时遇到问题,这是我的代码示例:
我的结构:
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)
}
}
}
尝试从 audios
中的 URL 中删除 .mp3
。 URLForResource:withExtension:
单独使用扩展名。