Swift3です
以下のコードで、ボタンを押した時間が短すぎると(チョン押し)音がなりません。
Apple製のGaragebandのように、押している間だけ音がなり、チョン押しでも少しだけ音がなる仕様を実現したいです。
ご教授願います
import UIKit
import AVFoundation
class ViewController: UIViewController {
let pianoKeys = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] var audioPlayers:[AVAudioPlayer] = [] override func viewDidLoad() { super.viewDidLoad() for key in pianoKeys { let pianoSoundURL = URL(fileURLWithPath: Bundle.main.path(forResource: key, ofType: "m4a")!) do { let audioPlayer = try AVAudioPlayer(contentsOf: pianoSoundURL) audioPlayer.prepareToPlay() audioPlayers.append(audioPlayer) } catch { } } func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
}
@IBAction func Pushed(_ sender: Any) { let keynum = (sender as AnyObject).tag audioPlayers[keynum!].currentTime = 1 audioPlayers[keynum!].play() //touchdown } @IBAction func Released(_ sender: Any) { let keynum = (sender as AnyObject).tag Thread.sleep(forTimeInterval: 0.03) audioPlayers[keynum!].pause() //touch up inside } @IBAction func Releasedoutside(_ sender: Any) { let keynum = (sender as AnyObject).tag Thread.sleep(forTimeInterval: 0.03) audioPlayers[keynum!].pause() //touch up outside }
あなたの回答
tips
プレビュー