博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
soundpool
阅读量:5304 次
发布时间:2019-06-14

本文共 2486 字,大约阅读时间需要 8 分钟。

A Flutter Sound Pool for playing short media files.

Sound Pool caches audio tracks in memory. This can be useful in following scenarios:

  • lower latency between play signal and actual playing of the sound (audio does not need to be read from disc/web),
  • the same sound may be used multiple times.

Inspired by .

 

import 'dart:async';import 'package:flutter/material.dart';import 'package:flutter/services.dart';import 'package:soundpool/soundpool.dart';class Sound extends StatefulWidget {  final Widget child;  const Sound({Key key, this.child}) : super(key: key);  @override  SoundState createState() => SoundState();  static SoundState of(BuildContext context) {    final state = context.ancestorStateOfType(const TypeMatcher
()); assert(state != null, 'can not find Sound widget'); return state; }}const _SOUNDS = [ 'clean.mp3', 'drop.mp3', 'explosion.mp3', 'move.mp3', 'rotate.mp3', 'start.mp3'];class SoundState extends State
{ Soundpool _pool; Map
_soundIds; bool mute = false; void _play(String name) { final soundId = _soundIds[name]; if (soundId != null && !mute) { _pool.play(soundId); } } @override void initState() { super.initState(); _pool = Soundpool(streamType: StreamType.music, maxStreams: 4); _soundIds = Map(); for (var value in _SOUNDS) { scheduleMicrotask(() async { final data = await rootBundle.load('assets/audios/$value'); _soundIds[value] = await _pool.load(data); }); } } @override void dispose() { super.dispose(); _pool.dispose(); } @override Widget build(BuildContext context) { return widget.child; } void start() { _play('start.mp3'); } void clear() { _play('clean.mp3'); } void fall() { _play('drop.mp3'); } void rotate() { _play('rotate.mp3'); } void move() { _play('move.mp3'); }}

  

 

 

 

class PlaySound {
final _SOUNDS = [ 'clean.mp3', 'drop.mp3', 'explosion.mp3', 'move.mp3', 'rotate.mp3', 'start.mp3' ]; Map soundIds = Map(); play(String name){
soundpool.play(soundIds[name]); } Soundpool soundpool = Soundpool(streamType: StreamType.music, maxStreams: 4); PlaySound(){
scheduleMicrotask(()async{
for(var value in _SOUNDS){
var data = await rootBundle.load('assets/audios/$value'); soundIds[value] = await soundpool.load(data); } }); } }

转载于:https://www.cnblogs.com/pythonClub/p/10858758.html

你可能感兴趣的文章
BTC功能类
查看>>
jsonRPC
查看>>
layui -page 分页类
查看>>
ETH功能类
查看>>
JosnRpcClient
查看>>
Redis功能类
查看>>
rabbitmq类
查看>>
nginx配置
查看>>
redis消息队列先进先出需要注意什么?
查看>>
商城秒杀实现
查看>>
乐观、悲观锁、redis分布式锁
查看>>
微信第三方登录
查看>>
分布式事务
查看>>
验证sll证书与密钥
查看>>
解决码云出现git@gitee.com: Permission denied (publickey)
查看>>
JS通过内核判断各种浏览器区分360与谷歌
查看>>
递归获取所有JSON对象
查看>>
少量数据文本分类避免过拟合的方法
查看>>
K8S入门系列之二进制部署(二)
查看>>
SQL增加列、修改列、删除列
查看>>