Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix - 230/Stop player should not be freed the resources #268

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ class AudioPlayer(
}
isPlayerPrepared = false
player?.stop()
player?.release()
result.success(true)
}

Expand All @@ -146,6 +145,15 @@ class AudioPlayer(
result.error(Constants.LOG_TAG, "Failed to pause the player", e.toString())
}

}
fun release(result: MethodChannel.Result) {
try {
player?.release()
result.success(true)
} catch (e: Exception) {
result.error(Constants.LOG_TAG, "Failed to release player resource", e.toString())
}

}

fun setVolume(volume: Float?, result: MethodChannel.Result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
result.error(Constants.LOG_TAG, "Player key can't be null", "")
}
}
Constants.releaseAudioPlayerResource -> {
val key = call.argument(Constants.playerKey) as String?
audioPlayers[key]?.release(result)
recorder = null
ujas-m-simformsolutions marked this conversation as resolved.
Show resolved Hide resolved
}
Constants.seekTo -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val progress = call.argument(Constants.progress) as Int?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object Constants {
const val startPlayer = "startPlayer"
const val stopPlayer = "stopPlayer"
const val pausePlayer = "pausePlayer"
const val releaseAudioPlayerResource = "releaseAudioPlayerResource"
ujas-m-simformsolutions marked this conversation as resolved.
Show resolved Hide resolved
const val seekTo = "seekTo"
const val progress = "progress"
const val setVolume = "setVolume"
Expand Down
13 changes: 8 additions & 5 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -155,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -199,10 +199,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -213,6 +215,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -339,7 +342,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -420,7 +423,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -469,7 +472,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
6 changes: 5 additions & 1 deletion ios/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
func stopPlayer(result: @escaping FlutterResult) {
stopListening()
player?.stop()
player = nil
timer = nil
result(true)
}

func release(result: @escaping FlutterResult) {
player = nil
result(true)
}

func getDuration(_ type: DurationType, _ result: @escaping FlutterResult) throws {
if type == .Current {
let ms = (player?.currentTime ?? 0) * 1000
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/SwiftAudioWaveformsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public class SwiftAudioWaveformsPlugin: NSObject, FlutterPlugin {
result(FlutterError(code: Constants.audioWaveforms, message: "Can not stop player", details: "Player key is null"))
}
break
case Constants.releaseAudioPlayerResource:
let key = args?[Constants.playerKey] as? String
if(key != nil){
audioPlayers[key!]?.release(result: result)
}
break;
case Constants.seekTo:
let key = args?[Constants.playerKey] as? String
if(key != nil){
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct Constants {
static let startPlayer = "startPlayer"
static let stopPlayer = "stopPlayer"
static let pausePlayer = "pausePlayer"
static let releaseAudioPlayerResource = "releaseAudioPlayerResource"
ujas-m-simformsolutions marked this conversation as resolved.
Show resolved Hide resolved
static let seekTo = "seekTo"
static let progress = "progress"
static let setVolume = "setVolume"
Expand Down
6 changes: 6 additions & 0 deletions lib/src/base/audio_waveforms_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ class AudioWaveformsInterface {
return result ?? false;
}

///platform call to release resource
Future<bool> release(String key)async{
var result = await _methodChannel.invokeMethod(Constants.releaseAudioPlayerResource,{Constants.playerKey : key});
ujas-m-simformsolutions marked this conversation as resolved.
Show resolved Hide resolved
return result ?? false;
}

///platform call to pause player
Future<bool> pausePlayer(String key) async {
var result = await _methodChannel
Expand Down
1 change: 1 addition & 0 deletions lib/src/base/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Constants {
static const String preparePlayer = "preparePlayer";
static const String startPlayer = "startPlayer";
static const String stopPlayer = "stopPlayer";
static const String releaseAudioPlayerResource = "releaseAudioPlayerResource";
static const String pausePlayer = "pausePlayer";
static const String seekTo = "seekTo";
static const String progress = "progress";
Expand Down
8 changes: 7 additions & 1 deletion lib/src/controllers/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class PlayerController extends ChangeNotifier {
notifyListeners();
}

/// A function to stop player. After calling this, resources are freed.
/// A function to stop player. After calling this.
Future<void> stopPlayer() async {
final isStopped =
await AudioWaveformsInterface.instance.stopPlayer(playerKey);
Expand All @@ -219,6 +219,11 @@ class PlayerController extends ChangeNotifier {
notifyListeners();
}

/// Releases the resources associated with this player.
Future<void> release() async {
await AudioWaveformsInterface.instance.release(playerKey);
}

/// Sets volume for this player. Doesn't throw Exception.
/// Returns false if it couldn't set the volume.
///
Expand Down Expand Up @@ -264,6 +269,7 @@ class PlayerController extends ChangeNotifier {
@override
void dispose() async {
if (playerState != PlayerState.stopped) await stopPlayer();
await release();
PlatformStreams.instance.playerControllerFactory.remove(this);
if (PlatformStreams.instance.playerControllerFactory.length == 1) {
PlatformStreams.instance.dispose();
Expand Down