diff --git a/README.md b/README.md index 7d94003..0f60c38 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ Load an audio file ### isPreloaded(...) ```typescript -isPreloaded(options: PreloadOptions) => Promise +isPreloaded(options: PreloadOptions) => Promise<{ found: boolean; }> ``` Check if an audio file is preloaded @@ -261,7 +261,7 @@ Check if an audio file is preloaded | ------------- | --------------------------------------------------------- | | **`options`** | PreloadOptions | -**Returns:** Promise<boolean> +**Returns:** Promise<{ found: boolean; }> **Since:** 6.1.0 @@ -404,13 +404,30 @@ Set the rate of an audio file -------------------- +### setCurrentTime(...) + +```typescript +setCurrentTime(options: { assetId: string; time: number; }) => Promise +``` + +Set the current time of an audio file + +| Param | Type | +| ------------- | ----------------------------------------------- | +| **`options`** | { assetId: string; time: number; } | + +**Since:** 6.5.0 + +-------------------- + + ### getCurrentTime(...) ```typescript getCurrentTime(options: { assetId: string; }) => Promise<{ currentTime: number; }> ``` -Set the current time of an audio file +Get the current time of an audio file | Param | Type | | ------------- | --------------------------------- | diff --git a/android/src/main/java/ee/forgr/audio/AudioAsset.java b/android/src/main/java/ee/forgr/audio/AudioAsset.java index 7a45258..3e1df05 100644 --- a/android/src/main/java/ee/forgr/audio/AudioAsset.java +++ b/android/src/main/java/ee/forgr/audio/AudioAsset.java @@ -66,6 +66,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/ee/forgr/audio/AudioDispatcher.java b/android/src/main/java/ee/forgr/audio/AudioDispatcher.java index 0dd97ef..7e562f5 100644 --- a/android/src/main/java/ee/forgr/audio/AudioDispatcher.java +++ b/android/src/main/java/ee/forgr/audio/AudioDispatcher.java @@ -56,6 +56,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 92b7cc9..a11806d 100644 --- a/ios/Plugin/AudioAsset.swift +++ b/ios/Plugin/AudioAsset.swift @@ -59,6 +59,16 @@ public class AudioAsset: NSObject, AVAudioPlayerDelegate { return player.currentTime } + func setCurrentTime(time: TimeInterval) { + if channels.count != 1 { + return + } + + let player: AVAudioPlayer = channels[playIndex] + + player.currentTime = time + } + func getDuration() -> TimeInterval { if channels.count != 1 { return 0 diff --git a/ios/Plugin/Plugin.m b/ios/Plugin/Plugin.m index da4d0a6..cbe15e3 100644 --- a/ios/Plugin/Plugin.m +++ b/ios/Plugin/Plugin.m @@ -15,6 +15,7 @@ CAP_PLUGIN_METHOD(resume, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(unload, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(setVolume, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(setCurrentTime, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getCurrentTime, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getDuration, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(isPlaying, CAPPluginReturnPromise); diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index b99c9f3..d63f667 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -188,6 +188,16 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate { return asset as? AudioAsset } + @objc 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 getDuration(_ call: CAPPluginCall) { guard let audioAsset: AudioAsset = self.getAudioAsset(call) else { return diff --git a/src/definitions.ts b/src/definitions.ts index 610fb85..9435ce3 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -175,6 +175,13 @@ export interface NativeAudio { setRate(options: { assetId: string; rate: number }): Promise; /** * Set the current time of an audio file + * @since 6.5.0 + * @param option {@link AssetPlayOptions} + * @returns {Promise} + */ + setCurrentTime(options: { assetId: string, time: number }): Promise; + /** + * Get the current time of an audio file * @since 5.0.0 * @param option {@link AssetPlayOptions} * @returns {Promise<{ currentTime: number }>} diff --git a/src/web.ts b/src/web.ts index e2bfd0d..36e3ff3 100644 --- a/src/web.ts +++ b/src/web.ts @@ -25,6 +25,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 }> {