LoginSignup
2
3

More than 5 years have passed since last update.

AppleWatchで寿司を廻してスシ食いねェ!

Last updated at Posted at 2016-11-26

はじめに

この記事は同じ kosen11s@pittanko_pta さん(以下、ぴったんさん)が元ネタです。

元ネタを Objective-C から Swift にコンバートして音声を流すというものです。

環境

  • WatchOS 3.1 (Apple Watch Serise 2)
  • iOS 10.1 (iPhone SE)
  • Xcode8.1
  • えるたそ(OSX 10.11.6)
  • Swift3.0.1

ネタの準備

ぴったんさんから許可を頂き画像を使用させていただいております。

ソースコード

ProjectName WatchKit Extension/InterfaceViewController.swift を以下のようにしました。

InterfaceViewController.swift
import WatchKit
import Foundation
import AVFoundation

class InterfaceController: WKInterfaceController {

    @IBOutlet var messageLabel: WKInterfaceLabel!
    @IBOutlet var sushiImage: WKInterfaceImage!

    let engine = AVAudioEngine()
    let audioPlayerNode = AVAudioPlayerNode()
    var audioFile:AVAudioFile!
    var imageNumber: Int!

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        setup()
        setupAudio()
    }

    override func willActivate() {
        super.willActivate()
    }

    override func didDeactivate() {
        super.didDeactivate()
    }

    fileprivate func setup() {
        crownSequencer.focus()
        crownSequencer.delegate = self
        imageNumber = 1
        messageLabel.setText("寿司を廻せ!!!!")
        sushiImage.setImageNamed("sushi_1")
    }

    fileprivate func setupAudio() {
        do {
            let path = Bundle.main.path(forResource: "sushi", ofType: "mp3")
            if let path = path {
                audioFile = try AVAudioFile(forReading: URL(fileURLWithPath: path))
            }
        } catch {
            fatalError("\(error)")
        }
        engine.attach(audioPlayerNode)

        if let file = audioFile {
            engine.connect(audioPlayerNode, to: engine.mainMixerNode, format: file.processingFormat)
        }

        do {
            try self.engine.start()
        } catch {
            fatalError("\(error)")
        }
    }
}

extension InterfaceController : WKCrownDelegate {
    func crownDidRotate(_ crownSequencer: WKCrownSequencer?,
                        rotationalDelta: Double) {
        if let file = audioFile {
            audioPlayerNode.scheduleFile(file, at: nil, completionHandler: nil)
            audioPlayerNode.play()
        }


        if (rotationalDelta > 0) {
            imageNumber = imageNumber + 1
        }

        if (rotationalDelta < 0) {
            imageNumber = imageNumber - 1
        }

        if (imageNumber == 45) {
            imageNumber = 1
        }

        if (imageNumber == 0) {
            imageNumber = 44
        }

        let imageName = String(format: "sushi_%zd", imageNumber)
        sushiImage.setImageNamed(imageName)
        messageLabel.setText("おすしぐるぐる")
    }

    func crownDidBecomeIdle(_ crownSequencer: WKCrownSequencer?) {
        if let file = audioFile {
            audioPlayerNode.scheduleFile(file, at: nil, completionHandler: nil)
            audioPlayerNode.pause()
        }
        messageLabel.setText("( `o´)ヘイラッシャイ")
    }
}

最後に

let path = Bundle.main.path(forResource: "sushi", ofType: "mp3") で指定しているファイルは sushi.mp3 でローカルにたまたま転がっていた音声をXcodeプロジェクトの中に叩き込みました。
DigitalCrownを回している間に音楽が流れます。

リポジトリ

2
3
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
2
3