flutter/examples/widgets/piano.dart
Adam Barth 4467a268ce Move theme into material.dart
Also, introduce Colors and Typography to hold the material colors and the
typography declarations. Previously we expected clients of these libraries to
import them into a namespace, but that doesn't play nice with re-exporting them
from material.dart.
2015-09-18 09:57:21 -07:00

97 lines
2.7 KiB
Dart

// 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.
import 'package:mojo/mojo/url_response.mojom.dart';
import 'package:sky_services/media/media.mojom.dart';
import 'package:sky/material.dart';
import 'package:sky/rendering.dart';
import 'package:sky/services.dart';
import 'package:sky/widgets.dart';
// 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;
EventDisposition down() {
if (player == null)
return EventDisposition.ignored;
player.ptr.seekTo(0);
player.ptr.start();
return EventDisposition.processed;
}
EventDisposition up() {
if (player == null)
return EventDisposition.ignored;
player.ptr.pause();
return EventDisposition.processed;
}
}
class PianoApp extends App {
final List<Key> keys = [
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),
];
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()
)
);
}
return new Column(children);
}
}
void main() {
runApp(new PianoApp());
}