piano.dart 2.53 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

John McCutchan's avatar
John McCutchan committed
5
import 'package:mojo/mojo/url_response.mojom.dart';
6
import 'package:sky_services/media/media.mojom.dart';
7
import 'package:sky/material.dart';
8
import 'package:sky/rendering.dart';
9
import 'package:sky/services.dart';
10
import 'package:sky/widgets.dart';
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

// All of these sounds are marked as public domain at soundbible.
const String chimes = "http://soundbible.com/grab.php?id=2030&type=wav";
const String chainsaw = "http://soundbible.com/grab.php?id=1391&type=wav";
const String stag = "http://soundbible.com/grab.php?id=2073&type=wav";
const String frogs = "http://soundbible.com/grab.php?id=2033&type=wav";
const String rattle = "http://soundbible.com/grab.php?id=2037&type=wav";
const String iLoveYou = "http://soundbible.com/grab.php?id=2045&type=wav";

class Key {
  Key(this.color, this.soundUrl);

  final Color color;
  final String soundUrl;
  MediaPlayerProxy player;

Adam Barth's avatar
Adam Barth committed
27
  void down() {
28
    if (player == null)
Adam Barth's avatar
Adam Barth committed
29
      return;
30 31 32 33
    player.ptr.seekTo(0);
    player.ptr.start();
  }

Adam Barth's avatar
Adam Barth committed
34
  void up() {
35
    if (player == null)
Adam Barth's avatar
Adam Barth committed
36
      return;
37 38 39 40 41 42
    player.ptr.pause();
  }
}

class PianoApp extends App {
  final List<Key> keys = [
43 44 45 46 47 48
    new Key(Colors.red[500], chimes),
    new Key(Colors.orange[500], chainsaw),
    new Key(Colors.yellow[500], stag),
    new Key(Colors.green[500], frogs),
    new Key(Colors.blue[500], rattle),
    new Key(Colors.purple[500], iLoveYou),
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  ];

  PianoApp() {
    loadSounds();
  }

  loadSounds() async {
    MediaServiceProxy mediaService = new MediaServiceProxy.unbound();
    shell.requestService(null, mediaService);

    for (Key key in keys) {
      MediaPlayerProxy player = new MediaPlayerProxy.unbound();
      mediaService.ptr.createPlayer(player);

      UrlResponse response = await fetchUrl(key.soundUrl);
      await player.ptr.prepare(response.body);
      key.player = player;
    }
    mediaService.close();
    // Are we leaking all the player connections?
  }

  Widget build() {
    List<Widget> children = [];
    for (Key key in keys) {
      children.add(
        new Listener(
          child: new Flexible(
            child: new Container(
              decoration: new BoxDecoration(backgroundColor: key.color)
            )
          ),
          onPointerCancel: (_) => key.up(),
          onPointerDown: (_) => key.down(),
          onPointerUp: (_) => key.up()
        )
      );
    }

88
    return new Column(children);
89 90 91 92 93 94
  }
}

void main() {
  runApp(new PianoApp());
}