Skip to content

Commit 0598aa6

Browse files
committed
bump audiomixing feature
1 parent 233a6c0 commit 0598aa6

13 files changed

+205
-102
lines changed

Diff for: .prettierrc

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"singleQuote": true,
33
"semi": true,
4-
"printWidth": 120,
54
"trailingComma": "none",
65
"arrowParens": "avoid"
76
}

Diff for: README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
## Installation
2121

22-
NativeScript 7+:
23-
`tns plugin add nativescript-audio`
22+
#### NativeScript 7+:
23+
24+
`ns plugin add nativescript-audio`
25+
26+
#### NativeScript Version prior to 7:
2427

25-
NativeScript Version prior to 7:
2628
`tns plugin add [email protected]`
2729

2830
---
@@ -136,10 +138,10 @@ const playerOptions = {
136138

137139
player
138140
.playFromUrl(playerOptions)
139-
.then(function (res) {
141+
.then(res => {
140142
console.log(res);
141143
})
142-
.catch(function (err) {
144+
.catch(err => {
143145
console.log('something went wrong...', err);
144146
});
145147
```

Diff for: demo/app/main-view-model.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
Slider,
1010
Utils
1111
} from '@nativescript/core';
12-
import { AudioPlayerOptions, AudioRecorderOptions, TNSPlayer, TNSRecorder } from 'nativescript-audio';
12+
import {
13+
AudioPlayerOptions,
14+
AudioRecorderOptions,
15+
TNSPlayer,
16+
TNSRecorder
17+
} from 'nativescript-audio';
1318

1419
export class AudioDemo extends Observable {
1520
@ObservableProperty() public isPlaying: boolean;
@@ -80,7 +85,9 @@ export class AudioDemo extends Observable {
8085
androidEncoder = 3;
8186
}
8287

83-
const recordingPath = `${audioFolder.path}/recording.${this.platformExtension()}`;
88+
const recordingPath = `${
89+
audioFolder.path
90+
}/recording.${this.platformExtension()}`;
8491

8592
const recorderOptions: AudioRecorderOptions = {
8693
filename: recordingPath,
@@ -144,7 +151,9 @@ export class AudioDemo extends Observable {
144151
public getFile(args) {
145152
try {
146153
const audioFolder = knownFolders.currentApp().getFolder('audio');
147-
const recordedFile = audioFolder.getFile(`recording.${this.platformExtension()}`);
154+
const recordedFile = audioFolder.getFile(
155+
`recording.${this.platformExtension()}`
156+
);
148157
console.log(JSON.stringify(recordedFile));
149158
console.log('recording exists: ' + File.exists(recordedFile.path));
150159
this.recordedAudioFile = recordedFile.path;
@@ -155,7 +164,9 @@ export class AudioDemo extends Observable {
155164

156165
public async playRecordedFile(args) {
157166
const audioFolder = knownFolders.currentApp().getFolder('audio');
158-
const recordedFile = audioFolder.getFile(`recording.${this.platformExtension()}`);
167+
const recordedFile = audioFolder.getFile(
168+
`recording.${this.platformExtension()}`
169+
);
159170
console.log('RECORDED FILE : ' + JSON.stringify(recordedFile));
160171

161172
const playerOptions: AudioPlayerOptions = {

Diff for: demo/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"@nativescript/core": "~7.0.0"
66
},
77
"devDependencies": {
8-
"@nativescript/android": "7.0.0",
9-
"@nativescript/ios": "7.0.0",
10-
"@nativescript/types": "7.0.0",
8+
"@nativescript/android": "~7.0.0",
9+
"@nativescript/ios": "~7.0.0",
10+
"@nativescript/types": "~7.0.0",
1111
"@nativescript/webpack": "~3.0.0",
12-
"typescript": "^3.9.3"
12+
"typescript": "~3.9.7"
1313
},
1414
"scripts": {
1515
"build.plugin": "cd ../src && npm run build",

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scripts": {
3-
"ci.base.setup": "npm i -g [email protected].2 && echo no | npm i -g nativescript && tns usage-reporting disable && tns error-reporting disable && cd src && npm i && tsc",
3+
"ci.base.setup": "npm i -g [email protected].7 && echo no | npm i -g nativescript && tns usage-reporting disable && tns error-reporting disable && cd src && npm i && tsc",
44
"ci.pip.install": "sudo pip install --upgrade pip && sudo pip install six",
55
"ci.tslint": "cd src && npm run ci.tslint && cd ../demo && npm run ci.tslint",
66
"ci.vanilla.android.build": "cd demo && tns build android --env.uglify",

Diff for: src/android/player.ts

+70-46
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import { AudioPlayerEvents, AudioPlayerOptions } from '../options';
44

55
export enum AudioFocusDurationHint {
66
AUDIOFOCUS_GAIN = android.media.AudioManager.AUDIOFOCUS_GAIN,
7-
AUDIOFOCUS_GAIN_TRANSIENT = android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
8-
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
9-
AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
7+
AUDIOFOCUS_GAIN_TRANSIENT = android.media.AudioManager
8+
.AUDIOFOCUS_GAIN_TRANSIENT,
9+
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = android.media.AudioManager
10+
.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
11+
AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = android.media.AudioManager
12+
.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
1013
}
1114

1215
export class TNSPlayer implements TNSPlayerI {
@@ -17,7 +20,9 @@ export class TNSPlayer implements TNSPlayerI {
1720
private _durationHint: AudioFocusDurationHint;
1821
private _options: AudioPlayerOptions;
1922

20-
constructor(durationHint: AudioFocusDurationHint = AudioFocusDurationHint.AUDIOFOCUS_GAIN) {
23+
constructor(
24+
durationHint: AudioFocusDurationHint = AudioFocusDurationHint.AUDIOFOCUS_GAIN
25+
) {
2126
this._durationHint = durationHint;
2227
}
2328

@@ -76,17 +81,15 @@ export class TNSPlayer implements TNSPlayerI {
7681
options.autoPlay = true;
7782
}
7883

79-
if (!this._player) {
80-
this._player = new android.media.MediaPlayer();
81-
}
82-
8384
// request audio focus, this will setup the onAudioFocusChangeListener
8485
if (!options.audioMixing) {
8586
this._mAudioFocusGranted = this._requestAudioFocus();
8687
}
8788

8889
const audioPath = resolveAudioFilePath(options.audioFile);
89-
this._player.setAudioStreamType(android.media.AudioManager.STREAM_MUSIC);
90+
this._player.setAudioStreamType(
91+
android.media.AudioManager.STREAM_MUSIC
92+
);
9093
this._player.reset();
9194
this._player.setDataSource(audioPath);
9295

@@ -175,12 +178,17 @@ export class TNSPlayer implements TNSPlayerI {
175178
this._sendEvent(AudioPlayerEvents.started);
176179
// set volume controls
177180
// https://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)
178-
Application.android.foregroundActivity.setVolumeControlStream(android.media.AudioManager.STREAM_MUSIC);
181+
Application.android.foregroundActivity.setVolumeControlStream(
182+
android.media.AudioManager.STREAM_MUSIC
183+
);
179184

180185
// register the receiver so when calls or another app takes main audio focus the player pauses
181186
Application.android.registerBroadcastReceiver(
182187
android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY,
183-
(context: android.content.Context, intent: android.content.Intent) => {
188+
(
189+
context: android.content.Context,
190+
intent: android.content.Intent
191+
) => {
184192
this.pause();
185193
}
186194
);
@@ -221,13 +229,19 @@ export class TNSPlayer implements TNSPlayerI {
221229
// this checks on API 23 and up
222230
if (android.os.Build.VERSION.SDK_INT >= 23 && this.play) {
223231
if (this._player?.isPlaying()) {
224-
(this._player as any).setPlaybackParams((this._player as any).getPlaybackParams().setSpeed(speed));
232+
(this._player as any).setPlaybackParams(
233+
(this._player as any).getPlaybackParams().setSpeed(speed)
234+
);
225235
} else {
226-
(this._player as any).setPlaybackParams((this._player as any).getPlaybackParams().setSpeed(speed));
236+
(this._player as any).setPlaybackParams(
237+
(this._player as any).getPlaybackParams().setSpeed(speed)
238+
);
227239
this._player?.pause();
228240
}
229241
} else {
230-
console.warn('Android device API is not 23+. Cannot set the playbackRate on lower Android APIs.');
242+
console.warn(
243+
'Android device API is not 23+. Cannot set the playbackRate on lower Android APIs.'
244+
);
231245
}
232246
}
233247

@@ -241,7 +255,9 @@ export class TNSPlayer implements TNSPlayerI {
241255
// (Refer to: https://developer.android.com/reference/android/media/MediaPlayer#state-diagram)
242256
this._options = undefined;
243257
// unregister broadcast receiver
244-
Application.android.unregisterBroadcastReceiver(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY);
258+
Application.android.unregisterBroadcastReceiver(
259+
android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY
260+
);
245261

246262
this._abandonAudioFocus();
247263
}
@@ -293,15 +309,19 @@ export class TNSPlayer implements TNSPlayerI {
293309
let result = true;
294310
if (!this._mAudioFocusGranted) {
295311
const ctx = this._getAndroidContext();
296-
const am = ctx.getSystemService(android.content.Context.AUDIO_SERVICE) as android.media.AudioManager;
312+
const am = ctx.getSystemService(
313+
android.content.Context.AUDIO_SERVICE
314+
) as android.media.AudioManager;
297315
// Request audio focus for play back
298316
const focusResult = am.requestAudioFocus(
299317
this._mOnAudioFocusChangeListener,
300318
android.media.AudioManager.STREAM_MUSIC,
301319
this._durationHint
302320
);
303321

304-
if (focusResult === android.media.AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
322+
if (
323+
focusResult === android.media.AudioManager.AUDIOFOCUS_REQUEST_GRANTED
324+
) {
305325
result = true;
306326
} else {
307327
result = false;
@@ -388,36 +408,40 @@ export class TNSPlayer implements TNSPlayerI {
388408
return this._mediaPlayer;
389409
}
390410

391-
private _mOnAudioFocusChangeListener = new android.media.AudioManager.OnAudioFocusChangeListener({
392-
onAudioFocusChange: (focusChange: number) => {
393-
switch (focusChange) {
394-
case android.media.AudioManager.AUDIOFOCUS_GAIN:
395-
// Set volume level to desired levels
396-
// if last volume more than 10 just set to 1.0 float
397-
if (this._lastPlayerVolume && this._lastPlayerVolume >= 10) {
398-
this.volume = 1.0;
399-
} else if (this._lastPlayerVolume) {
400-
this.volume = parseFloat('0.' + this._lastPlayerVolume.toString());
401-
}
411+
private _mOnAudioFocusChangeListener = new android.media.AudioManager.OnAudioFocusChangeListener(
412+
{
413+
onAudioFocusChange: (focusChange: number) => {
414+
switch (focusChange) {
415+
case android.media.AudioManager.AUDIOFOCUS_GAIN:
416+
// Set volume level to desired levels
417+
// if last volume more than 10 just set to 1.0 float
418+
if (this._lastPlayerVolume && this._lastPlayerVolume >= 10) {
419+
this.volume = 1.0;
420+
} else if (this._lastPlayerVolume) {
421+
this.volume = parseFloat(
422+
'0.' + this._lastPlayerVolume.toString()
423+
);
424+
}
402425

403-
this.resume();
404-
break;
405-
case android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
406-
// You have audio focus for a short time
407-
break;
408-
case android.media.AudioManager.AUDIOFOCUS_LOSS:
409-
this.pause();
410-
break;
411-
case android.media.AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
412-
// Temporary loss of audio focus - expect to get it back - you can keep your resources around
413-
this.pause();
414-
break;
415-
case android.media.AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
416-
// Lower the volume, keep playing
417-
this._lastPlayerVolume = this.volume;
418-
this.volume = 0.2;
419-
break;
426+
this.resume();
427+
break;
428+
case android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
429+
// You have audio focus for a short time
430+
break;
431+
case android.media.AudioManager.AUDIOFOCUS_LOSS:
432+
this.pause();
433+
break;
434+
case android.media.AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
435+
// Temporary loss of audio focus - expect to get it back - you can keep your resources around
436+
this.pause();
437+
break;
438+
case android.media.AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
439+
// Lower the volume, keep playing
440+
this._lastPlayerVolume = this.volume;
441+
this.volume = 0.2;
442+
break;
443+
}
420444
}
421445
}
422-
});
446+
);
423447
}

Diff for: src/android/recorder.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export class TNSRecorder implements TNSRecordI {
1212

1313
public static CAN_RECORD(): boolean {
1414
const pManager = Application.android.context.getPackageManager();
15-
const canRecord = pManager.hasSystemFeature(android.content.pm.PackageManager.FEATURE_MICROPHONE);
15+
const canRecord = pManager.hasSystemFeature(
16+
android.content.pm.PackageManager.FEATURE_MICROPHONE
17+
);
1618
if (canRecord) {
1719
return true;
1820
} else {
@@ -23,7 +25,9 @@ export class TNSRecorder implements TNSRecordI {
2325
public requestRecordPermission(explanation = '') {
2426
return new Promise(async (resolve, reject) => {
2527
try {
26-
await requestPermission((android as any).Manifest.permission.RECORD_AUDIO).catch(err => {
28+
await requestPermission(
29+
(android as any).Manifest.permission.RECORD_AUDIO
30+
).catch(err => {
2731
reject(err);
2832
});
2933
resolve();
@@ -34,7 +38,9 @@ export class TNSRecorder implements TNSRecordI {
3438
}
3539

3640
public hasRecordPermission() {
37-
const permission = hasPermission((android as any).Manifest.permission.RECORD_AUDIO);
41+
const permission = hasPermission(
42+
(android as any).Manifest.permission.RECORD_AUDIO
43+
);
3844
return !0 === permission ? !0 : !1;
3945
}
4046

Diff for: src/common.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ export function resolveAudioFilePath(path: string) {
120120
let audioPath;
121121
let fileName = Utils.isString(path) ? path.trim() : '';
122122
if (fileName.indexOf('~/') === 0) {
123-
fileName = nsFilePath.join(knownFolders.currentApp().path, fileName.replace('~/', ''));
123+
fileName = nsFilePath.join(
124+
knownFolders.currentApp().path,
125+
fileName.replace('~/', '')
126+
);
124127
audioPath = fileName;
125128
} else {
126129
audioPath = fileName;

Diff for: src/index.d.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,24 @@ export enum AudioFocusDurationHint {
354354
* playback to go back to where it was once your application no
355355
* longer requires audio focus.
356356
*/
357-
AUDIOFOCUS_GAIN_TRANSIENT = android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
357+
AUDIOFOCUS_GAIN_TRANSIENT = android.media.AudioManager
358+
.AUDIOFOCUS_GAIN_TRANSIENT,
358359
/**
359360
* This focus request type is similar to AUDIOFOCUS_GAIN_TRANSIENT
360361
* for the temporary aspect of the focus request, but it also
361362
* expresses the fact during the time you own focus, you allow
362363
* another application to keep playing at a reduced volume,
363364
* “ducked”.
364365
*/
365-
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
366+
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = android.media.AudioManager
367+
.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
366368
/**
367369
* Also for a temporary request, but also expresses that your
368370
* application expects the device to not play anything else. This
369371
* is typically used if you are doing audio recording or speech
370372
* recognition, and don’t want for examples notifications to be
371373
* played by the system during that time.
372374
*/
373-
AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
375+
AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = android.media.AudioManager
376+
.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
374377
}

0 commit comments

Comments
 (0)