Skip to content

Commit f6f2753

Browse files
committed
feat: seek option for playFromFile to start at a specific position
1 parent 4ca172b commit f6f2753

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/audio/index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export interface AudioPlayerOptions {
4646
* Should mix audio.
4747
*/
4848
audioMixing?: boolean;
49+
/**
50+
* start at a specific position
51+
*/
52+
seek?: number;
4953

5054
/**
5155
* iOS: The category for playing recorded music or other sounds that are central to the successful use of your app.
@@ -74,6 +78,7 @@ export interface AudioPlayerOptions {
7478
* Android: custom player datasource.
7579
*/
7680
dataSource?: android.media.MediaDataSource;
81+
7782
}
7883

7984
export interface AudioRecorderAndroidOptions {

src/audio/player.android.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ export class TNSPlayer extends Observable {
219219
player.reset();
220220
if (options.audioFile) {
221221
const audioPath = resolveAudioFilePath(options.audioFile);
222-
player.setDataSource(audioPath);
222+
if (audioPath.startsWith('content://')) {
223+
player.setDataSource(Utils.android.getApplicationContext(), android.net.Uri.parse(audioPath));
224+
} else {
225+
player.setDataSource(audioPath);
226+
}
223227

224228
// check if local file or remote - local then `prepare` is okay https://developer.android.com/reference/android/media/MediaPlayer.html#prepare()
225229
if (Utils.isFileOrResourcePath(audioPath)) {
@@ -249,6 +253,9 @@ export class TNSPlayer extends Observable {
249253
new android.media.MediaPlayer.OnPreparedListener({
250254
onPrepared: (mp) => {
251255
try {
256+
if (options.seek) {
257+
this.seekTo(options.seek);
258+
}
252259
if (options.autoPlay) {
253260
this.play();
254261
}
@@ -344,8 +351,7 @@ export class TNSPlayer extends Observable {
344351

345352
public async seekTo(time: number) {
346353
if (this._player) {
347-
time = time * 1000;
348-
this._player.seekTo(time);
354+
this._player.seekTo(time * 1000);
349355
this.notify({ eventName: AudioPlayerEvents.seek });
350356
}
351357
}

src/audio/player.ios.ts

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ export class TNSPlayer extends Observable {
123123
this._player.numberOfLoops = -1;
124124
}
125125

126+
if (options.seek) {
127+
this.seekTo(options.seek);
128+
}
126129
if (options.autoPlay !== false) {
127130
this._player.play();
128131
}

0 commit comments

Comments
 (0)