Skip to content

Commit aaccd50

Browse files
committed
[ogg_opus_player] add audio session to readme
1 parent b185809 commit aaccd50

File tree

7 files changed

+70
-14
lines changed

7 files changed

+70
-14
lines changed

packages/ogg_opus_player/README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
a ogg opus file player for flutter.
66

7-
| platform | | required os version |
8-
|----------|-------|---------------------|
9-
| iOS | | 10.0 |
10-
| macOS | | 10.12 |
11-
| Windows | | |
12-
| Linux | | |
13-
| Android | | minSdk 21 |
7+
| platform | | required os version |
8+
|----------|---|---------------------|
9+
| iOS || 10.0 |
10+
| macOS || 10.12 |
11+
| Windows || |
12+
| Linux || |
13+
| Android || minSdk 21 |
1414

1515
## Getting Started
1616

@@ -35,6 +35,12 @@ a ogg opus file player for flutter.
3535
}
3636
```
3737

38+
## AudioSession
39+
40+
For android/iOS platform, you need to manage audio session by yourself.
41+
42+
It is recommended to use [audio_session](https://pub.dev/packages/audio_session) to manage audio session.
43+
3844
## Linux required
3945

4046
Need SDL2 library installed on Linux.
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
PODS:
2+
- audio_session (0.0.1):
3+
- Flutter
24
- Flutter (1.0.0)
35
- ogg_opus_player (0.0.1):
46
- Flutter
57
- path_provider_ios (0.0.1):
68
- Flutter
79

810
DEPENDENCIES:
11+
- audio_session (from `.symlinks/plugins/audio_session/ios`)
912
- Flutter (from `Flutter`)
1013
- ogg_opus_player (from `.symlinks/plugins/ogg_opus_player/ios`)
1114
- path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`)
1215

1316
EXTERNAL SOURCES:
17+
audio_session:
18+
:path: ".symlinks/plugins/audio_session/ios"
1419
Flutter:
1520
:path: Flutter
1621
ogg_opus_player:
@@ -19,10 +24,11 @@ EXTERNAL SOURCES:
1924
:path: ".symlinks/plugins/path_provider_ios/ios"
2025

2126
SPEC CHECKSUMS:
27+
audio_session: 4f3e461722055d21515cf3261b64c973c062f345
2228
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
2329
ogg_opus_player: 01dc6eaa63d1e061f0065f41845a0a9ea76b9396
2430
path_provider_ios: 14f3d2fd28c4fdb42f44e0f751d12861c43cee02
2531

2632
PODFILE CHECKSUM: 4e8f8b2be68aeea4c0d5beb6ff1e79fface1d048
2733

28-
COCOAPODS: 1.11.3
34+
COCOAPODS: 1.12.0

packages/ogg_opus_player/example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@
357357
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
358358
CLANG_ENABLE_MODULES = YES;
359359
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
360-
DEVELOPMENT_TEAM = MY267Y3M37;
360+
DEVELOPMENT_TEAM = "";
361361
ENABLE_BITCODE = NO;
362362
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
363363
INFOPLIST_FILE = Runner/Info.plist;
@@ -487,7 +487,7 @@
487487
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
488488
CLANG_ENABLE_MODULES = YES;
489489
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
490-
DEVELOPMENT_TEAM = MY267Y3M37;
490+
DEVELOPMENT_TEAM = "";
491491
ENABLE_BITCODE = NO;
492492
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
493493
INFOPLIST_FILE = Runner/Info.plist;
@@ -511,7 +511,7 @@
511511
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
512512
CLANG_ENABLE_MODULES = YES;
513513
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
514-
DEVELOPMENT_TEAM = MY267Y3M37;
514+
DEVELOPMENT_TEAM = "";
515515
ENABLE_BITCODE = NO;
516516
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
517517
INFOPLIST_FILE = Runner/Info.plist;

packages/ogg_opus_player/example/lib/main.dart

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import 'dart:async';
22
import 'dart:io';
33

4+
import 'package:audio_session/audio_session.dart';
45
import 'package:flutter/material.dart';
56
import 'package:flutter/services.dart';
67
import 'package:ogg_opus_player/ogg_opus_player.dart';
78
import 'package:path/path.dart' as p;
89
import 'package:path_provider/path_provider.dart';
910

