Skip to content

Commit 70a8905

Browse files
committed
fix(performance): remove total duration functionality from the plugin
1 parent 8e21ee6 commit 70a8905

File tree

9 files changed

+281
-361
lines changed

9 files changed

+281
-361
lines changed

examples/audio-service.ts

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
/**
2-
* This is an example Angular + Ionic 3 service to wrap the Cordova audio player plugin.
3-
* Simply drop this into your app, make sure you have the plugin installed,
4-
* add the service to your app.module.ts and import it wherever you need.
5-
*
6-
* An example of such usage might be:
7-
*
8-
* constructor(
9-
* private cdvAudioPlayer: CordovaAudioPlayerService,
10-
* ) {
11-
* this.cdvAudioPlayer.setOptions({ verbose: true, resetStreamOnPause: true })
12-
* .then(() => {
13-
* this.cdvAudioPlayer.setPlaylistItems([
14-
* { trackId: '12345', assetUrl: testUrls[0], albumArt: testImgs[0], artist: 'Awesome', album: 'Test Files', title: 'Test 1' },
15-
* { trackId: '678900', assetUrl: testUrls[1], albumArt: testImgs[1], artist: 'Awesome', album: 'Test Files', title: 'Test 2' },
16-
* { trackId: 'a1b2c3d4', assetUrl: testUrls[2], albumArt: testImgs[2], artist: 'Awesome', album: 'Test Files', title: 'Test 3' },
17-
* { trackId: 'a1bSTREAM', assetUrl: testUrls[3], albumArt: testImgs[3], artist: 'Awesome', album: 'Streams', title: 'The Stream', isStream: true },
18-
* ])
19-
* .then(() => {
20-
* this.cdvAudioPlayer.play();
21-
* }).catch((err) => console.log('YourService, cdvAudioPlayer setPlaylistItems error: ', err));
22-
* }).catch((err) => console.log('YourService, cdvAudioPlayer init error: ', err));
23-
*
1+
/**
2+
* This is an example Angular + Ionic 3 service to wrap the Cordova audio player plugin.
3+
* Simply drop this into your app, make sure you have the plugin installed,
4+
* add the service to your app.module.ts and import it wherever you need.
5+
*
6+
* An example of such usage might be:
7+
*
8+
* constructor(
9+
* private cdvAudioPlayer: CordovaAudioPlayerService,
10+
* ) {
11+
* this.cdvAudioPlayer.setOptions({ verbose: true, resetStreamOnPause: true })
12+
* .then(() => {
13+
* this.cdvAudioPlayer.setPlaylistItems([
14+
* { trackId: '12345', assetUrl: testUrls[0], albumArt: testImgs[0], artist: 'Awesome', album: 'Test Files', title: 'Test 1' },
15+
* { trackId: '678900', assetUrl: testUrls[1], albumArt: testImgs[1], artist: 'Awesome', album: 'Test Files', title: 'Test 2' },
16+
* { trackId: 'a1b2c3d4', assetUrl: testUrls[2], albumArt: testImgs[2], artist: 'Awesome', album: 'Test Files', title: 'Test 3' },
17+
* { trackId: 'a1bSTREAM', assetUrl: testUrls[3], albumArt: testImgs[3], artist: 'Awesome', album: 'Streams', title: 'The Stream', isStream: true },
18+
* ])
19+
* .then(() => {
20+
* this.cdvAudioPlayer.play();
21+
* }).catch((err) => console.log('YourService, cdvAudioPlayer setPlaylistItems error: ', err));
22+
* }).catch((err) => console.log('YourService, cdvAudioPlayer init error: ', err));
23+
*
2424
* this.cdvAudioPlayer.setOptions({ verbose: true, resetStreamOnPause: true });
2525
* this.cdvAudioPlayer.setVolume(0.5);
2626
*
27-
* this.cdvAudioPlayer.onStatus.subscribe((status) => {
28-
* console.log('YourService: Got RmxAudioPlayer onStatus: ', status);
29-
* });
30-
* }
27+
* this.cdvAudioPlayer.onStatus.subscribe((status) => {
28+
* console.log('YourService: Got RmxAudioPlayer onStatus: ', status);
29+
* });
30+
* }
3131
*/
3232
import { Injectable, NgZone } from '@angular/core';
3333
import { Platform } from 'ionic-angular';
@@ -250,12 +250,6 @@ export class CordovaAudioPlayerService {
250250
}).catch((ex) => console.warn(ex));
251251
}
252252

