Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit a4509da

Browse files
MLKIT Barcode scanning no beep on scan #1174
1 parent 1999dda commit a4509da

File tree

17 files changed

+120
-39
lines changed

17 files changed

+120
-39
lines changed

demo-ng/app/App_Resources/iOS/build.xcconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ IPHONEOS_DEPLOYMENT_TARGET = 10.0
99

1010
// Don't check this in because of CI
1111
// DEVELOPMENT_TEAM = 8Q5F6M3TNS
12+
13+
DEVELOPMENT_TEAM = 8Q5F6M3TNS

demo-ng/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"nativescript-angular": "^6.1.0",
2626
"nativescript-camera": "~4.1.1",
2727
"nativescript-imagepicker": "~6.0.5",
28-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
28+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.1.0.tgz",
2929
"nativescript-theme-core": "~1.0.4",
3030
"reflect-metadata": "~0.1.10",
3131
"rxjs": "~6.0.0 || >=6.1.0",
@@ -43,4 +43,4 @@
4343
"nativescript-dev-webpack": "^0.15.1",
4444
"typescript": "~2.8.0"
4545
}
46-
}
46+
}

demo-push/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
},
1111
"dependencies": {
12-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
12+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.1.0.tgz",
1313
"nativescript-theme-core": "^1.0.4",
1414
"nativescript-unit-test-runner": "^0.3.4",
1515
"tns-core-modules": "~4.2.0"

demo-vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
},
1616
"dependencies": {
17-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
17+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.1.0.tgz",
1818
"nativescript-theme-core": "^1.0.4",
1919
"nativescript-vue": "^2.0.0",
2020
"tns-core-modules": "^5.0.2"

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"firebase-functions": "^2.0.5",
13-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
13+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.1.0.tgz",
1414
"nativescript-theme-core": "^1.0.4",
1515
"nativescript-unit-test-runner": "^0.3.4",
1616
"tns-core-modules": "~5.2.0"

src/mlkit/barcodescanning/index.android.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,60 @@ import { ImageSource } from "tns-core-modules/image-source";
22
import { MLKitScanBarcodesOnDeviceOptions, MLKitScanBarcodesOnDeviceResult, MLKitScanBarcodesResultBounds } from "./";
33
import { MLKitVisionOptions } from "../index";
44
import { BarcodeFormat, MLKitBarcodeScanner as MLKitBarcodeScannerBase } from "./barcodescanning-common";
5+
import * as application from "tns-core-modules/application";
56

67
export { BarcodeFormat };
78

89
const gmsTasks = (<any>com.google.android.gms).tasks;
910

1011
export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase {
1112

13+
private player: android.media.MediaPlayer;
14+
15+
disposeNativeView(): void {
16+
super.disposeNativeView();
17+
if (this.player) {
18+
this.player.release();
19+
this.player = undefined;
20+
}
21+
}
22+
1223
protected createDetector(): any {
1324
let formats: Array<BarcodeFormat>;
1425
if (this.formats) {
1526
formats = [];
1627
const requestedFormats = this.formats.split(",");
1728
requestedFormats.forEach(format => formats.push(BarcodeFormat[format.trim().toUpperCase()]))
1829
}
30+
31+
if (this.beepOnScan) {
32+
const activity = (application.android.foregroundActivity || application.android.startActivity);
33+
activity.setVolumeControlStream(android.media.AudioManager.STREAM_MUSIC);
34+
try {
35+
const file = application.android.context.getResources().getIdentifier("beep", "raw", application.android.context.getPackageName());
36+
if (file === 0) {
37+
console.log("No 'beep.*' soundfile found in the resources /raw folder. There will be no audible feedback upon scanning a barcode.");
38+
} else {
39+
this.player = new android.media.MediaPlayer();
40+
const fileDescriptor: android.content.res.AssetFileDescriptor = application.android.context.getResources().openRawResourceFd(file);
41+
try {
42+
this.player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
43+
} finally {
44+
fileDescriptor.close();
45+
}
46+
// this.mediaPlayer.setOnErrorListener(this);
47+
this.player.setAudioStreamType(android.media.AudioManager.STREAM_MUSIC);
48+
this.player.setLooping(false);
49+
this.player.setVolume(0.10, 0.10);
50+
this.player.prepare();
51+
}
52+
} catch (e) {
53+
console.log(e);
54+
this.player.release();
55+
this.player = undefined;
56+
}
57+
}
58+
1959
return getBarcodeDetector(formats);
2060
}
2161

@@ -27,7 +67,7 @@ export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase {
2767
barcodes: []
2868
};
2969

30-
if (barcodes) {
70+
if (barcodes && barcodes.size() > 0) {
3171
// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
3272
for (let i = 0; i < barcodes.size(); i++) {
3373
const barcode = barcodes.get(i);
@@ -38,6 +78,10 @@ export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase {
3878
bounds: boundingBoxToBounds(barcode.getBoundingBox())
3979
});
4080
}
81+
82+
if (this.player) {
83+
this.player.start();
84+
}
4185
}
4286

4387
this.notify({

src/mlkit/barcodescanning/index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ export interface MLKitScanBarcodesOnDeviceOptions extends MLKitVisionOptions {
3333
* If not set, we'll detect all supported formats.
3434
*/
3535
formats?: Array<BarcodeFormat>;
36+
37+
/**
38+
* Play a sound when a code was scanned.
39+
* Default: true
40+
*/
41+
beepOnScan?: boolean;
42+
43+
/**
44+
* Wheter or not to report duplicate scan results during continuous scanning.
45+
* Default false.
46+
*/
47+
reportDuplicates?: boolean;
3648
}
3749

3850
export declare function scanBarcodesOnDevice(options: MLKitScanBarcodesOnDeviceOptions): Promise<MLKitScanBarcodesOnDeviceResult>;

src/mlkit/barcodescanning/index.ios.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@ export { BarcodeFormat };
77

88
export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase {
99

10+
private player: AVAudioPlayer;
11+
1012
protected createDetector(): any {
1113
let formats: Array<BarcodeFormat>;
1214
if (this.formats) {
1315
formats = [];
1416
const requestedFormats = this.formats.split(",");
1517
requestedFormats.forEach(format => formats.push(BarcodeFormat[format.trim().toUpperCase()]))
1618
}
19+
20+
if (this.beepOnScan) {
21+
// play nice with others when playing sound
22+
AVAudioSession.sharedInstance().setCategoryModeOptionsError(AVAudioSessionCategoryPlayback, AVAudioSessionModeDefault, AVAudioSessionCategoryOptions.MixWithOthers)
23+
24+
// prepare an audio player, with a sound file bundled in our custom fwk
25+
const barcodeBundlePath = NSBundle.bundleWithIdentifier("org.nativescript.plugin.firebase.MLKit").bundlePath;
26+
this.player = new AVAudioPlayer({contentsOfURL: NSURL.fileURLWithPath(barcodeBundlePath + "/beep.caf")});
27+
this.player.numberOfLoops = 1;
28+
this.player.volume = 0.7; // this is not the actual volume, as that really depends on the device volume
29+
this.player.prepareToPlay();
30+
}
31+
1732
return getBarcodeDetector(formats);
1833
}
1934

@@ -42,6 +57,10 @@ export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase {
4257
object: this,
4358
value: result
4459
});
60+
61+
if (barcodes.count > 0 && this.player) {
62+
this.player.play();
63+
}
4564
}
4665
}
4766
}

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-plugin-firebase",
3-
"version": "8.0.1",
3+
"version": "8.1.0",
44
"description": "Fire. Base. Firebase!",
55
"main": "firebase",
66
"typings": "index.d.ts",
6.25 KB
Binary file not shown.

0 commit comments

Comments
 (0)