piano.dart 2.55 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 8 9
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// 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
26
  void down() {
27
    if (player == null)
Adam Barth's avatar
Adam Barth committed
28
      return;
29 30 31 32
    player.ptr.seekTo(0);
    player.ptr.start();
  }

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

40
class PianoApp extends StatelessComponent {
41
  final List<Key> keys = [
42 43 44 45 46 47
    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),
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  ];

  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?
  }

70
  Widget build(BuildContext context) {
71 72 73
    List<Widget> children = [];
    for (Key key in keys) {
      children.add(
74 75
        new Flexible(
          child: new Listener(
76 77
            child: new Container(
              decoration: new BoxDecoration(backgroundColor: key.color)
78 79 80 81 82
            ),
            onPointerCancel: (_) => key.up(),
            onPointerDown: (_) => key.down(),
            onPointerUp: (_) => key.up()
          )
83 84 85 86
        )
      );
    }

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

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