LoginSignup
466

More than 5 years have passed since last update.

[iOS]部屋の灯りが消えたら自動でGet Wildを再生してGet Wild退勤する

Last updated at Posted at 2016-03-28

https://twitter.com/kozeni_shkt/status/709743397196541953
http://www.b-ch.com/ttl/index.php?ttl_c=467

照度センサーという事で最初arduinoが思い浮かんだのだけれど、一般のご家庭やオフィスにarduinoは無いと思うのでiOSでやった。使わなくなったiPadにアプリを入れてオフィスの出入口に置いておく運用イメージ。

IMG_1679.jpg

設定した閾値をディスプレイの輝度(部屋の照度)が下回ったらGet Wildし始める。なお手を抜いてるので閾値以下で輝度が変化するたびにGet Wildされる。

//
//  ViewController.swift
//  gettlod
//
//  Created by ouba on 2016/03/28.
//  Copyright © 2016年 oubakiou. All rights reserved.
//

import UIKit
import MediaPlayer

class ViewController: UIViewController {

    @IBOutlet var blightnessLabel: UILabel!
    var blightness: Float = 0.0

    @IBOutlet var thresholdSlider: UISlider!
    @IBOutlet var thresholdLabel: UILabel!
    var threshold: Float = 0.0

    var audio: AVAudioPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(animated: Bool) {
        blightness = Float(UIScreen.mainScreen().brightness)
        blightnessLabel.text = String(format: "%.1f", blightness)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(brightnessDidChange(_:)), name: UIScreenBrightnessDidChangeNotification, object: nil)

        threshold = thresholdSlider.value*0.1
        thresholdLabel.text = String(format: "%.1f", threshold)

        thresholdSlider.addTarget(self, action: #selector(thresholdSliderValueDidChange(_:)), forControlEvents: UIControlEvents.ValueChanged)

        checkThreshold()
    }

    internal func brightnessDidChange(notification: NSNotification) {
        blightness = Float(UIScreen.mainScreen().brightness)
        blightnessLabel.text = String(format: "%.1f", blightness)
        checkThreshold()
    }

    internal func thresholdSliderValueDidChange(sender :UISlider) {
        threshold = thresholdSlider.value*0.1
        thresholdLabel.text = String(format: "%.1f", threshold)
        checkThreshold()
    }

    internal func checkThreshold() {
        if (blightness <= threshold) {
            getWildAndTough()
        }
    }

    internal func getWildAndTough() {
        let item: MPMediaItem = getMediaItemBySongFreeword("Get Wild")
        let url: NSURL = item.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL
        do {
            audio = try AVAudioPlayer(contentsOfURL: url, fileTypeHint: nil)
            audio!.play()
        } catch {
            // nothing to do
            print(error)
        }
    }

    internal func getMediaItemBySongFreeword(songFreeword : NSString) -> MPMediaItem {
        let property: MPMediaPropertyPredicate = MPMediaPropertyPredicate(value: songFreeword, forProperty: MPMediaItemPropertyTitle)
        let query: MPMediaQuery = MPMediaQuery()
        query.addFilterPredicate(property)
        let items: [MPMediaItem] = query.items! as [MPMediaItem]
        return items[items.count - 1]
    }
}

Swift歴6時間なのでサンプルコードとして不適切な書き方も含まれているかもしれない。なおGet Wildというタイトルの曲が端末に入ってないとたぶんクラッシュするので買いましょう。

実運用上の注意点としては、

  • iPhoneのスリープ設定を無効化して常にフォアグラウンドでこのアプリを起動し続けている必要がある
  • 部屋が暗くなってからディスプレイ輝度がすぐに対応して下がるとは限らないので、曲が始まるまで黄昏た顔で夜の街を見つめながら待つ、など運用でカバーする必要がある

変更履歴

提案によりplayGetWildをgetWildAndToughへ変更するリファクタリングを行った。thx @WorldDownTown

追記

GetWild退勤的なセンサーアプリを簡単に作れるフレームワークを作ったのでiPhoneに歯痛を心配されたり色々する

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
466