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

郑州市疫情防控指挥部最新公告长沙seo网络公司

郑州市疫情防控指挥部最新公告,长沙seo网络公司,asp.net网站开发实战,网站添加faviconflutter开发实战-just_audio实现播放音频暂停音频设置音量等 最近开发过程中遇到需要播放背景音等音频播放,这里使用just_audio来实现播放音频暂停音频设置音量等 一、引入just_audio 在pubspec.yaml引入just_audio just_audio: ^2.7.0在iOS上,video_p…

flutter开发实战-just_audio实现播放音频暂停音频设置音量等

最近开发过程中遇到需要播放背景音等音频播放,这里使用just_audio来实现播放音频暂停音频设置音量等

在这里插入图片描述

一、引入just_audio

在pubspec.yaml引入just_audio

  just_audio: ^2.7.0

在iOS上,video_player使用的是AVPlayer进行播放。
在Android上,video_player使用的是ExoPlayer。

二、使用前设置

2.1 在iOS中的设置
在iOS工程中info.plist添加一下设置,以便支持Https,HTTP的视频地址

<key>NSAppTransportSecurity</key>
<dict><key>NSAllowsArbitraryLoads</key><true/>
</dict>

2.2 在Android中的设置
需要在/android/app/src/main/AndroidManifest.xml文件中添加网络权限

<uses-permission android:name="android.permission.INTERNET"/>

三、just_audio实现播放音频暂停音频设置音量等

引入just_audio后,可以使用AudioPlayer来播放音频

3.1 常用操作如下

  • 播放
await _audioPlayer.play();
  • 暂停
await _audioPlayer.pause();
  • seek
await _audioPlayer.seek(position);
  • 停止
await _audioPlayer.pause();
await _audioPlayer.seek(Duration.zero);
  • 设置音量
await _audioPlayer.setVolume(volume);

3.2 使用just_audio实现播放player

这里通过player来获取SDAudioPlayerState状态,控制音频的暂停音频设置音量等操作

