react-native-pcm-player-lite 0.1.2 → 0.1.3

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.
@@ -1,5 +1,6 @@
1
1
  import Foundation
2
2
  import AVFoundation
3
+ import React // <- This imports RCTPromiseResolveBlock / RCTPromiseRejectBlock
3
4
 
4
5
  @objc(PcmPlayer)
5
6
  class PcmPlayer: NSObject {
@@ -13,14 +14,20 @@ class PcmPlayer: NSObject {
13
14
  func start(sampleRate: NSNumber,
14
15
  resolve: RCTPromiseResolveBlock,
15
16
  reject: RCTPromiseRejectBlock) {
16
- if isStarted { resolve(nil); return }
17
+ if isStarted {
18
+ resolve(NSNull()) // avoid 'nil requires a contextual type'
19
+ return
20
+ }
17
21
  let sr = sampleRate.doubleValue
22
+
18
23
  do {
19
24
  try AVAudioSession.sharedInstance().setCategory(.playback, options: [.duckOthers])
20
25
  try AVAudioSession.sharedInstance().setActive(true)
21
- } catch {}
26
+ } catch {
27
+ // don't fail hard on session issues; TTS still likely works
28
+ }
22
29
 
23
- // Use float32 format and convert from Int16 manually.
30
+ // Use float32 format; we’ll feed converted int16 -> float32
24
31
  fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: sr, channels: 1, interleaved: true)
25
32
 
26
33
  engine.attach(player)
@@ -29,7 +36,7 @@ class PcmPlayer: NSObject {
29
36
  try engine.start()
30
37
  player.play()
31
38
  isStarted = true
32
- resolve(nil)
39
+ resolve(NSNull())
33
40
  } catch {
34
41
  reject("ERR_START", "Failed to start audio engine", error)
35
42
  }
@@ -39,6 +46,8 @@ class PcmPlayer: NSObject {
39
46
  func enqueueBase64(_ b64: NSString) {
40
47
  guard isStarted, let fmt = fmt else { return }
41
48
  guard let data = Data(base64Encoded: b64 as String) else { return }
49
+
50
+ // Convert Int16 LE -> Float32
42
51
  let count = data.count / 2
43
52
  var floats = [Float](repeating: 0, count: count)
44
53
  data.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) in
@@ -47,6 +56,7 @@ class PcmPlayer: NSObject {
47
56
  floats[i] = Float(int16s[i]) / 32768.0
48
57
  }
49
58
  }
59
+
50
60
  let frameCount = AVAudioFrameCount(count)
51
61
  guard let buffer = AVAudioPCMBuffer(pcmFormat: fmt, frameCapacity: frameCount) else { return }
52
62
  buffer.frameLength = frameCount
@@ -54,16 +64,20 @@ class PcmPlayer: NSObject {
54
64
  floats.withUnsafeBufferPointer { src in
55
65
  ch.assign(from: src.baseAddress!, count: count)
56
66
  }
67
+
57
68
  player.scheduleBuffer(buffer, completionHandler: nil)
58
69
  }
59
70
 
60
71
  @objc(stop:withRejecter:)
61
72
  func stop(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
62
- if !isStarted { resolve(nil); return }
73
+ if !isStarted {
74
+ resolve(NSNull())
75
+ return
76
+ }
63
77
  player.stop()
64
78
  engine.stop()
65
79
  isStarted = false
66
- resolve(nil)
80
+ resolve(NSNull())
67
81
  }
68
82
 
69
83
  @objc static func requiresMainQueueSetup() -> Bool { false }
@@ -1,7 +1,10 @@
1
1
  #import <React/RCTBridgeModule.h>
2
2
 
3
3
  @interface RCT_EXTERN_MODULE(PcmPlayer, NSObject)
4
- RCT_EXTERN_METHOD(start:(nonnull NSNumber *)sampleRate withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject)
4
+ RCT_EXTERN_METHOD(start:(nonnull NSNumber *)sampleRate
5
+ withResolver:(RCTPromiseResolveBlock)resolve
6
+ withRejecter:(RCTPromiseRejectBlock)reject)
5
7
  RCT_EXTERN_METHOD(enqueueBase64:(NSString *)b64)
6
- RCT_EXTERN_METHOD(stop:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject)
8
+ RCT_EXTERN_METHOD(stop:(RCTPromiseResolveBlock)resolve
9
+ withRejecter:(RCTPromiseRejectBlock)reject)
7
10
  @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-pcm-player-lite",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Ultra-small native module to play streaming 16-bit PCM (mono) in React Native (Android + iOS).",
5
5
  "license": "MIT",
6
6
  "author": "Anas Syed <me@anasyd.com>",
@@ -12,6 +12,7 @@ Pod::Spec.new do |s|
12
12
  s.platforms = { :ios => "12.0" }
13
13
  s.source_files = "ios/**/*.{swift,m}"
14
14
  s.requires_arc = true
15
-
15
+ s.swift_version = '5.0'
16
16
  s.dependency "React-Core"
17
+
17
18
  end