Skip to content

Commit

Permalink
Merge pull request #268 from SimformSolutionsPvtLtd/fix/230/stop_play…
Browse files Browse the repository at this point in the history
…er_should_not_freed_the_resournce

bugfix - 230/Stop player should not be freed the resources
  • Loading branch information
himanshuGandhiSimform authored Mar 1, 2024
2 parents d91c33e + 1ac99f1 commit 538b3d4
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 13 deletions.
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 @@ -115,6 +115,10 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
result.error(Constants.LOG_TAG, "Player key can't be null", "")
}
}
Constants.releasePlayer -> {
val key = call.argument(Constants.playerKey) as String?
audioPlayers[key]?.release(result)
}
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 releasePlayer = "releasePlayer"
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.releasePlayer:
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 releasePlayer = "releasePlayer"
static let seekTo = "seekTo"
static let progress = "progress"
static let setVolume = "setVolume"
Expand Down
18 changes: 14 additions & 4 deletions lib/src/base/audio_waveforms_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,25 @@ class AudioWaveformsInterface {

///platform call to stop player
Future<bool> stopPlayer(String key) async {
var result = await _methodChannel
.invokeMethod(Constants.stopPlayer, {Constants.playerKey: key});
var result = await _methodChannel.invokeMethod(Constants.stopPlayer, {
Constants.playerKey: key,
});
return result ?? false;
}

///platform call to release resource
Future<bool> release(String key) async {
var result = await _methodChannel.invokeMethod(Constants.releasePlayer, {
Constants.playerKey: key,
});
return result ?? false;
}

///platform call to pause player
Future<bool> pausePlayer(String key) async {
var result = await _methodChannel
.invokeMethod(Constants.pausePlayer, {Constants.playerKey: key});
var result = await _methodChannel.invokeMethod(Constants.pausePlayer, {
Constants.playerKey: key,
});
return result ?? false;
}

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 releasePlayer = "releasePlayer";
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

0 comments on commit 538b3d4

Please sign in to comment.