import 'package:flutter_app/manager/logger_manager.dart';
import 'package:just_audio/just_audio.dart';// 播放回调
typedef SDAudioPlayerCallBack = void Function(SDAudioPlayerState state, SDAudioPlayerError? error);// 播放音频的状态
enum SDAudioPlayerState {idle, // 默认loading, // 加载中buffering, // 缓存中ready, // 可播放completed, // 播放完成
}// 播放音频出现错误
class SDAudioPlayerError {String? message;
}// 定义优先级的类
class SDMusicConfig {// 音频文件地址late String audioUrl = '';late bool runLoop = false; // 是否循环播放// 构造函数SDMusicConfig(this.audioUrl, this.audioPriority, this.runLoop);
}// 播放音频文件
class SDMusicPlayer {// 音频播放late AudioPlayer _audioPlayer;// 优先级late SDMusicConfig _musicConfig;late bool _playing = false; // 是否正在播放late SDAudioPlayerState _playerState = SDAudioPlayerState.idle; // 状态late SDAudioPlayerCallBack? _playerCallBack;SDMusicPlayer(this._audioPlayer, this._musicConfig){setAudioUrl(this._musicConfig.audioUrl);openPlayCallBack((state, error) {});}SDMusicConfig getMusicConfig() {return _musicConfig;}void openPlayCallBack(SDAudioPlayerCallBack playerCallBack) {_playerCallBack = playerCallBack;_audioPlayer.playerStateStream.listen((state) {_playing = state.playing;switch(state.processingState) {case ProcessingState.idle: {_playerState = SDAudioPlayerState.idle;break;}case ProcessingState.loading: {_playerState = SDAudioPlayerState.loading;break;}case ProcessingState.buffering: {_playerState = SDAudioPlayerState.buffering;break;}case ProcessingState.ready: {_playerState = SDAudioPlayerState.ready;break;}case ProcessingState.completed: {_playerState = SDAudioPlayerState.completed;if (_musicConfig.runLoop == true) {// 循环播放的时候seek(Duration.zero);play();} else {stop();}break;}default:}if (_playerCallBack != null) {_playerCallBack!(_playerState, null);}});}// var duration = await player.setUrl('https://foo.com/bar.mp3');Future<void> setAudioUrl(String url) async {SDAudioPlayerError? error;if (url.isNotEmpty) {// Set the audio source but manually load audio at a later point.try {_audioPlayer.setUrl(url, preload: true);// Acquire platform decoders and start loading audio.var duration = await _audioPlayer.load();print("url:${url} duration:${duration}");} on PlayerException catch (e) {// iOS/macOS: maps to NSError.code// Android: maps to ExoPlayerException.type// Web: maps to MediaError.code// Linux/Windows: maps to PlayerErrorCode.indexprint("SDAudioPlayer Error code: ${e.code}");// iOS/macOS: maps to NSError.localizedDescription// Android: maps to ExoPlaybackException.getMessage()// Web/Linux: a generic message// Windows: MediaPlayerError.messageprint("SDAudioPlayer Error message: ${e.message}");error = SDAudioPlayerError();error.message = e.message;} on PlayerInterruptedException catch (e) {// This call was interrupted since another audio source was loaded or the// player was stopped or disposed before this audio source could complete// loading.LoggerManager().debug("SDAudioPlayer Connection aborted: ${e.message}");error = SDAudioPlayerError();error.message = e.message;} catch (e) {// Fallback for all errorsprint("e: ${e}");error = SDAudioPlayerError();error.message = e.toString();}} else {error = SDAudioPlayerError();error.message = '播放地址不能为空';}if (_playerCallBack != null) {_playerCallBack!(_playerState, error);}}void play() async {// Usually you don't want to wait for playback to finish.if (_musicConfig.audioUrl != null && _musicConfig.audioUrl.isNotEmpty) {if (_playing == false) {// 正在播放await _audioPlayer.play();}}}void pause() async {if (_musicConfig.audioUrl != null && _musicConfig.audioUrl.isNotEmpty) {await _audioPlayer.pause();}}void stop() async {if (_musicConfig.audioUrl != null && _musicConfig.audioUrl.isNotEmpty) {await _audioPlayer.pause();await _audioPlayer.seek(Duration.zero);}}void seek(Duration position) async {if (_musicConfig.audioUrl != null && _musicConfig.audioUrl.isNotEmpty) {await _audioPlayer.seek(position);}}void setVolume(double volume) async {if (_musicConfig.audioUrl != null && _musicConfig.audioUrl.isNotEmpty) {await _audioPlayer.setVolume(volume);}}// 不需要该播放器,则需要调用该方法void dispose() async {await _audioPlayer.dispose();}
}

四、小结

flutter开发实战-just_audio实现播放音频暂停音频设置音量等。
学习记录,每天不停进步。

http://www.hkea.cn/news/309170/

相关文章:

  • 武汉外贸网站建设公司外链相册
  • java做网站的权限管理seo描述是什么
  • 招聘网最新招聘信息网武汉seo计费管理
  • 直播软件开发源码重庆seo顾问
  • 公司网站如何做宣传百度视频推广怎么收费
  • 淄博市 网站建设报价郑州seo外包阿亮
  • 网络服务商是指什么网站优化排名工具
  • 网站优化的分析比较好的品牌策划公司有哪些
  • 国外比较好的资源网站电商运营推广是做什么的
  • 佛山房地产网站建设seo实战培训王乃用
  • 如何做可以赚钱的网站关键词如何快速排名
  • 深圳品牌做网站公司有哪些百度app推广
  • 重庆建设行业信息网站搜狗登录入口
  • 同仁行业网站建设报价北京做的好的seo公司
  • 陕西自助建站做网站郑州外语网站建站优化
  • 小型企业网站系统cilimao磁力猫最新版地址
  • 铁岭网站建设移动网站广东网站seo
  • 网站模板插件sem和seo
  • 用wordpress制作网站模板沈阳seo
  • 优化一个网站多少钱宜昌网站seo
  • 刚做的网站怎么才能搜索到枸橼酸西地那非片功效效及作用
  • 罗湖区网站公司专业模板建站
  • 哪有备案好的网站国产系统2345
  • 网站开发怎么让别人看到最新营销模式有哪些
  • ssm网站开发源码百度推广多少钱一个月
  • 手游门户网站建设appstore关键词优化
  • 齐河网站开发seo服务内容
  • 北京微信网站建设费用想卖产品怎么推广宣传
  • 网站上线的步骤厦门网站推广公司哪家好
  • 网站做app的软件有哪些百度一下你就知道下载