From cb1026772e324b5da471cdf338b435bf69cafb87 Mon Sep 17 00:00:00 2001 From: Christopher Weinhardt Date: Fri, 12 Apr 2024 22:18:34 -0400 Subject: [PATCH 1/2] Add time position setter --- .../com/getcapacitor/community/audio/AudioAsset.java | 10 ++++++++++ .../getcapacitor/community/audio/AudioDispatcher.java | 6 ++++++ ios/Plugin/AudioAsset.swift | 9 +++++++++ ios/Plugin/Plugin.swift | 10 ++++++++++ src/definitions.ts | 1 + src/web.ts | 6 ++++++ 6 files changed, 42 insertions(+) diff --git a/android/src/main/java/com/getcapacitor/community/audio/AudioAsset.java b/android/src/main/java/com/getcapacitor/community/audio/AudioAsset.java index ade5282..e900057 100644 --- a/android/src/main/java/com/getcapacitor/community/audio/AudioAsset.java +++ b/android/src/main/java/com/getcapacitor/community/audio/AudioAsset.java @@ -56,6 +56,16 @@ public double getDuration() { return 0; } + public void setCurrentPosition(double time) { + if (audioList.size() != 1) return; + + AudioDispatcher audio = audioList.get(playIndex); + + if (audio != null) { + audio.setCurrentPosition(time); + } + } + public double getCurrentPosition() { if (audioList.size() != 1) return 0; diff --git a/android/src/main/java/com/getcapacitor/community/audio/AudioDispatcher.java b/android/src/main/java/com/getcapacitor/community/audio/AudioDispatcher.java index 6ed5d39..df95329 100644 --- a/android/src/main/java/com/getcapacitor/community/audio/AudioDispatcher.java +++ b/android/src/main/java/com/getcapacitor/community/audio/AudioDispatcher.java @@ -55,6 +55,12 @@ public double getDuration() { return mediaPlayer.getDuration() / 1000.0; } + public void setCurrentPosition(double time) { + if (mediaState == PLAYING || mediaState == PAUSE) { + mediaPlayer.seekTo((int) (time * 1000)); + } + } + public double getCurrentPosition() { return mediaPlayer.getCurrentPosition() / 1000.0; } diff --git a/ios/Plugin/AudioAsset.swift b/ios/Plugin/AudioAsset.swift index b0221c8..f01b635 100644 --- a/ios/Plugin/AudioAsset.swift +++ b/ios/Plugin/AudioAsset.swift @@ -48,6 +48,15 @@ public class AudioAsset: NSObject, AVAudioPlayerDelegate { } } + func setCurrentTime(time: TimeInterval) { + if channels.count != 1 { + return + } + + let player: AVAudioPlayer = channels.object(at: playIndex) as! AVAudioPlayer + player.currentTime = time + } + func getCurrentTime() -> TimeInterval { if channels.count != 1 { return 0 diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index caacec3..3ad471c 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -111,6 +111,16 @@ public class NativeAudio: CAPPlugin { ]) } + @obj func setCurrentTime(_ call: CAPPluginCall) { + guard let audioAsset: AudioAsset = self.getAudioAsset(call) else { + return + } + + let time = call.getDouble("time") ?? 0 + audioAsset.setCurrentTime(time: time) + call.resolve() + } + @objc func getCurrentTime(_ call: CAPPluginCall) { guard let audioAsset: AudioAsset = self.getAudioAsset(call) else { return diff --git a/src/definitions.ts b/src/definitions.ts index 3cd0386..aa71014 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -11,6 +11,7 @@ export interface NativeAudio { unload(options: { assetId: string }): Promise; setVolume(options: { assetId: string; volume: number }): Promise; getCurrentTime(options: { assetId: string }): Promise<{ currentTime: number }>; + setCurrentTime(options: { assetId: string, time: number }): Promise; getDuration(options: { assetId: string }): Promise<{ duration: number }>; isPlaying(options: { assetId: string }): Promise<{ isPlaying: boolean }>; /** diff --git a/src/web.ts b/src/web.ts index ed82df0..7b244e3 100644 --- a/src/web.ts +++ b/src/web.ts @@ -19,6 +19,12 @@ export class NativeAudioWeb extends WebPlugin implements NativeAudio { return audio.pause(); } + async setCurrentTime(options: { assetId: string; time: number; }): Promise { + const audio: HTMLAudioElement = this.getAudioAsset(options.assetId).audio; + audio.currentTime = options.time; + return; + } + async getCurrentTime(options: { assetId: string }): Promise<{ currentTime: number }> { const audio: HTMLAudioElement = this.getAudioAsset(options.assetId).audio; return { currentTime: audio.currentTime }; From dc51da5775863c49946b896aed61abb483129b9c Mon Sep 17 00:00:00 2001 From: Christopher Weinhardt Date: Fri, 12 Apr 2024 22:26:54 -0400 Subject: [PATCH 2/2] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 9e05138..8daa325 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ No configuration required for this plugin. | setVolume | ✅ | ✅ | ✅ | | getDuration | ✅ | ✅ | ✅ | | getCurrentTime | ✅ | ✅ | ✅ | +| setCurrentTime | ✅ | ✅ | ✅ | | isPlaying | ✅ | ✅ | ✅ | ## Usage @@ -188,6 +189,15 @@ NativeAudio.getCurrentTime({ console.log(result.currentTime); }) +/** + * this method will set the new current time of a playing audio file. + * only works if channels == 1 + */ +NativeAudio.setCurrentTime({ + assetId: 'fire', + time: 60.0 +}); + /** * This method will return false if audio is paused or not loaded. * @param assetId - identifier of the asset