选择音频

This commit is contained in:
toly
2023-05-03 08:32:17 +08:00
parent 3ea5729743
commit 1893c43a9f
3 changed files with 104 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
class AudioOption{
final String name;
final String src;
const AudioOption(this.name, this.src);
}

View File

@@ -4,6 +4,8 @@ import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_first_station/muyu/animate_text.dart';
import 'package:flutter_first_station/muyu/options/select_audio.dart';
import 'models/audio_option.dart';
import 'models/image_option.dart';
import 'muyu_image.dart';
@@ -21,10 +23,18 @@ class MuyuPage extends StatefulWidget {
class _MuyuPageState extends State<MuyuPage> {
int _counter = 0;
int _cruValue = 0;
int _activeImageIndex = 0;
int _activeAudioIndex = 0;
final Random _random = Random();
final List<AudioOption> audioOptions = const [
AudioOption('音效1', 'muyu_1.mp3'),
AudioOption('音效2', 'muyu_2.mp3'),
AudioOption('音效3', 'muyu_3.mp3'),
];
final List<ImageOption> imageOptions = const [
ImageOption('基础版', 'assets/images/muyu.png', 1, 3),
ImageOption('尊享版', 'assets/images/muyu2.png', 3, 6),
@@ -79,7 +89,18 @@ class _MuyuPageState extends State<MuyuPage> {
void _toHistory() {}
void _onTapSwitchAudio() {}
void _onTapSwitchAudio() {
showCupertinoModalPopup(
context: context,
builder: (BuildContext context) {
return AudioOptionPanel(
audioOptions: audioOptions,
activeIndex: _activeAudioIndex,
onSelect: _onSelectAudio,
);
},
);
}
void _onTapSwitchImage() {
showCupertinoModalPopup(
@@ -107,15 +128,26 @@ class _MuyuPageState extends State<MuyuPage> {
int get knockValue {
int min = imageOptions[_activeImageIndex].min;
int max = imageOptions[_activeImageIndex].max;
return min + _random.nextInt(max+1 - min);
return min + _random.nextInt(max + 1 - min);
}
void _onSelectImage(int value) {
Navigator.of(context).pop();
if(value == _activeImageIndex) return;
if (value == _activeImageIndex) return;
setState(() {
_activeImageIndex = value;
});
}
String get activeAudio => audioOptions[_activeAudioIndex].src;
void _onSelectAudio(int value) async{
Navigator.of(context).pop();
if (value == _activeAudioIndex) return;
_activeAudioIndex = value;
pool = await FlameAudio.createPool(
activeAudio,
maxPlayers: 1,
);
}
}

View File

@@ -0,0 +1,62 @@
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/material.dart';
import '../models/audio_option.dart';
class AudioOptionPanel extends StatelessWidget {
final List<AudioOption> audioOptions;
final ValueChanged<int> onSelect;
final int activeIndex;
const AudioOptionPanel({
Key? key,
required this.audioOptions,
required this.activeIndex,
required this.onSelect,
}) : super(key: key);
@override
Widget build(BuildContext context) {
const TextStyle labelStyle =
TextStyle(fontSize: 16, fontWeight: FontWeight.bold);
return Material(
child: SizedBox(
height: 300,
child: Column(
children: [
Container(
height: 46,
alignment: Alignment.center,
child: const Text(
"选择音效",
style: labelStyle,
)),
...List.generate(audioOptions.length, _buildByIndex)
],
),
),
);
}
Widget _buildByIndex(int index) {
bool active = index == activeIndex;
return ListTile(
selected: active,
onTap: () => onSelect(index),
title: Text(audioOptions[index].name),
trailing: IconButton(
splashRadius: 20,
onPressed: ()=>_tempPlay(audioOptions[index].src),
icon: const Icon(
Icons.record_voice_over_rounded,
color: Colors.blue,
),
),
);
}
void _tempPlay(String src) async{
AudioPool pool = await FlameAudio.createPool(src, maxPlayers: 1);
pool.start();
}
}