Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/audiofileplayer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
## 1.0.1 - 21 Nov 2019

* Fix new build issues in podfile, add pubspec.yaml dependency versions

## 1.0.1 - 21 Nov 2019

* Fix background audio on iOS, add ability to specify iOS audio category (which defaults to 'playback').
1 change: 1 addition & 0 deletions packages/audiofileplayer/example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
283EA6CAD6FBBC952F6CE58F /* libPods-Runner.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Runner.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
48D486519172122DC77807F5 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand All @@ -55,6 +56,8 @@
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C1B130F899C72F55F2976942 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
EC8F38AE54ABD5EB70CDAE54 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -74,6 +77,9 @@
73E993770646A7EF8D98E434 /* Pods */ = {
isa = PBXGroup;
children = (
C1B130F899C72F55F2976942 /* Pods-Runner.debug.xcconfig */,
EC8F38AE54ABD5EB70CDAE54 /* Pods-Runner.release.xcconfig */,
48D486519172122DC77807F5 /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
Expand Down Expand Up @@ -178,6 +184,11 @@
TargetAttributes = {
97C146ED1CF9000F007C117D = {
CreatedOnToolsVersion = 7.3.1;
SystemCapabilities = {
com.apple.BackgroundModes = {
enabled = 1;
};
};
};
};
};
Expand All @@ -186,6 +197,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -267,16 +279,13 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
Expand Down
4 changes: 4 additions & 0 deletions packages/audiofileplayer/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
32 changes: 30 additions & 2 deletions packages/audiofileplayer/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class _MyAppState extends State<MyApp> {
bool _remoteAudioLoading = false;
String _remoteErrorMessage;

// The iOS audio category dropdown item in the fourth card.
IosAudioCategory _iosAudioCategory = IosAudioCategory.playback;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -57,7 +60,7 @@ class _MyAppState extends State<MyApp> {
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: RaisedButton(
onPressed: onTap,
child: isPlaying
Expand Down Expand Up @@ -198,9 +201,34 @@ class _MyAppState extends State<MyApp> {
}),
_remoteErrorMessage != null
? Text(_remoteErrorMessage,
style: TextStyle(color: const Color(0xFFFF0000)))
style: const TextStyle(color: const Color(0xFFFF0000)))
: Text(_remoteAudioLoading ? 'loading...' : 'loaded')
]),
_cardWrapper(<Widget>[
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
const Text('Enable background playback:'),
Checkbox(
value: Audio.shouldPlayWhileAppPaused,
onChanged: (bool isOn) =>
setState(() => Audio.shouldPlayWhileAppPaused = isOn))
]),
const Text('(iOS only) iOS audio category:'),
DropdownButton<IosAudioCategory>(
value: _iosAudioCategory,
onChanged: (IosAudioCategory newValue) {
setState(() {
_iosAudioCategory = newValue;
Audio.setIosAudioCategory(_iosAudioCategory);
});
},
items: IosAudioCategory.values.map((IosAudioCategory category) {
return DropdownMenuItem<IosAudioCategory>(
value: category,
child: Text(category.toString()),
);
}).toList(),
)
]),
]),
));
}
Expand Down
29 changes: 28 additions & 1 deletion packages/audiofileplayer/ios/Classes/AudiofileplayerPlugin.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import <AVFoundation/AVFoundation.h>
#import "AudiofileplayerPlugin.h"
#import "ManagedPlayer.h"

Expand All @@ -23,6 +24,12 @@
static NSString *const kPositionSeconds = @"position_seconds";
static NSString *const kErrorCode = @"AudioPluginError";

static NSString *const kAudioCategoryMethod = @"iosAudioCategory";
static NSString *const kAudioCategory = @"iosAudioCategory";
static NSString *const kAudioCategoryAmbientSolo = @"iosAudioCategoryAmbientSolo";
static NSString *const kAudioCategoryAmbientMixed = @"iosAudioCategoryAmbientMixed";
static NSString *const kAudioCategoryPlayback = @"iosAudioCategoryPlayback";

@interface AudiofileplayerPlugin () <FLTManagedPlayerDelegate>
@end

