播放音频

This commit is contained in:
toly
2023-05-02 10:03:16 +08:00
parent d6c4f9be05
commit 46a1cd177c
12 changed files with 308 additions and 15 deletions

View File

@@ -0,0 +1,32 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class MuyuAppBar extends StatelessWidget implements PreferredSizeWidget {
final VoidCallback onTapHistory;
const MuyuAppBar({
Key? key,
required this.onTapHistory,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return AppBar(
elevation: 0,
systemOverlayStyle: const SystemUiOverlayStyle(
statusBarIconBrightness: Brightness.dark,
statusBarColor: Colors.transparent),
backgroundColor: Colors.white,
titleTextStyle: const TextStyle(
color: Colors.black, fontSize: 16, fontWeight: FontWeight.bold),
iconTheme: const IconThemeData(color: Colors.black),
title: const Text("电子木鱼"),
actions: [
IconButton(onPressed: onTapHistory, icon: const Icon(Icons.history))
],
);
}
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}

View File

@@ -1,10 +1,13 @@
import 'dart:math';
// import 'package:audioplayers/audioplayers.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_first_station/muyu/muyu_image.dart';
import 'count_panel.dart';
import 'muyu_app_bar.dart';
class MuyuPage extends StatefulWidget {
const MuyuPage({Key? key}) : super(key: key);
@@ -14,25 +17,29 @@ class MuyuPage extends StatefulWidget {
}
class _MuyuPageState extends State<MuyuPage> {
int _counter = 0;
final Random _random = Random();
AudioPool? pool;
@override
void initState() {
super.initState();
_initAudioPool();
}
void _initAudioPool() async {
pool = await FlameAudio.createPool(
'muyu_1.mp3',
maxPlayers: 1,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
systemOverlayStyle: const SystemUiOverlayStyle(
statusBarIconBrightness: Brightness.dark,
statusBarColor: Colors.transparent),
backgroundColor: Colors.white,
titleTextStyle: const TextStyle(color: Colors.black,fontSize: 16,fontWeight: FontWeight.bold),
iconTheme: const IconThemeData(color: Colors.black),
title: const Text("电子木鱼"),
actions: [
IconButton(onPressed: _toHistory, icon: const Icon(Icons.history))
],
appBar: MuyuAppBar(
onTapHistory: _toHistory,
),
body: Column(
children: [
@@ -61,6 +68,7 @@ class _MuyuPageState extends State<MuyuPage> {
void _onTapSwitchImage() {}
void _onKnock() {
pool?.start();
setState(() {
int addCount = 1 + _random.nextInt(3);
_counter += addCount;