Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to configure "SMTC integration" for Windows apps. #183

Merged
merged 1 commit into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 7 additions & 5 deletions RNSound/RNSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ -(NSDictionary *)constantsToExport {
category = AVAudioSessionCategoryRecord;
} else if ([categoryName isEqual: @"PlayAndRecord"]) {
category = AVAudioSessionCategoryPlayAndRecord;
}
}
#if TARGET_OS_IOS
else if ([categoryName isEqual: @"AudioProcessing"]) {
category = AVAudioSessionCategoryAudioProcessing;
Expand All @@ -112,25 +112,27 @@ -(NSDictionary *)constantsToExport {
[session setActive: enabled error: nil];
}

RCT_EXPORT_METHOD(prepare:(NSString*)fileName withKey:(nonnull NSNumber*)key
RCT_EXPORT_METHOD(prepare:(NSString*)fileName
withKey:(nonnull NSNumber*)key
withOptions:(NSDictionary*)options
withCallback:(RCTResponseSenderBlock)callback) {
NSError* error;
NSURL* fileNameUrl;
AVAudioPlayer* player;

if ([fileName hasPrefix:@"http"]) {
fileNameUrl = [NSURL URLWithString:[fileName stringByRemovingPercentEncoding]];
}
else {
fileNameUrl = [NSURL fileURLWithPath:[fileName stringByRemovingPercentEncoding]];
}

if (fileNameUrl) {
player = [[AVAudioPlayer alloc]
initWithData:[[NSData alloc] initWithContentsOfURL:fileNameUrl]
error:&error];
}

if (player) {
player.delegate = self;
player.enableRate = YES;
Expand Down
3 changes: 2 additions & 1 deletion android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;

import java.io.File;
Expand All @@ -35,7 +36,7 @@ public String getName() {
}

@ReactMethod
public void prepare(final String fileName, final Integer key, final Callback callback) {
public void prepare(final String fileName, final Integer key, final ReadableMap options, final Callback callback) {
MediaPlayer player = createMediaPlayer(fileName);
if (player == null) {
WritableMap e = Arguments.createMap();
Expand Down
4 changes: 2 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 @@ -31,7 +31,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, options || {}, (error, props) => {
if (props) {
if (typeof props.duration === 'number') {
this._duration = props.duration;
Expand Down
35 changes: 17 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,18 @@ 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, JObject options, ICallback callback)
{
bool enableSMTCIntegration = true;
JToken smtcOptionToken = options["enableSMTCIntegration"];
if (smtcOptionToken != null)
{
enableSMTCIntegration = smtcOptionToken.Value<bool>();
}

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 +95,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 +128,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