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

feat(setCurrentTime): Added a setCurrentTime function (not tested) #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Load an audio file
### isPreloaded(...)

```typescript
isPreloaded(options: PreloadOptions) => Promise<boolean>
isPreloaded(options: PreloadOptions) => Promise<{ found: boolean; }>
```

Check if an audio file is preloaded
Expand All @@ -261,7 +261,7 @@ Check if an audio file is preloaded
| ------------- | --------------------------------------------------------- |
| **`options`** | <code><a href="#preloadoptions">PreloadOptions</a></code> |

**Returns:** <code>Promise&lt;boolean&gt;</code>
**Returns:** <code>Promise&lt;{ found: boolean; }&gt;</code>

**Since:** 6.1.0

Expand Down Expand Up @@ -404,13 +404,30 @@ Set the rate of an audio file
--------------------


### setCurrentTime(...)

```typescript
setCurrentTime(options: { assetId: string; time: number; }) => Promise<void>
```

Set the current time of an audio file

| Param | Type |
| ------------- | ----------------------------------------------- |
| **`options`** | <code>{ assetId: string; time: number; }</code> |

**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 |
| ------------- | --------------------------------- |
Expand Down
10 changes: 10 additions & 0 deletions android/src/main/java/ee/forgr/audio/AudioAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions android/src/main/java/ee/forgr/audio/AudioDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions ios/Plugin/AudioAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ export interface NativeAudio {
setRate(options: { assetId: string; rate: number }): Promise<void>;
/**
* Set the current time of an audio file
* @since 6.5.0
* @param option {@link AssetPlayOptions}
* @returns {Promise<void>}
*/
setCurrentTime(options: { assetId: string, time: number }): Promise<void>;
/**
* Get the current time of an audio file
* @since 5.0.0
* @param option {@link AssetPlayOptions}
* @returns {Promise<{ currentTime: number }>}
Expand Down
6 changes: 6 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export class NativeAudioWeb extends WebPlugin implements NativeAudio {
return audio.pause();
}

async setCurrentTime(options: { assetId: string; time: number; }): Promise<void> {
const audio: HTMLAudioElement = this.getAudioAsset(options.assetId).audio;
audio.currentTime = options.time;
return;
}

async getCurrentTime(options: {
assetId: string;
}): Promise<{ currentTime: number }> {
Expand Down