当前位置: 首页 > news >正文

模板网站也需要服务器吗今天新闻联播

模板网站也需要服务器吗,今天新闻联播,WordPress版本更新时间,怎样优化网站appflutter开发实战-实现音效soundpool播放音频 最近开发过程中遇到低配置设备时候,在Media播放音频时候出现音轨限制问题。所以将部分音频采用音效sound来播放。 一、音效类似iOS中的Sound 在iOS中使用sound来播放mp3音频示例如下 // 通过通知的Sound设置为voip_c…

flutter开发实战-实现音效soundpool播放音频

最近开发过程中遇到低配置设备时候,在Media播放音频时候出现音轨限制问题。所以将部分音频采用音效sound来播放。

一、音效类似iOS中的Sound

在iOS中使用sound来播放mp3音频示例如下

// 通过通知的Sound设置为voip_call.caf,这里播放一段空白音频,音频结束后结束震动NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_call.mp3" ofType:nil];AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, soundCompleteCallback, NULL);
[self startShakeSound];
/// 开始播放与震动
- (void)startShakeSound {// 声音AudioServicesPlaySystemSound(soundID);// 震动AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
/// 结束播放与震动
- (void)stopShakeSound {AudioServicesDisposeSystemSoundID(soundID);AudioServicesRemoveSystemSoundCompletion(soundID);
}
//AudioServicesAddSystemSoundCompletion的回调函数
void soundCompleteCallback(SystemSoundID sound,void * clientData) {if (sound == kSystemSoundID_Vibrate) {AudioServicesPlayAlertSound(sound);//重复响铃震动} else {// 移除AudioServicesDisposeSystemSoundID(sound);AudioServicesRemoveSystemSoundCompletion(sound);AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);}
}

在iOS中通过soundID,可以控制播放与暂停,当然iOS中sound播放完成有通知回调callback。soundpool暂时没找到播放完成回调callback。

二、使用soundpool播放音频

2.1、引入soundpool

在pubspec.yaml中引入

soundpool: ^2.3.0

2.2、使用soundpool来播放音频

Soundpool的初始化要用到SoundpoolOptions

SoundpoolOptions soundpoolOptions = SoundpoolOptions();
_soundpool = Soundpool.fromOptions(options: soundpoolOptions);
_soundId = await _soundpool!.loadUri(url);

播放控制需要使用AudioStreamControl,比如播放、暂停、停止、设置音量等控制操作

  • 播放
_audioStreamControl = await _soundpool!.playWithControls(_soundId!);
  • 暂停
await _audioStreamControl!.pause();
  • 停止
await _audioStreamControl!.stop();
  • 设置音量
await _audioStreamControl!.setVolume(volume: volume);

2.3、通过播放时长控制是否可以再次播放

由于soundpool没有播放音频完成回调,这里采用通过Media将音频时长获取到之后判断时间。判断当前播放时间与上次播放的时间间隔是否超过了音频时长Duration。

if (lastPlayMilliseconds == null) {lastPlayMilliseconds = TimeUtil.currentTimeMillis();_audioStreamControl = await _soundpool!.playWithControls(_soundId!);} else {int nowMilliseconds = TimeUtil.currentTimeMillis();bool shouldPlay = true;if (audioDurationInMilliseconds != null) {if (nowMilliseconds - lastPlayMilliseconds! <audioDurationInMilliseconds!) {shouldPlay = false;}}if (shouldPlay) {// 如果上次没有播放完成,不进行播放// 通过判断播放时长_audioStreamControl = await _soundpool!.playWithControls(_soundId!);lastPlayMilliseconds = nowMilliseconds;}}

整体使用soundpool播放音频的相关代码如下

import 'package:soundpool/soundpool.dart';// 使用SoundPool
class SoundPlayer implements BaseAudioPlayer {// AudioStreamControl// SoundpoolSoundpool? _soundpool;String? audioUrl;// 上次播放时长的毫秒数int? lastPlayMilliseconds;// 时长int? audioDurationInMilliseconds;// 优先级late AudioConfig __audioConfig;late bool _playing = false; // 是否正在播放int? _soundId;AudioStreamControl? _audioStreamControl;SoundPlayer(this.__audioConfig, {Duration? duration}) {if (duration != null) {audioDurationInMilliseconds = duration!.inMilliseconds;}print("SoundPlayer audioDurationInMilliseconds:${audioDurationInMilliseconds}");SoundpoolOptions soundpoolOptions = SoundpoolOptions();_soundpool = Soundpool.fromOptions(options: soundpoolOptions);setAudioUrl(__audioConfig.audioUrl);}Future<void> setAudioUrl(String url) async {if (_soundpool != null) {_soundId = await _soundpool!.loadUri(url);}}AudioConfig get_audioConfig() {return __audioConfig;}void play() async {// Usually you don't want to wait for playback to finish.if (__audioConfig.audioUrl != null &&__audioConfig.audioUrl.isNotEmpty &&_soundId != null &&_soundpool != null) {if (lastPlayMilliseconds == null) {lastPlayMilliseconds = TimeUtil.currentTimeMillis();_audioStreamControl = await _soundpool!.playWithControls(_soundId!);} else {int nowMilliseconds = TimeUtil.currentTimeMillis();bool shouldPlay = true;if (audioDurationInMilliseconds != null) {if (nowMilliseconds - lastPlayMilliseconds! <audioDurationInMilliseconds!) {shouldPlay = false;}}if (shouldPlay) {// 如果上次没有播放完成,不进行播放// 通过判断播放时长_audioStreamControl = await _soundpool!.playWithControls(_soundId!);lastPlayMilliseconds = nowMilliseconds;}}}}void pause() async {if (__audioConfig.audioUrl != null &&__audioConfig.audioUrl.isNotEmpty &&_audioStreamControl != null) {try {await _audioStreamControl!.pause();} catch(e) {print("audioStreamControl pause e:${e.toString()}");}}}void stop() async {if (__audioConfig.audioUrl != null &&__audioConfig.audioUrl.isNotEmpty &&_audioStreamControl != null) {try {await _audioStreamControl!.stop();} catch(e) {print("audioStreamControl stop e:${e.toString()}");}}}void setVolume(double volume) async {if (__audioConfig.audioUrl != null &&__audioConfig.audioUrl.isNotEmpty &&_audioStreamControl != null) {try {await _audioStreamControl!.setVolume(volume: volume);} catch(e) {print("audioStreamControl setVolume e:${e.toString()}");}}}// 不需要该播放器,则需要调用该方法void dispose() {if (_soundpool != null) {_soundpool!.dispose();}}
}

三、小结

flutter开发实战-实现音效soundpool播放音频及控制播放、暂停、停止、设置音量等控制操作。

学习记录,每天不停进步。

http://www.ds6.com.cn/news/53036.html

相关文章:

  • 工程监理行业为什么做网站网店怎么运营和推广
  • 男人女人做邪恶的事网站seo 技术优化
  • 做网站带来的好处代做百度收录排名
  • wordpress 1g cpu长沙专业seo优化公司
  • 凡度网络北京网站建设公司app广告联盟
  • 深圳团购网站设计多少钱军事网站大全军事网
  • 网络营销是学什么的seo公司彼亿营销
  • 做网站建设还有钱赚吗b2c有哪些电商平台
  • 网站开启伪静态需要编写什么代码微信营销神器
  • 宁海有做网站的吗网店培训骗局
  • 精准营销的特征seo外推
  • 公司网站建设外包流程今天最新新闻国内大事件
  • 网站招工费怎么做会计分录黄冈seo
  • 铁岭做网站哪家好得物app的网络营销分析论文
  • wordpress自动保存外链mp3苏州seo服务热线
  • 简易微网站模板免费个人网站源码
  • 网站建设云推广赚钱的平台有哪些
  • 网站群建设报价seo简单优化
  • 郑州艾特软件 网站建设十大技能培训机构排名
  • 网站建设审核需要多长时间优化营商环境个人心得体会
  • 长春制作公司网站怎么进行推广
  • 做购物网站步骤百度官方优化指南
  • 网站如何做权重搜索引擎平台
  • 外贸网站建设报价国际新闻报道
  • 免费做初级会计试题网站有哪些海外市场推广方案
  • 国外做汽配的网站朋友圈广告推广文字
  • 浙江建设工程信息网站微信推广软件
  • 外汇局网站做结汇申报重庆森林壁纸
  • 网页设计网站测试宁波seo推荐推广平台
  • 建站都需要什么黄石seo