Flutter:如何配置 audio_session 躲避别人
Flutter: How to configure audio_session to duck others
我需要在不中断现有播放列表的情况下播放音频。我的目标是通过 phone 上正在进行的当前音频服务播放音频,理想情况下我想隐藏音频并通过它播放我的流。与驾驶时的 GPS 服务非常相似(Waze,Google 地图)。
步骤:
- 安装依赖项
- 创建 initAudioService() 函数
- 创建一个 AudioSession 实例
- 按照示例配置您的 AudioSession 实例
- 按照示例初始化您的 AudioService
- 在 runApp 之前从 main 调用 initAudioService()
依赖关系:
- audio_service: ^0.18.0;
- audio_session: ^0.1.6+1
示例:
import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
import 'audio_player_service.dart';
Future initAudioService() async {
//audio_session INSTANCE
final session = await AudioSession.instance;
//audio_session DUCK OTHERS CONFIGURATION
await session.configure(const AudioSessionConfiguration(
avAudioSessionCategory: AVAudioSessionCategory.playback,
avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.duckOthers,
avAudioSessionMode: AVAudioSessionMode.defaultMode,
avAudioSessionRouteSharingPolicy:
AVAudioSessionRouteSharingPolicy.defaultPolicy,
avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
androidAudioAttributes: AndroidAudioAttributes(
contentType: AndroidAudioContentType.music,
flags: AndroidAudioFlags.none,
usage: AndroidAudioUsage.media,
),
androidAudioFocusGainType: AndroidAudioFocusGainType.gainTransientMayDuck,
androidWillPauseWhenDucked: true,
));
//INITIALIZE audio_service
return await AudioService.init(
builder: () => AudioPlayerService(),
config: const AudioServiceConfig(
androidNotificationChannelId: 'com.YOUR_COMPANY.YOUR_APP_NAME.audio',
androidNotificationChannelName: 'Audio Service Demo',
androidNotificationOngoing: true,
androidStopForegroundOnPause: true,
),
);
}
//MAIN
void main() async {
await initAudioService();
runApp(const MaterialApp(home: MyApp()));
}
我需要在不中断现有播放列表的情况下播放音频。我的目标是通过 phone 上正在进行的当前音频服务播放音频,理想情况下我想隐藏音频并通过它播放我的流。与驾驶时的 GPS 服务非常相似(Waze,Google 地图)。
步骤:
- 安装依赖项
- 创建 initAudioService() 函数
- 创建一个 AudioSession 实例
- 按照示例配置您的 AudioSession 实例
- 按照示例初始化您的 AudioService
- 在 runApp 之前从 main 调用 initAudioService()
依赖关系:
- audio_service: ^0.18.0;
- audio_session: ^0.1.6+1
示例:
import 'package:audio_service/audio_service.dart'; import 'package:audio_session/audio_session.dart'; import 'audio_player_service.dart'; Future initAudioService() async { //audio_session INSTANCE final session = await AudioSession.instance; //audio_session DUCK OTHERS CONFIGURATION await session.configure(const AudioSessionConfiguration( avAudioSessionCategory: AVAudioSessionCategory.playback, avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.duckOthers, avAudioSessionMode: AVAudioSessionMode.defaultMode, avAudioSessionRouteSharingPolicy: AVAudioSessionRouteSharingPolicy.defaultPolicy, avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none, androidAudioAttributes: AndroidAudioAttributes( contentType: AndroidAudioContentType.music, flags: AndroidAudioFlags.none, usage: AndroidAudioUsage.media, ), androidAudioFocusGainType: AndroidAudioFocusGainType.gainTransientMayDuck, androidWillPauseWhenDucked: true, )); //INITIALIZE audio_service return await AudioService.init( builder: () => AudioPlayerService(), config: const AudioServiceConfig( androidNotificationChannelId: 'com.YOUR_COMPANY.YOUR_APP_NAME.audio', androidNotificationChannelName: 'Audio Service Demo', androidNotificationOngoing: true, androidStopForegroundOnPause: true, ), ); } //MAIN void main() async { await initAudioService(); runApp(const MaterialApp(home: MyApp())); }