11+
late AudioSession session;
12+
1013
Future<void> main() async {
1114
WidgetsFlutterBinding.ensureInitialized();
1215
final tempDir = await getTemporaryDirectory();
1316
final workDir = p.join(tempDir.path, 'ogg_opus_player');
1417
debugPrint('workDir: $workDir');
18+
session = await AudioSession.instance;
1519
runApp(
1620
MaterialApp(
1721
home: Scaffold(
@@ -140,12 +144,15 @@ class _OpusOggPlayerWidgetState extends State<_OpusOggPlayerWidget> {
140144
)
141145
else
142146
IconButton(
143-
onPressed: () {
147+
onPressed: () async {
144148
_player?.dispose();
145149
_speedIndex = 1;
146150
_player = OggOpusPlayer(widget.path);
151+
session.configure(const AudioSessionConfiguration.music());
152+
bool active = await session.setActive(true);
153+
debugPrint('active: $active');
147154
_player?.play();
148-
_player?.state.addListener(() {
155+
_player?.state.addListener(() async {
149156
setState(() {});
150157
if (_player?.state.value == PlayerState.ended) {
151158
_player?.dispose();
@@ -158,8 +165,14 @@ class _OpusOggPlayerWidgetState extends State<_OpusOggPlayerWidget> {
158165
IconButton(
159166
onPressed: () {
160167
setState(() {
168+
debugPrint('ended');
161169
_player?.dispose();
162170
_player = null;
171+
session.setActive(false).then((value) {
172+
debugPrint('active: $value');
173+
}).onError((error, stackTrace) {
174+
debugPrint('error: $error');
175+
});
163176
});
164177
},
165178
icon: const Icon(Icons.stop),
@@ -212,12 +225,19 @@ class _RecorderExampleState extends State<_RecorderExample> {
212225
const SizedBox(height: 8),
213226
if (_recorder == null)
214227
IconButton(
215-
onPressed: () {
228+
onPressed: () async {
216229
final file = File(_recordedPath);
217230
if (file.existsSync()) {
218231
File(_recordedPath).deleteSync();
219232
}
220233
File(_recordedPath).createSync(recursive: true);
234+
await session.configure(const AudioSessionConfiguration(
235+
avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
236+
avAudioSessionCategoryOptions:
237+
AVAudioSessionCategoryOptions.allowBluetooth,
238+
avAudioSessionMode: AVAudioSessionMode.spokenAudio,
239+
));
240+
await session.setActive(true);
221241
final recorder = OggOpusRecorder(_recordedPath);
222242
recorder.start();
223243
setState(() {
@@ -236,6 +256,11 @@ class _RecorderExampleState extends State<_RecorderExample> {
236256
_recorder?.dispose();
237257
setState(() {
238258
_recorder = null;
259+
session.setActive(
260+
false,
261+
avAudioSessionSetActiveOptions:
262+
AVAudioSessionSetActiveOptions.notifyOthersOnDeactivation,
263+
);
239264
});
240265
},
241266
icon: const Icon(Icons.stop),

packages/ogg_opus_player/example/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import FlutterMacOS
66
import Foundation
77

8+
import audio_session
89
import ogg_opus_player
910
import path_provider_macos
1011

1112
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
13+
AudioSessionPlugin.register(with: registry.registrar(forPlugin: "AudioSessionPlugin"))
1214
OggOpusPlayerPlugin.register(with: registry.registrar(forPlugin: "OggOpusPlayerPlugin"))
1315
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
1416
}

packages/ogg_opus_player/example/pubspec.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ packages:
99
url: "https://pub.dev"
1010
source: hosted
1111
version: "2.10.0"
12+
audio_session:
13+
dependency: "direct main"
14+
description:
15+
name: audio_session
16+
sha256: e4acc4e9eaa32436dfc5d7aed7f0a370f2d7bb27ee27de30d6c4f220c2a05c73
17+
url: "https://pub.dev"
18+
source: hosted
19+
version: "0.1.13"
1220
boolean_selector:
1321
dependency: transitive
1422
description:
@@ -231,6 +239,14 @@ packages:
231239
url: "https://pub.dev"
232240
source: hosted
233241
version: "4.2.4"
242+
rxdart:
243+
dependency: transitive
244+
description:
245+
name: rxdart
246+
sha256: "0c7c0cedd93788d996e33041ffecda924cc54389199cde4e6a34b440f50044cb"
247+
url: "https://pub.dev"
248+
source: hosted
249+
version: "0.27.7"
234250
sky_engine:
235251
dependency: transitive
236252
description: flutter

packages/ogg_opus_player/example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies:
4343
cupertino_icons: ^1.0.2
4444
path_provider: ^2.0.9
4545
platform: ^3.1.0
46+
audio_session: ^0.1.13
4647

4748
dev_dependencies:
4849
flutter_test:

0 commit comments

Comments
 (0)