Skip to content

Commit

Permalink
Added the ability to configure "SMTC integration" for Windows apps.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndbroadbent committed May 16, 2017
1 parent 1a725fd commit 68d6af8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,17 @@ whoosh.release();
```

## API
### `constructor(filename, basePath, onError)`
### `constructor(filename, basePath, onError, options)`
`filename` {string} Either absolute or relative path to the sound file

`basePath` {?string} Optional base path of the file. Omit this or pass `''` if `filename` is an absolute path. Otherwise, you may use one of the predefined directories: `Sound.MAIN_BUNDLE`, `Sound.DOCUMENT`, `Sound.LIBRARY`, `Sound.CACHES`.

`onError` {?function(error, props)} Optional callback function. If the file is successfully loaded, the first parameter `error` is `null`, and `props` contains an object with two properties: `duration` (in seconds) and `numberOfChannels` (`1` for mono and `2` for stereo sound), both of which can also be accessed from the `Sound` instance object. If an initialization error is encountered (e.g. file not found), `error` will be an object containing `code`, `description`, and the stack trace.

`options` {?object} Platform-specific options:

**Windows Only:** `enableSMTCIntegration` {?boolean}. Optional setting for windows to enable or disable SMTC integration (controlling your apps sounds or music via the keyboard, and the built-in media controls on Windows.) This is enabled by default. Set this to false when you don't want users to be able to control your sounds (e.g. sound effects.) See the [Windows.Media.SystemMediaTransportControls documentation](https://docs.microsoft.com/en-us/uwp/api/Windows.Media.SystemMediaTransportControls) for more information.
### `isLoaded()`
Return `true` if the sound has been loaded.
Expand Down
12 changes: 10 additions & 2 deletions sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function isRelativePath(path) {
return !/^(\/|http(s?))/.test(path);
}

function Sound(filename, basePath, onError) {
function Sound(filename, basePath, onError, options) {
var asset = resolveAssetSource(filename);
if (asset) {
this._filename = asset.uri;
Expand All @@ -23,6 +23,14 @@ function Sound(filename, basePath, onError) {
}
}

// On Windows, all MediaPlayer instances are connected to the system controls
// by default. (e.g. pressing play/pause on the keyboard can control the sounds
// in your app.)
let enableSMTCIntegration = true
if (IsWindows && options != null && options.enableSMTCIntegration != null) {
enableSMTCIntegration = options.enableSMTCIntegration
}

this._loaded = false;
this._key = nextKey++;
this._duration = -1;
Expand All @@ -31,7 +39,7 @@ function Sound(filename, basePath, onError) {
this._pan = 0;
this._numberOfLoops = 0;
this._speed = 1;
RNSound.prepare(this._filename, this._key, (error, props) => {
RNSound.prepare(this._filename, this._key, enableSMTCIntegration, (error, props) => {
if (props) {
if (typeof props.duration === 'number') {
this._duration = props.duration;
Expand Down
28 changes: 10 additions & 18 deletions windows/RNSoundModule/RNSoundModule/RNSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace RNSoundModule
public class RNSound : ReactContextNativeModuleBase
{
private const String IsWindows = "IsWindows";

private ReactContext context;

Dictionary<int, MediaPlayer> playerPool = new Dictionary<int, MediaPlayer>();
Expand Down Expand Up @@ -65,11 +64,11 @@ public override IReadOnlyDictionary<string, object> Constants


[ReactMethod]
public async void prepare(String fileName, int key, ICallback callback)
public async void prepare(String fileName, int key, bool enableSMTCIntegration, ICallback callback)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
MediaPlayer player = await createMediaPlayer(fileName);
MediaPlayer player = await createMediaPlayer(fileName, enableSMTCIntegration);
player.MediaOpened +=
delegate
{
Expand All @@ -89,17 +88,14 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
}

this.playerPool.Add(key, player);


});




}
protected async Task<MediaPlayer> createMediaPlayer(String fileName)

protected async Task<MediaPlayer> createMediaPlayer(String fileName, bool enableSMTCIntegration)
{
MediaPlayer song = new MediaPlayer();
MediaPlayer player = new MediaPlayer();
player.CommandManager.IsEnabled = enableSMTCIntegration;

StorageFile file = null;
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
Expand All @@ -125,18 +121,14 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio

if (file != null)
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var stream = await file.OpenAsync(FileAccessMode.Read);

var mediaSource = MediaSource.CreateFromStorageFile(file);
song.Source = mediaSource;
player.Source = mediaSource;
}
}).AsTask();




return song;

return player;
}

[ReactMethod]
Expand Down

0 comments on commit 68d6af8

Please sign in to comment.