Skip to content

Commit

Permalink
feat: Add isPlaying method
Browse files Browse the repository at this point in the history
  • Loading branch information
mamane10 committed Dec 24, 2021
1 parent 937986b commit 94e1984
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ No configuration required for this plugin.
| setVolume ||||
| getDuration ||||
| getCurrentTime ||||
| isPlaying ||||

## Usage

Expand Down Expand Up @@ -185,6 +186,18 @@ NativeAudio.getCurrentTime({
.then(result => {
console.log(result.currentTime);
})

/**
* This method will return false if audio is paused or not loaded.
* @param assetId - identifier of the asset
* @returns {isPlaying: boolean}
*/
NativeAudio.isPlaying({
assetId: 'fire'
})
.then(result => {
console.log(result.isPlaying);
})
```

## API
Expand Down Expand Up @@ -341,6 +354,21 @@ getDuration(options: { assetId: string; }) => Promise<{ duration: number; }>
--------------------


### isPlaying(...)

```typescript
isPlaying(options: { assetId: string; }) => Promise<{ isPlaying: boolean; }>
```

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

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

--------------------


### Interfaces


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,11 @@ public void setVolume(float volume) throws Exception {
}
}
}

public boolean isPlaying() throws Exception {
if (audioList.size() != 1) return false;

return audioList.get(playIndex).isPlaying();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,8 @@ public void onSeekComplete(MediaPlayer mp) {
mediaState = PLAYING;
}
}

public boolean isPlaying() throws Exception {
return mediaPlayer.isPlaying();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,33 @@ public void setVolume(PluginCall call) {
}
}

@PluginMethod
public void isPlaying(final PluginCall call) {
try {
initSoundPool();

String audioId = call.getString(ASSET_ID);

if (!isStringValid(audioId)) {
call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
return;
}

if (audioAssetList.containsKey(audioId)) {
AudioAsset asset = audioAssetList.get(audioId);
if (asset != null) {
call.resolve(
new JSObject().put("isPlaying", asset.isPlaying())
);
}
} else {
call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
}
} catch (Exception ex) {
call.reject(ex.getMessage());
}
}

public void dispatchComplete(String assetId) {
JSObject ret = new JSObject();
ret.put("assetId", assetId);
Expand Down
10 changes: 10 additions & 0 deletions ios/Plugin/AudioAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,14 @@ public class AudioAsset: NSObject, AVAudioPlayerDelegate {
func playerDecodeError(player: AVAudioPlayer!, error: NSError!) {

}

func isPlaying() -> Bool {
if channels.count != 1 {
return 0
}

let player: AVAudioPlayer = channels.object(at: playIndex) as! AVAudioPlayer

return player.isPlaying
}
}
11 changes: 11 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ public class NativeAudio: CAPPlugin {
audioAsset.setVolume(volume: volume as NSNumber)
call.success()
}

@objc func isPlaying(_ call: CAPPluginCall) {
guard let audioAsset: AudioAsset = self.getAudioAsset(call) else {
return
}

call.resolve([
"isPlaying": audioAsset.isPlaying()
])
}


private func preloadAsset(_ call: CAPPluginCall, isComplex complex: Bool) {
let audioId = call.getString(Constant.AssetIdKey) ?? ""
Expand Down
3 changes: 3 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export interface NativeAudio {
assetId: string;
}): Promise<{ currentTime: number }>;
getDuration(options: { assetId: string }): Promise<{ duration: number }>;
isPlaying(options: {
assetId: string;
}): Promise<{ isPlaying: boolean }>;
}

export interface ConfigureOptions {
Expand Down
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class NativeAudioWeb extends WebPlugin implements NativeAudio {
console.log(options);
throw new Error("Method not implemented.");
}
isPlaying(options: { assetId: string }): Promise<{ isPlaying: boolean; }> {
console.log(options)
throw new Error('Method not implemented.');
}
}

const NativeAudio = new NativeAudioWeb();
Expand Down

0 comments on commit 94e1984

Please sign in to comment.