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: ability to set time position in audio #100

Open
wants to merge 2 commits into
base: master
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ No configuration required for this plugin.
| setVolume | ✅ | ✅ | ✅ |
| getDuration | ✅ | ✅ | ✅ |
| getCurrentTime | ✅ | ✅ | ✅ |
| setCurrentTime | ✅ | ✅ | ✅ |
| isPlaying | ✅ | ✅ | ✅ |

## Usage
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions ios/Plugin/AudioAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface NativeAudio {
unload(options: { assetId: string }): Promise<void>;
setVolume(options: { assetId: string; volume: number }): Promise<void>;
getCurrentTime(options: { assetId: string }): Promise<{ currentTime: number }>;
setCurrentTime(options: { assetId: string, time: number }): Promise<void>;
getDuration(options: { assetId: string }): Promise<{ duration: number }>;
isPlaying(options: { assetId: string }): Promise<{ isPlaying: boolean }>;
/**
Expand Down
6 changes: 6 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 }> {
const audio: HTMLAudioElement = this.getAudioAsset(options.assetId).audio;
return { currentTime: audio.currentTime };
Expand Down