Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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というタイトルの曲が端末に入ってないとたぶんクラッシュするので買いましょう。

https://itunes.apple.com/jp/album/get-wild/id569842261?i=569842264

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

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

変更履歴

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

追記

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした