expo-speech-recognition 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # expo-speech-recognition
2
2
 
3
+ ## 3.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 7a64d6e: (iOS) Allow passing through folder URIs for audio persistence.
8
+
9
+ You can now do the following on Android and iOS:
10
+
11
+ ```ts
12
+ import { ExpoSpeechRecognitionModule } from "expo-speech-recognition";
13
+ import { Directory, Paths } from "expo-file-system";
14
+
15
+ const outputDirectory = new Directory(Paths.join(Paths.cache, "recordings"));
16
+
17
+ // Create the folder, if it doesn't exist yet (Android does not yet create intermediates)
18
+ outputDirectory.create({ intermediates: true, idempotent: true });
19
+
20
+ // Start speech recognition with a custom directory
21
+ ExpoSpeechRecognitionModule.start({
22
+ lang: "en-US",
23
+ recordingOptions: {
24
+ persist: true,
25
+ // e.g. "file:///data/user/0/com.yourapp.app/cache/recordings/"
26
+ outputDirectory: outputDirectory.uri,
27
+ },
28
+ });
29
+ ```
30
+
3
31
  ## 3.0.0
4
32
 
5
33
  ### Major Changes
@@ -236,16 +236,16 @@ public class ExpoSpeechRecognitionModule: Module {
236
236
  },
237
237
  audioStartHandler: { [weak self] filePath in
238
238
  if let filePath: String {
239
- // "file:///Users/..../Library/Caches/audio_CD5E6C6C-3D9D-4754-9188-D6FAF97D9DF2.wav"
240
- self?.sendEvent("audiostart", ["uri": "file://" + filePath])
239
+ let uri = filePath.hasPrefix("file://") ? filePath : "file://" + filePath
240
+ self?.sendEvent("audiostart", ["uri": uri])
241
241
  } else {
242
242
  self?.sendEvent("audiostart", ["uri": nil])
243
243
  }
244
244
  },
245
245
  audioEndHandler: { [weak self] filePath in
246
246
  if let filePath: String {
247
- // "file:///Users/..../Library/Caches/audio_CD5E6C6C-3D9D-4754-9188-D6FAF97D9DF2.wav"
248
- self?.sendEvent("audioend", ["uri": "file://" + filePath])
247
+ let uri = filePath.hasPrefix("file://") ? filePath : "file://" + filePath
248
+ self?.sendEvent("audioend", ["uri": uri])
249
249
  } else {
250
250
  self?.sendEvent("audioend", ["uri": nil])
251
251
  }
@@ -239,7 +239,7 @@ actor ExpoSpeechRecognizer: ObservableObject {
239
239
  startHandler()
240
240
 
241
241
  // If user has opted in to recording, emit an "audiostart" event with the path
242
- audioStartHandler(outputFileUrl?.path)
242
+ audioStartHandler(outputFileUrl?.absoluteString)
243
243
  } catch {
244
244
  errorHandler(error)
245
245
  reset(andEmitEnd: true)
@@ -399,9 +399,14 @@ actor ExpoSpeechRecognizer: ObservableObject {
399
399
  let baseDir: URL
400
400
 
401
401
  if let outputDirectory = outputDirectory {
402
- baseDir = URL(fileURLWithPath: outputDirectory, isDirectory: true)
402
+ guard let resolvedURL = Self.resolveOutputDirectoryURL(outputDirectory) else {
403
+ print("expo-speech-recognition: ERROR - Invalid outputDirectory: \(outputDirectory)")
404
+ return nil
405
+ }
406
+ baseDir = resolvedURL
403
407
  } else {
404
- guard let dirPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
408
+ guard
409
+ let dirPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
405
410
  else {
406
411
  print("Failed to get cache directory path.")
407
412
  return nil
@@ -409,10 +414,20 @@ actor ExpoSpeechRecognizer: ObservableObject {
409
414
  baseDir = dirPath
410
415
  }
411
416
 
417
+ do {
418
+ try FileManager.default.createDirectory(
419
+ at: baseDir,
420
+ withIntermediateDirectories: true
421
+ )
422
+ } catch {
423
+ print("expo-speech-recognition: ERROR - Failed to create output directory: \(error)")
424
+ return nil
425
+ }
426
+
412
427
  let fileName = outputFileName ?? "recording_\(UUID().uuidString).wav"
413
428
  let filePath = baseDir.appendingPathComponent(fileName)
414
429
 
415
- _ = ExtAudioFileCreateWithURL(
430
+ let status = ExtAudioFileCreateWithURL(
416
431
  filePath as CFURL,
417
432
  kAudioFileWAVEType,
418
433
  audioFormat.streamDescription,
@@ -421,6 +436,12 @@ actor ExpoSpeechRecognizer: ObservableObject {
421
436
  &audioFileRef
422
437
  )
423
438
 
439
+ guard status == noErr else {
440
+ print("expo-speech-recognition: ERROR - Failed to create audio file: \(status)")
441
+ audioFileRef = nil
442
+ return nil
443
+ }
444
+
424
445
  // Note: using `AVAudioFile()` doesn't seem to work
425
446
  // when downsampling pcmFloat32 to pcmInt16
426
447
 
@@ -432,13 +453,29 @@ actor ExpoSpeechRecognizer: ObservableObject {
432
453
  return filePath
433
454
  }
434
455
 
456
+ private static func resolveOutputDirectoryURL(_ directory: String) -> URL? {
457
+ if let url = URL(string: directory.trimmingCharacters(in: .whitespacesAndNewlines)),
458
+ let scheme = url.scheme,
459
+ !scheme.isEmpty
460
+ {
461
+ guard url.isFileURL else {
462
+ print("expo-speech-recognition: ERROR - Output directory must be a file URL.")
463
+ return nil
464
+ }
465
+ return url.hasDirectoryPath ? url : url.appendingPathComponent("")
466
+ }
467
+
468
+ let expandedDirectory = (directory as NSString).expandingTildeInPath
469
+ return URL(fileURLWithPath: expandedDirectory, isDirectory: true)
470
+ }
471
+
435
472
  private func handleSpeechStart() {
436
473
  speechStartHandler?()
437
474
  speechStartHandler = nil
438
475
  }
439
476
 
440
477
  private func end() {
441
- let filePath = self.outputFileUrl?.path
478
+ let filePath = self.outputFileUrl?.absoluteString
442
479
  outputFileUrl = nil
443
480
  Task {
444
481
  await MainActor.run {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-speech-recognition",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Speech Recognition for React Native Expo projects",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",