zoukankan      html  css  js  c++  java
  • soundpool

    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 Android SoundPool API.

    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<SoundState>());
        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<Sound> {
      Soundpool _pool;
    
      Map<String, int> _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);
    }
    });
    }
    }
  • 相关阅读:
    Apple的App Analytics统计平台你必须知道的Q&A整理与翻译
    WWDC2014总结---For产品经理们
    AppStore占坑注意事项
    Mac上的终端(Terminal)启动缓慢
    iOS推送失败的可能问题汇总
    Mac OS X 10.9 Mavericks安装后,Xcode调试时模拟器黑屏的处理方法
    MySql批处理的小窍门:排行榜类数据生成
    升级OSX 10.9 Mavericks后,会导致Finder始终无响应的一个问题
    拉面馆中的移动互联网——无线KPI探讨
    Weak is not weak,Strong is not strong
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10858758.html
Copyright © 2011-2022 走看看