少しやってみたところ、numberOfLoopsで再生回数の取得は辛そうです。単に技術不足かもしれませんが・・
numberOfLoopsでなく、1曲づつ再生し、再生の終了イベント時に呼ばれるデリゲートメソッドを実装し、そこで手動でカウントを行ったところ、やりたいことができるように思えます。
swift
1import UIKit
2import AVFoundation
3
4class ViewController: UIViewController,AVAudioPlayerDelegate{
5 var audioPlayer = AVAudioPlayer()
6 var count = 1
7
8 override func viewDidLoad() {
9 super.viewDidLoad()
10 let title = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("fluorescent_switch1", ofType: "mp3")!)
11 do {
12 audioPlayer = try AVAudioPlayer(contentsOfURL: title, fileTypeHint: nil)
13 audioPlayer.prepareToPlay()
14 audioPlayer.enableRate = true
15 audioPlayer.delegate = self
16 print("good!")
17 } catch {
18 print("not good...")
19 }
20 // Do any additional setup after loading the view, typically from a nib.
21 }
22
23 @IBAction func pushPlayButton(sender: UIButton) {
24 if !audioPlayer.playing {
25 audioPlayer.play()
26 }else{
27 audioPlayer.stop()
28 }
29
30 }
31
32 func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
33 print("finish")
34 count = count+1
35 print(count)
36 if count <= 3 {
37 //1倍
38 audioPlayer.rate = 1.0
39 audioPlayer.play()
40 }else if count <= 6 {
41 //0.7倍
42 audioPlayer.rate = 0.7
43 audioPlayer.play()
44 }else if count <= 9 {
45 //1.5倍
46 audioPlayer.rate = 1.5
47 audioPlayer.play()
48 }else{
49 //なし:再生しない
50 }
51 }
52
53}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/10 08:53