LoginSignup
9
9

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-03-31

wp_ss_20160401_0001.png

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

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

実装

UWPで作ってます。UWP歴3時間。
https://github.com/iwata-n/GetWild

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 を参照してください

namespace GetWild
{

    /// <summary>
    /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private LightSensor sensor;
        private uint thresholdValue;
        private MediaElement media = new MediaElement();

        public MainPage()
        {
            this.InitializeComponent();

            this.sensor = LightSensor.GetDefault();
            if (sensor == null)
            {
            }
        }

        async private void getWildAndTough()
        {
            if (media.CurrentState == MediaElementState.Playing)
            {
                return;
            }
            var uri = new System.Uri("ms-appx:///Assets/getwild.mp3");
            var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            media.SetSource(stream, file.ContentType);
            media.Play();
        }

        /// <summary>
        /// ページが表示された時に実行
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // センサのコールバックの登録
            this.sensor.ReadingChanged += new TypedEventHandler<LightSensor, LightSensorReadingChangedEventArgs>(ReadingChanged);

            this.threshold_slider.ValueChanged += new RangeBaseValueChangedEventHandler(ValueChanged);
        }

        async private void ReadingChanged(object sender, LightSensorReadingChangedEventArgs e)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                LightSensorReading reading = e.Reading;
                this.nowValue.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);

                if ((uint)reading.IlluminanceInLux < this.thresholdValue)
                {
                    getWildAndTough();
                }
            });
        }

        async private void ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                this.thresholdValue = (uint)(e.NewValue) * 10;
                this.threshold.Text = String.Format("{0,5:0.00}", this.thresholdValue);
            });
        }
    }
}

メモ

  • センサが無いと死ぬ
  • Assetsにgetwild.mp3が無いと死ぬ
  • 画面が暗くなると曲が止まる
  • 設定値は覚えてない
9
9
0

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
9
9