AVAudioPlayer (3)

sliderAndVolume

ボタン(play/pause)を2つと、スライダー(progress/volume)とを、追加しました。 Interface Builderを使うのに慣れてきたようです。

//  AppDelegate.swift

import Cocoa
import AVFoundation

var player : AVAudioPlayer?

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate, AVAudioPlayerDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var myCustomView: MyView!
    @IBOutlet weak var sliderValue: NSSliderCell!
    @IBOutlet weak var volumeValue: NSSlider!

    @IBAction func playPushed(sender: AnyObject) {
        player!.play()
    }
    @IBAction func pausePushed(sender: AnyObject) {
        player!.pause()
    }
    @IBAction func sliderChanged(sender: AnyObject) {
        player!.currentTime = NSTimeInterval(sliderValue.integerValue)
    }
    @IBAction func volumeChanged(sender: AnyObject) {
        player!.volume = volumeValue.floatValue
    }
    
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        self.window.delegate = self
        NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "update", userInfo: nil, repeats: true)
        
        let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("test", ofType: "wav")!)
        player  = try! AVAudioPlayer(contentsOfURL: audioPath, fileTypeHint: nil)
        player!.meteringEnabled = true
        player!.numberOfLoops = -1
        player!.prepareToPlay()
        player!.delegate = self
        player!.play()
        
        sliderValue.maxValue = Double(player!.duration)
    }

    func update() {
        sliderValue.integerValue = Int(player!.currentTime)
        myCustomView.display()
    }
    
}
//  MyView.swift

import Cocoa

class MyView: NSView {
    
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        
        NSColor.grayColor().set()
        NSRectFill(self.bounds)
        let dur = player?.duration
        let cur = player?.currentTime
        
        if cur != nil && dur != nil {
            for var ch: Int = 0; ch < 2; ch++ {
                switch ch {
                case 0: NSColor(red: 0.8, green: 1.0, blue: 0.4, alpha: 1.0).set()
                case 1: NSColor(red: 0.2, green: 1.0, blue: 0.8, alpha: 1.0).set()
                default: break
                }
                player?.updateMeters()
                let ave = player?.averagePowerForChannel(ch)
                if ave != nil {
                    let nbar = CGFloat (600.0 * (ave! + 160.0) / 160.0)
                    let y2   = CGFloat (10 + 35 * ch)
                    let rect2 = NSMakeRect(20, y2, nbar, 30)
                    NSRectFill(rect2)
                }
                
                NSColor(red: 1.0, green: 0.4, blue: 0.2, alpha: 1.0).set()

                let peak = player?.peakPowerForChannel(ch)
                if peak != nil {
                    let nbar = CGFloat (600.0 * (peak! + 160.0) / 160.0)
                    let y2   = CGFloat (10 + 35 * ch)
                    let rect2 = NSMakeRect(nbar, y2, 20, 30)
                    NSRectFill(rect2)
                }
            }
            
            let font = NSFont.boldSystemFontOfSize(40)
            let textFontAttributes = [
                NSFontAttributeName: font,
                NSForegroundColorAttributeName: NSColor.whiteColor()
            ]
            
            let dd : Int = Int(dur!)
            let cc : Int = Int(cur!)
            let s:String = " duration: \(dd)sec, current: \(cc)sec"
            s.drawInRect(self.bounds, withAttributes: textFontAttributes)
        }
    }
}