253-
getTotalDuration = () => {
254-
return this.wrapPromise('getTotalDuration', (resolve, reject) => {
255-
this.AudioPlayer.getTotalDuration(this.getSuccessCb(resolve), this.getErrorCb(reject));
256-
}).catch((ex) => console.warn(ex));
257-
}
258-
259253
getQueuePosition = () => {
260254
return this.wrapPromise('getQueuePosition', (resolve, reject) => {
261255
this.AudioPlayer.getQueuePosition(this.getSuccessCb(resolve), this.getErrorCb(reject));
@@ -285,10 +279,10 @@ export class CordovaAudioPlayerService {
285279
this.Log.log(`AudioPlayerSuccessCb [${name}]:`, data);
286280
return data;
287281
})
288-
.catch((err) => {
289-
this.Log.warn(`AudioPlayerSuccessCb [${name}]:`, err);
290-
throw err;
291-
});
282+
.catch((err) => {
283+
this.Log.warn(`AudioPlayerSuccessCb [${name}]:`, err);
284+
throw err;
285+
});
292286
}
293287

294288
return executor;

src/android/java/AudioPlayerPlugin.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,6 @@ public void run() {
312312
return true;
313313
}
314314

315-
if (GET_TOTAL_DURATION.equals(action)) {
316-
// Not yet implemented on android. I'm not sure how to, since the tracks haven't loaded yet.
317-
// On iOS, the AVQueuePlayer gets the metadata for all tracks immediately, that's why that works there.
318-
float totalDuration = 0f;
319-
PluginResult result = new PluginResult(PluginResult.Status.OK, totalDuration);
320-
new PluginCallback(callbackContext).send(result);
321-
return true;
322-
}
323-
324315
if (GET_QUEUE_POSITION.equals(action)) {
325316
// Not yet implemented on android. I'm not sure how to, since the tracks haven't loaded yet.
326317
// On iOS, the AVQueuePlayer gets the metadata for all tracks immediately, that's why that works there.

src/android/java/RmxConstants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public interface RmxConstants {
3434
String GET_PLAYBACK_VOLUME = "getPlaybackVolume";
3535
String GET_PLAYBACK_POSITION = "getPlaybackPosition";
3636
String GET_BUFFER_STATUS = "getCurrentBuffer";
37-
String GET_TOTAL_DURATION = "getTotalDuration";
3837
String GET_QUEUE_POSITION = "getQueuePosition";
3938

4039
String RELEASE = "release";

src/ios/AVBidirectionalQueuePlayer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
-(BOOL)isAtEnd;
3434
-(BOOL)isPlaying;
3535

36-
-(CMTime)estimatedTotalDuration;
3736
-(CMTime)currentTimeOffsetInQueue;
3837
-(void)seekToTimeInQueue:(CMTime)time completionHandler:(void (^)(BOOL))completionHandler;
3938
-(void)setCurrentIndex:(NSUInteger)currentIndex completionHandler:(void (^)(BOOL)) completionHandler;

src/ios/AVBidirectionalQueuePlayer.m

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
@implementation AVBidirectionalQueuePlayer {
1515
NSMutableArray * _itemsForPlayer;
16-
// CMTime _estimatedDuration;
1716
}
1817

1918
-(NSMutableArray *)itemsForPlayer {
@@ -36,7 +35,6 @@ -(id)init {
3635
self = [super init];
3736
if (self) {
3837
self.itemsForPlayer = [NSMutableArray new];
39-
// _estimatedDuration = kCMTimeZero;
4038
}
4139
return self;
4240
}
@@ -47,7 +45,6 @@ -(id)initWithItems:(NSArray *)items
4745
self = [super initWithItems:items];
4846
if (self){
4947
self.itemsForPlayer = [NSMutableArray arrayWithArray:items];
50-
// _estimatedDuration = kCMTimeInvalid;
5148
}
5249
return self;
5350
}
@@ -185,7 +182,6 @@ -(void)removeAllItems
185182
{
186183
// This does the same thing as the normal AVQueuePlayer removeAllItems, but clears our collection copy
187184
[super removeAllItems];
188-
// _estimatedDuration = kCMTimeZero;
189185
[_itemsForPlayer removeAllObjects];
190186

191187
[[NSNotificationCenter defaultCenter] postNotificationName:AVBidirectionalQueueCleared object:self userInfo:nil];
@@ -200,10 +196,6 @@ -(void)removeItem:(AVPlayerItem *)item
200196
// array before the current value.
201197
[super removeItem:item];
202198

203-
// if (CMTIME_IS_VALID(item.duration)) {
204-
// _estimatedDuration = CMTimeSubtract(_estimatedDuration, item.duration);
205-
// }
206-
207199
[_itemsForPlayer removeObject:item];
208200
[[NSNotificationCenter defaultCenter] postNotificationName:AVBidirectionalQueueRemovedItem object:self userInfo:@{@"item":item}];
209201
}
@@ -214,19 +206,6 @@ -(void)insertItem:(AVPlayerItem *)item afterItem:(AVPlayerItem *)afterItem
214206
// proper location in the itemsForPlayer array and increments the nowPlayingIndex if necessary.
215207
[super insertItem:item afterItem:afterItem];
216208

217-
// https://github.com/Rolamix/cordova-plugin-playlist/issues/19
218-
// This causes some pretty bad performance so I am going to remove it for now.
219-
// You should be maintaining a database of your tracks, not using their metadata characteristics
220-
// at load time to decide what their duration is..
221-
222-
// if (CMTIME_IS_NUMERIC(item.duration)) {
223-
// NSLog(@"duration: %5.2f", (double) CMTimeGetSeconds(item.duration));
224-
// if (CMTimeCompare(_estimatedDuration, kCMTimeZero) == 0)
225-
// _estimatedDuration = item.duration;
226-
// else
227-
// _estimatedDuration = CMTimeAdd(_estimatedDuration, item.duration);
228-
// }
229-
230209
if ([_itemsForPlayer containsObject:afterItem]){ // AfterItem is non-nil
231210
if ([_itemsForPlayer indexOfObject:afterItem] < [_itemsForPlayer count] - 1){
232211
[_itemsForPlayer insertObject:item atIndex:[_itemsForPlayer indexOfObject:afterItem] + 1];
@@ -248,20 +227,6 @@ -(void)insertAllItems:(NSMutableArray *)itemsForPlayer {
248227
[center postNotificationName:AVBidirectionalQueueAddedAllItems object:self userInfo:@{@"items":itemsForPlayer}];
249228
}
250229

251-
-(CMTime)estimatedTotalDuration {
252-
// if (!CMTIME_IS_VALID(self->_estimatedDuration)) {
253-
// //NSTimeInterval dur = 0.0;
254-
// CMTime duration = kCMTimeZero;
255-
// for (AVPlayerItem * item in _itemsForPlayer) {
256-
// if (CMTIME_IS_VALID(item.duration))
257-
// duration = CMTimeAdd(duration, item.duration);
258-
// }
259-
// self->_estimatedDuration = duration;
260-
// }
261-
// return self->_estimatedDuration;
262-
return kCMTimeZero;
263-
}
264-
265230
-(CMTime)currentTimeOffsetInQueue {
266231
CMTime timeOffset = kCMTimeZero;
267232
NSUInteger currentIndex = self.currentIndex;

src/ios/RmxAudioPlayer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
- (void) getPlaybackVolume:(CDVInvokedUrlCommand *) command;
5656
- (void) getPlaybackPosition:(CDVInvokedUrlCommand *) command;
5757
- (void) getCurrentBuffer:(CDVInvokedUrlCommand *) command;
58-
- (void) getTotalDuration:(CDVInvokedUrlCommand *) command;
5958
- (void) getQueuePosition:(CDVInvokedUrlCommand *) command;
6059

6160
// Cleanup

src/ios/RmxAudioPlayer.m

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ @interface RmxAudioPlayer() {
2929
@property (nonatomic) BOOL isAtEnd;
3030
@property (nonatomic) BOOL isAtBeginning;
3131
@property (nonatomic) BOOL isPlaying;
32-
@property (nonatomic) float estimatedDuration;
3332
@property (nonatomic) float queuePosition;
3433
@end
3534

@@ -374,15 +373,6 @@ - (void) getCurrentBuffer:(CDVInvokedUrlCommand *) command {
374373
}
375374

376375

377-
- (void) getTotalDuration:(CDVInvokedUrlCommand *) command {
378-
float duration = self.estimatedDuration;
379-
NSLog(@"RmxAudioPlayer.execute=getTotalDuration, %f", duration);
380-
381-
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:duration];
382-
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
383-
}
384-
385-
386376
- (void) getQueuePosition:(CDVInvokedUrlCommand *) command {
387377
float position = self.queuePosition;
388378
NSLog(@"RmxAudioPlayer.execute=getQueuePosition, %f", position);
@@ -1185,10 +1175,6 @@ - (BOOL) isPlaying {
11851175
return [self avQueuePlayer].isPlaying;
11861176
}
11871177

1188-
- (float) estimatedDuration {
1189-
return CMTimeGetSeconds([self avQueuePlayer].estimatedTotalDuration);
1190-
}
1191-
11921178
- (float) queuePosition {
11931179
return CMTimeGetSeconds([self avQueuePlayer].currentTimeOffsetInQueue);
11941180
}

src/js/RmxAudioPlayer.ts

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ export class RmxAudioPlayer {
137137
* Player interface
138138
*/
139139

140-
/**
141-
* Returns a promise that resolves when the plugin is ready.
142-
*/
140+
/**
141+
* Returns a promise that resolves when the plugin is ready.
142+
*/
143143
ready = () => {
144144
return this._initPromise;
145145
}
@@ -168,31 +168,31 @@ export class RmxAudioPlayer {
168168
};
169169

170170
exec(onNativeStatus, error, 'RmxAudioPlayer', 'initialize', []);
171-
// channel.initializationComplete('onRmxAudioPlayerReady');
171+
// channel.initializationComplete('onRmxAudioPlayerReady');
172172
// });
173173

174174
return this._initPromise;
175175
}
176176

177-
/**
178-
* Sets the player options. This can be called at any time and is not required before playback can be initiated.
179-
*/
177+
/**
178+
* Sets the player options. This can be called at any time and is not required before playback can be initiated.
179+
*/
180180
setOptions = (successCallback: SuccessCallback, errorCallback: ErrorCallback, options: AudioPlayerOptions) => {
181-
this.options = {...this.options, ...options};
181+
this.options = { ...this.options, ...options };
182182
exec(successCallback, errorCallback, 'RmxAudioPlayer', 'setOptions', [options]);
183183
}
184184

185185
/**
186186
* Playlist item management
187187
*/
188188

189-
/**
190-
* Sets the entire list of tracks to be played by the playlist.
191-
* This will clear all previous items from the playlist.
192-
* If you pass options.retainPosition = true, the current playback position will be
193-
* recorded and used when playback restarts. This can be used, for example, to set the
194-
* playlist to a new set of tracks, but retain the currently-playing item to avoid skipping.
195-
*/
189+
/**
190+
* Sets the entire list of tracks to be played by the playlist.
191+
* This will clear all previous items from the playlist.
192+
* If you pass options.retainPosition = true, the current playback position will be
193+
* recorded and used when playback restarts. This can be used, for example, to set the
194+
* playlist to a new set of tracks, but retain the currently-playing item to avoid skipping.
195+
*/
196196
setPlaylistItems = (successCallback: SuccessCallback, errorCallback: ErrorCallback, items: AudioTrack[], options?: PlaylistItemOptions) => {
197197
exec(successCallback, errorCallback, 'RmxAudioPlayer', 'setPlaylistItems', [this.validateTracks(items), options || {}]);
198198
}
@@ -241,9 +241,9 @@ export class RmxAudioPlayer {
241241
* Playback management
242242
*/
243243

244-
/**
245-
* Begin playback. If no tracks have been added, this has no effect.
246-
*/
244+
/**
245+
* Begin playback. If no tracks have been added, this has no effect.
246+
*/
247247
play = (successCallback: SuccessCallback, errorCallback: ErrorCallback) => {
248248
exec(successCallback, errorCallback, 'RmxAudioPlayer', 'play', []);
249249
}
@@ -328,9 +328,9 @@ export class RmxAudioPlayer {
328328
* Get accessors
329329
*/
330330

331-
/**
332-
* Reports the current playback rate.
333-
*/
331+
/**
332+
* Reports the current playback rate.
333+
*/
334334
getPlaybackRate = (successCallback: SuccessCallback, errorCallback: ErrorCallback) => {
335335
exec(successCallback, errorCallback, 'RmxAudioPlayer', 'getPlaybackRate', []);
336336
}
@@ -358,14 +358,6 @@ export class RmxAudioPlayer {
358358
exec(successCallback, errorCallback, 'RmxAudioPlayer', 'getCurrentBuffer', []);
359359
}
360360

361-
/**
362-
* (iOS only): Reports the duration of the entire playlist, in seconds (e.g. 4500 seconds for the entire playlist)
363-
* Not implemented on Android since durations are not known ahead of time.
364-
*/
365-
getTotalDuration = (successCallback: SuccessCallback, errorCallback: ErrorCallback) => {
366-
exec(successCallback, errorCallback, 'RmxAudioPlayer', 'getTotalDuration', []);
367-
}
368-
369361
/**
370362
* (iOS only): Gets the overall playback position in the entire queue, in seconds (e.g. 1047 seconds).
371363
* Not implemented on Android since durations are not known ahead of time.
@@ -379,11 +371,11 @@ export class RmxAudioPlayer {
379371
* Status event handling
380372
*/
381373

382-
/**
383-
* @internal
384-
* Call this function to emit an onStatus event via the on('status') handler.
385-
* Internal use only, to raise events received from the native interface.
386-
*/
374+
/**
375+
* @internal
376+
* Call this function to emit an onStatus event via the on('status') handler.
377+
* Internal use only, to raise events received from the native interface.
378+
*/
387379
protected onStatus(trackId: string, type: RmxAudioStatusMessage, value: OnStatusCallbackUpdateData | OnStatusTrackChangedData | OnStatusErrorCallbackData) {
388380
const status = { type, trackId, value };
389381
if (this.options.verbose) {

0 commit comments

Comments
 (0)