我在页面控制中有几页。在每一页上,都有文字和图片,每页都会改变。我想在每个页面上放置不同的声音文件。我试图用一个按钮在每个页面上设置不同的功能。但是什么也没出来。
@IBOutlet weak var playButtonAkbar: UIButton!
var audioFile1: AVAudioPlayer?
var audioFile2: AVAudioPlayer?
@IBOutlet weak var pageControl: UIPageControl!
var slides:[Slide] = [];
override func viewDidLoad() {
super.viewDidLoad()
let sound = Bundle.main.path(forResource: "001", ofType: "mp3")
do {
audioFile1 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
} catch {
print(error)
}
let sound2 = Bundle.main.path(forResource: "002", ofType: "mp3")
do {
audioFile2 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound2!))
} catch {
print(error)
}
slides = createSlides()
setupSlideScrollView(slides: slides)
pageControl.numberOfPages = slides.count
pageControl.currentPage = 0
view.bringSubview(toFront: pageControl)
}
func createSlides() -> [Slide] {
let slide1:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide
slide1.imageView.image = UIImage(named: "001")
slide1.labelTitle.text = "Text1"
func playButtonAkbar (_ sender: Any) {
audioFile1?.play()
}
let slide2:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide
slide2.imageView.image = UIImage(named: "002")
slide2.labelTitle.text = "Text2"
func playButtonAkbar (_ sender: Any) {
audioFile1?.play()
}