Skip to content

Commit

Permalink
Merge pull request #1 from nypublicradio/bono/setVolume
Browse files Browse the repository at this point in the history
Bono/set volume
  • Loading branch information
bonomite authored Aug 29, 2024
2 parents 8a5de76 + 5a20e09 commit 5191f87
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ npx cap sync
* [`resume()`](#resume)
* [`seekTo(...)`](#seekto)
* [`stop()`](#stop)
* [`setVolume(...)`](#setvolume)
* [`setPlaybackRate(...)`](#setplaybackrate)
* [`setNowPlayingInfo(...)`](#setnowplayinginfo)
* [`addListener('error' | 'play' | 'pause' | 'stop' | 'timeUpdate' | 'buffering', ...)`](#addlistenererror--play--pause--stop--timeupdate--buffering-)
Expand Down Expand Up @@ -83,6 +84,19 @@ stop() => Promise<void>
--------------------


### setVolume(...)

```typescript
setVolume(options: { volume: number; }) => Promise<void>
```

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

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


### setPlaybackRate(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,18 @@ public void onAudioFocusChange(int focusChange) {
});
}

@PluginMethod
public void setVolume(PluginCall call) {
handler.post(() -> {
Float volume;
try {
volume = (float) call.getData().getDouble("volume");
} catch (JSONException e) {
throw new RuntimeException(e);
}
player.setVolume(volume);
});
}

public void actionCallback(String action) {
actionCallback(action, new JSObject());
Expand Down
5 changes: 5 additions & 0 deletions ios/Sources/RemoteStreamerPlugin/RemoteStreamer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ class RemoteStreamer: NSObject {
NotificationCenter.default.post(name: Notification.Name("RemoteStreamerTimeUpdate"), object: nil, userInfo: ["currentTime": time])
}

func setVolume(volume: Double) {
//player?.setVolume({volume})
player?.volume = Float(volume)
}

@objc private func playerDidFinishPlaying(note: NSNotification) {
NotificationCenter.default.post(name: Notification.Name("RemoteStreamerEnded"), object: nil, userInfo:
["ended": true])
Expand Down
9 changes: 8 additions & 1 deletion ios/Sources/RemoteStreamerPlugin/RemoteStreamerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class RemoteStreamerPlugin: CAPPlugin, CAPBridgedPlugin {
CAPPluginMethod(name: "resume", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "stop", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "seekTo", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setNowPlayingInfo", returnType: CAPPluginReturnPromise)
CAPPluginMethod(name: "setNowPlayingInfo", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setVolume", returnType: CAPPluginReturnPromise)
]

private let implementation = RemoteStreamer()
Expand Down Expand Up @@ -112,6 +113,12 @@ public class RemoteStreamerPlugin: CAPPlugin, CAPBridgedPlugin {
call.resolve()
}

@objc func setVolume(_ call: CAPPluginCall) {
print("set volume")
implementation.setVolume(volume: call.getDouble("volume")!)
call.resolve()
}

@objc func seekTo(_ call: CAPPluginCall) {
guard let position = call.getDouble("position") else {
call.reject("Must provide a position")
Expand Down
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface RemoteStreamerPlugin {
resume(): Promise<void>;
seekTo(options: { position: number }): Promise<void>;
stop(): Promise<void>;
setVolume(options: { volume: number }): Promise<void>;
setPlaybackRate(options: { rate: number }): Promise<void>;
setNowPlayingInfo(options: { title: string; artist: string; album: string; duration: string; imageUrl: string; isLiveStream: boolean }): Promise<void>;
addListener(
Expand Down
6 changes: 6 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export class RemoteStreamerWeb extends WebPlugin implements RemoteStreamerPlugin
}
}

async setVolume(options: { volume: number }): Promise<void> {
if (this.audio) {
this.audio.volume = options.volume;
}
}

private setupEventListeners() {
if (this.audio) {
this.audio.onplaying = () => this.notifyListeners('play', {});
Expand Down

0 comments on commit 5191f87

Please sign in to comment.