Flutter Engine
The Flutter Engine
AudioSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
11#include "src/base/SkUtils.h"
12#include "tools/Resources.h"
14
16 std::unique_ptr<SkAudioPlayer> fPlayer;
17 SkRect fBar;
18
19public:
20 AudioSlide() { fName = "Audio"; }
21
22 void load(SkScalar w, SkScalar h) override {
23 auto data = SkData::MakeFromFileName("/Users/reed/skia/mp3/scott-joplin-peacherine-rag.mp3");
24 if (data) {
25 fPlayer = SkAudioPlayer::Make(data);
26
27 SkDebugf("make: dur:%g time%g state:%d",
28 fPlayer->duration(),
29 fPlayer->time(),
30 (int)fPlayer->state());
31 }
32
33 fBar = { 10, 10, 510, 30 };
34 }
35
36 void draw(SkCanvas* canvas) override {
37 if (!fPlayer) {
38 return;
39 }
40
41 SkPaint p;
42 p.setColor(0xFFCCCCCC);
43 canvas->drawRect(fBar, p);
44
45 p.setColor(fPlayer->isPlaying() ? SK_ColorBLUE : 0xFF8888FF);
46 SkRect r = fBar;
47 r.fRight = r.fLeft + (float)fPlayer->normalizedTime() * r.width();
48 canvas->drawRect(r, p);
49 }
50
51 bool onChar(SkUnichar c) override {
52 if (c == ' ') {
53 switch (fPlayer->state()) {
54 case SkAudioPlayer::State::kPlaying: fPlayer->pause(); break;
55 case SkAudioPlayer::State::kPaused: fPlayer->play(); break;
56 case SkAudioPlayer::State::kStopped: fPlayer->play(); break;
57 }
58 return true;
59 }
60 return this->ClickHandlerSlide::onChar(c);
61 }
62
64 if (fPlayer && fBar.contains(x, y)) {
65 bool wasPlaying = fPlayer->isPlaying();
66 if (wasPlaying) {
67 fPlayer->pause();
68 }
69 return new Click([this, wasPlaying](Click* click) {
70 if (fBar.contains(click->fCurr.fX, click->fCurr.fY)) {
71 fPlayer->setNormalizedTime((click->fCurr.fX - fBar.fLeft) / fBar.width());
72 }
73
74 if (click->fState == skui::InputState::kUp) {
75 if (wasPlaying) {
76 fPlayer->play();
77 }
78 }
79 return true;
80 });
81 }
82 return nullptr;
83 }
84
85 bool onClick(ClickHandlerSlide::Click*) override { return false; }
86
87 bool animate(double /*nanos*/) override {
88 return true;
89 }
90
91};
92
93DEF_SLIDE( return new AudioSlide; )
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
int32_t SkUnichar
Definition: SkTypes.h:175
#define DEF_SLIDE(code)
Definition: Slide.h:25
bool onChar(SkUnichar c) override
Definition: AudioSlide.cpp:51
Click * onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override
Definition: AudioSlide.cpp:63
void draw(SkCanvas *canvas) override
Definition: AudioSlide.cpp:36
void load(SkScalar w, SkScalar h) override
Definition: AudioSlide.cpp:22
bool animate(double) override
Definition: AudioSlide.cpp:87
bool onClick(ClickHandlerSlide::Click *) override
Definition: AudioSlide.cpp:85
static std::unique_ptr< SkAudioPlayer > Make(sk_sp< SkData >)
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition: SkData.cpp:148
virtual bool onChar(SkUnichar c)
Definition: Slide.h:44
SkString fName
Definition: Slide.h:54
float SkScalar
Definition: extension.cpp:12
if(end==-1)
double y
double x
ModifierKey
Definition: ModifierKey.h:9
SkScalar w
SkScalar h
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
bool contains(SkScalar x, SkScalar y) const
Definition: extension.cpp:19
constexpr float width() const
Definition: SkRect.h:762
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63