Expand All @@ -48,13 +55,33 @@ - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar
_registrar = registrar;
_channel = channel;
_playersDict = [NSMutableDictionary dictionary];
// Set audio category to initial default of 'playback'.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
error:nil];
}
return self;
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
NSLog(@"handleMethodCall: method = %@", call.method);
if ([call.method isEqualToString:@"load"]) {

// Setting the audio category.
if ([call.method isEqualToString:kAudioCategoryMethod]) {
NSString *categoryString = call.arguments[kAudioCategory];
AVAudioSessionCategory category;
if ([categoryString isEqualToString:kAudioCategoryAmbientSolo]) {
category = AVAudioSessionCategorySoloAmbient;
} else if ([categoryString isEqualToString:kAudioCategoryAmbientMixed]) {
category = AVAudioSessionCategoryAmbient;
} if ([categoryString isEqualToString:kAudioCategoryPlayback]) {
category = AVAudioSessionCategoryPlayback;
}
[[AVAudioSession sharedInstance] setCategory:category error:nil];
return;
}

// Loading an audio instance.
if ([call.method isEqualToString:kLoadMethod]) {
[self handleLoadWithCall:call result:result];
return;
}
Expand Down
55 changes: 54 additions & 1 deletion packages/audiofileplayer/lib/audiofileplayer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io' show Platform;
import 'dart:typed_data';
import 'dart:ui' show AppLifecycleState;

Expand Down Expand Up @@ -33,6 +34,39 @@ const String onPositionCallback = 'onPosition';
const String positionSecondsKey = 'position_seconds';
const String errorCode = 'AudioPluginError';

const String iosAudioCategoryMethod = 'iosAudioCategory';
const String iosAudioCategoryKey = 'iosAudioCategory';
const String iosAudioCategoryAmbientSolo = 'iosAudioCategoryAmbientSolo';
const String iosAudioCategoryAmbientMixed = 'iosAudioCategoryAmbientMixed';
const String iosAudioCategoryPlayback = 'iosAudioCategoryPlayback';

/// Represents audio playback category on iOS.
///
/// An 'ambient' category should be used for tasks like game audio, whereas
/// the [playback] category should be used for tasks like music player playback.
///
/// Note that for background audio, the [shouldPlayWhileAppPaused] flag must
/// also be set.
///
/// See
/// https://developer.apple.com/documentation/avfoundation/avaudiosessioncategory
/// for more information.
enum IosAudioCategory {
/// Audio is silenced by screen lock and the silent switch; audio will not mix
/// with other apps' audio.
ambientSolo,

/// Audio is silenced by screen lock and the silent switch; audio will mix
/// with other apps' (mixable) audio.
ambientMixed,

/// Audio is not silenced by screen lock or silent switch; audio will not mix
/// with other apps' audio.
///
/// The default value.
playback
}

/// A plugin for audio playback.
///
/// Example usage:
Expand Down Expand Up @@ -170,7 +204,7 @@ class Audio with WidgetsBindingObserver {
}

@visibleForTesting
static final MethodChannel channel = MethodChannel(channelName)
static final MethodChannel channel = const MethodChannel(channelName)
..setMethodCallHandler(handleMethodCall);

static final Uuid _uuid = Uuid();
Expand Down Expand Up @@ -503,6 +537,25 @@ class Audio with WidgetsBindingObserver {
}
}

/// Sets the iOS audio category.
///
/// Only communicates with the underlying plugin on iOS; no-op otherwise.
static Future<void> setIosAudioCategory(IosAudioCategory category) async {
const Map<IosAudioCategory, String> categoryToString =
<IosAudioCategory, String>{
IosAudioCategory.ambientSolo: iosAudioCategoryAmbientSolo,
IosAudioCategory.ambientMixed: iosAudioCategoryAmbientMixed,
IosAudioCategory.playback: iosAudioCategoryPlayback
};
if (!Platform.isIOS) return;
try {
await channel.invokeMethod<dynamic>(iosAudioCategoryMethod,
<String, dynamic>{iosAudioCategoryKey: categoryToString[category]});
} on PlatformException catch (e) {
_logger.severe('setIosAudioCategory error, category: $category', e);
}
}

/// Sends method call for starting playback.
Future<void> _playNative(bool playFromStart, double endpointSeconds) async {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/audiofileplayer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: audiofileplayer
description: A Flutter plugin for audio playback.
version: 1.0.1
version: 1.0.2
homepage: https://github.com/google/flutter.plugins/tree/master/packages/audiofileplayer

environment:
Expand Down