Flutter音频均衡器插件equalizer_flutter_custom的使用
Flutter音频均衡器插件equalizer_flutter_custom的使用
Flutter_Equalizer_Custom 是一个 Flutter 包,允许你创建自定义音乐播放器均衡器。你可以使用这个包来创建一个漂亮的均衡器,而无需编写大量的代码。
文档
安装
添加到pubspeck.yaml
dependencies:
equalizer_flutter_custom: ^0.0.2
or
flutter pub add equalizer_flutter_custom
快速概述
CustomEqualizer(
isEqEnabled: true, // 启用均衡器
playerSessionId: androidAudioSessionId!, // 重要!音频会话ID
)
设置:编辑AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
</manifest>
添加此依赖项到pubspec.yml
dependencies:
provider: any
将你的MaterialApp包裹在ChangeNotifierProvider中
[@override](/user/override)
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => EqualizerProvider(), // 从当前包导入
child: MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
),
);
}
示例与完整代码
import 'package:flutter/material.dart';
import 'package:flutter_audio_query/flutter_audio_query.dart';
import 'package:just_audio/just_audio.dart';
import 'package:equalizer_flutter_custom/src/CustomEqualizer.dart';
import 'package:provider/provider.dart';
import 'package:equalizer_flutter_custom/src/EqalizerProvider/EqualizerProvider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// 这个小部件是你的应用的根。
[@override](/user/override)
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => EqualizerProvider(),
child: MaterialApp(
title: 'Demo Equalizer',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late List<SongInfo> songs; // 使用flutter_audio_query
late AudioPlayer audioPlayer; // 使用just_audio包。你可以使用自己的。
bool isLoading = true;
getSOngs() async {
FlutterAudioQuery audioQuery = FlutterAudioQuery();
List<SongInfo> songsd = await audioQuery.getSongs();
setState(() {
songs = songsd;
isLoading = false;
});
}
[@override](/user/override)
void initState() {
// TODO: 实现initState
audioPlayer = AudioPlayer();
getSOngs();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("均衡器"),
),
body: isLoading
? const CircularProgressIndicator()
: ListView.builder(
itemCount: songs.length,
itemBuilder: (context, index) {
return InkWell(
onTap: () {
audioPlayer.setUrl(songs[index].filePath);
audioPlayer.play();
Future.delayed(const Duration(milliseconds: 300)) // 确保音频正在播放后再访问均衡器(首次)。
.then((value) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
//equalizer_flutter_custom
CustomEqualizer(
isEqEnabled: true, // 启用均衡器
playerSessionId: audioPlayer.androidAudioSessionId!, // 最重要的音频会话ID,非空
bandTextColor: Colors.green,
sliderBoxHeight: 220,
sliderBoxPadding: 10,
sliderBoxBorderRadius: BorderRadius.circular(20),
),
));
});
},
child: ListTile(
title: Text(songs[index].displayName),
),
);
},
),
);
}
}
更多关于Flutter音频均衡器插件equalizer_flutter_custom的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter音频均衡器插件equalizer_flutter_custom的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用equalizer_flutter_custom
插件来实现音频均衡器的示例代码。这个插件允许你控制音频播放的均衡器设置。
首先,你需要在你的pubspec.yaml
文件中添加依赖项:
dependencies:
flutter:
sdk: flutter
equalizer_flutter_custom: ^最新版本号 # 请替换为实际最新版本号
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤实现音频均衡器功能:
- 导入必要的包:
import 'package:flutter/material.dart';
import 'package:equalizer_flutter_custom/equalizer_flutter_custom.dart';
- 创建UI界面:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Audio Equalizer Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: EqualizerScreen(),
);
}
}
class EqualizerScreen extends StatefulWidget {
@override
_EqualizerScreenState createState() => _EqualizerScreenState();
}
class _EqualizerScreenState extends State<EqualizerScreen> {
EqualizerController? _equalizerController;
@override
void initState() {
super.initState();
// 初始化EqualizerController
_equalizerController = EqualizerController();
// 监听均衡器变化(可选)
_equalizerController!.bands.addListener(() {
print('Bands changed: ${_equalizerController!.bands.value}');
});
}
@override
void dispose() {
// 释放资源
_equalizerController?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Audio Equalizer Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// 显示当前均衡器设置
Text('Current Bands Values:'),
..._equalizerController!.bands.value.map((band) {
return Text('Band ${band.bandIndex}: ${band.value.toInt()} dB');
}).toList(),
// 均衡器控件
SizedBox(height: 20),
Equalizer(
controller: _equalizerController,
bandCount: 5, // 均衡器条带数量
height: 200,
onChange: (List<Band> bands) {
// 均衡器变化时的回调(可选)
print('Bands changed to: $bands');
},
),
],
),
),
);
}
}
- 确保权限(如果需要访问设备的音频设置,你可能需要在Android和iOS上添加相应的权限声明,具体取决于插件的要求)。
这个示例展示了如何使用equalizer_flutter_custom
插件来创建一个简单的音频均衡器界面。用户可以通过滑动条带来调整不同的音频频段。
请注意,equalizer_flutter_custom
插件的具体API和实现可能会有所不同,因此请务必查阅最新的官方文档和示例代码以获取最准确的信息。如果插件有更新或者API有所变动,请根据最新的文档进行调整。