From cd52cbfd443bfd1f1d8d295c5b6827f14853cc3c Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 25 Dec 2023 03:57:24 +0800 Subject: [PATCH] Change to `react-native-player` --- components/models/ASR.tsx | 5 ++--- components/models/TTS.tsx | 5 ++--- ios/Podfile.lock | 9 ++++++++ package.json | 2 +- .../react-native-sound-player+0.13.2.patch | 21 ------------------- utils/audio.ts | 13 ++++++++++++ yarn.lock | 8 +++---- 7 files changed, 31 insertions(+), 32 deletions(-) delete mode 100644 patches/react-native-sound-player+0.13.2.patch diff --git a/components/models/ASR.tsx b/components/models/ASR.tsx index 47455a1..e005da0 100644 --- a/components/models/ASR.tsx +++ b/components/models/ASR.tsx @@ -1,6 +1,5 @@ import React, { useState, useEffect, useCallback, useRef } from 'react'; import { View, Text, PermissionsAndroid, Platform, Alert } from 'react-native'; -import SoundPlayer from 'react-native-sound-player'; import SelectField from '../form/SelectField'; import TextField from '../form/TextField'; import NumberField from '../form/NumberField'; @@ -10,7 +9,7 @@ import Recorder from '../../utils/recorder'; import InlineSection from '../form/InlineSection'; import Section from '../form/Section'; import { useColor } from '../../utils/style'; -import { decodeBuffer, toSingleChannel, downsample } from '../../utils/audio'; +import { decodeBuffer, toSingleChannel, downsample, play } from '../../utils/audio'; import { getFile } from '../../utils/fs-cache'; import * as logger from '../../utils/logger'; @@ -93,7 +92,7 @@ export function Interact({ settings: { model }, params, runPipe }: InteractProps const playExample = useCallback(async () => { if (!example) return; - SoundPlayer.playUrl(example); + play(example); }, [example]); return ( diff --git a/components/models/TTS.tsx b/components/models/TTS.tsx index ee402b2..70b8f63 100644 --- a/components/models/TTS.tsx +++ b/components/models/TTS.tsx @@ -1,13 +1,12 @@ import React, { useState, useEffect, useCallback, useRef } from 'react'; import { Text, Platform, Alert } from 'react-native'; -import SoundPlayer from 'react-native-sound-player'; import RNFS from 'react-native-fs'; import SelectField from '../form/SelectField'; import TextField from '../form/TextField'; import NumberField from '../form/NumberField'; import BooleanField from '../form/BooleanField'; import Button from '../form/Button'; -import { encodeBuffer } from '../../utils/audio'; +import { encodeBuffer, play } from '../../utils/audio'; export const title = 'Text To Speech'; @@ -75,7 +74,7 @@ export function Interact({ settings: { model }, params, runPipe }: InteractProps const play = useCallback(() => { if (!output) return; - SoundPlayer.playSoundFile(output.replace('.wav', ''), 'wav'); + play(output); }, [output]); return ( diff --git a/ios/Podfile.lock b/ios/Podfile.lock index f04a286..949c704 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -430,6 +430,11 @@ PODS: - React - RNFS (2.20.0): - React-Core + - RNSound (0.11.2): + - React-Core + - RNSound/Core (= 0.11.2) + - RNSound/Core (0.11.2): + - React-Core - RNSoundPlayer (0.13.2): - React-Core - SocketRocket (0.6.0) @@ -502,6 +507,7 @@ DEPENDENCIES: - ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`) - "RNAudioPcmStream (from `../node_modules/@fugood/react-native-audio-pcm-stream`)" - RNFS (from `../node_modules/react-native-fs`) + - RNSound (from `../node_modules/react-native-sound`) - RNSoundPlayer (from `../node_modules/react-native-sound-player`) - Yoga (from `../node_modules/react-native/ReactCommon/yoga`) @@ -603,6 +609,8 @@ EXTERNAL SOURCES: :path: "../node_modules/@fugood/react-native-audio-pcm-stream" RNFS: :path: "../node_modules/react-native-fs" + RNSound: + :path: "../node_modules/react-native-sound" RNSoundPlayer: :path: "../node_modules/react-native-sound-player" Yoga: @@ -662,6 +670,7 @@ SPEC CHECKSUMS: ReactCommon: 85c98ab0a509e70bf5ee5d9715cf68dbf495b84c RNAudioPcmStream: d7491fdfe6bddcebd6ab325df8327014be16743f RNFS: 4ac0f0ea233904cb798630b3c077808c06931688 + RNSound: 6c156f925295bdc83e8e422e7d8b38d33bc71852 RNSoundPlayer: 369105c565b8fe6ea0a43fc882dc81eba444e842 SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608 Yoga: 065f0b74dba4832d6e328238de46eb72c5de9556 diff --git a/package.json b/package.json index f926bed..8e5e966 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "react-native-image-picker": "^5.3.1", "react-native-quick-base64": "^2.0.6", "react-native-select-dropdown": "^3.3.3", - "react-native-sound-player": "^0.13.2", + "react-native-sound": "^0.11.2", "resize-image-data": "^0.3.1", "sha.js": "^2.4.11", "stream-browserify": "^3.0.0", diff --git a/patches/react-native-sound-player+0.13.2.patch b/patches/react-native-sound-player+0.13.2.patch deleted file mode 100644 index dff29c0..0000000 --- a/patches/react-native-sound-player+0.13.2.patch +++ /dev/null @@ -1,21 +0,0 @@ -diff --git a/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java b/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java -index 2a1561c..0f3ef18 100644 ---- a/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java -+++ b/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java -@@ -164,6 +164,8 @@ public class RNSoundPlayerModule extends ReactContextBaseJavaModule implements L - this.mediaPlayer = MediaPlayer.create(getCurrentActivity(), this.getUriFromFile(name, type)); - } - -+ if (this.mediaPlayer == null) return; -+ - this.mediaPlayer.setOnCompletionListener( - new OnCompletionListener() { - @Override -@@ -217,6 +219,7 @@ public class RNSoundPlayerModule extends ReactContextBaseJavaModule implements L - if (this.mediaPlayer == null) { - Uri uri = Uri.parse(url); - this.mediaPlayer = MediaPlayer.create(getCurrentActivity(), uri); -+ if (this.mediaPlayer == null) return; - this.mediaPlayer.setOnCompletionListener( - new OnCompletionListener() { - @Override diff --git a/utils/audio.ts b/utils/audio.ts index bdee8c3..5f14fa9 100644 --- a/utils/audio.ts +++ b/utils/audio.ts @@ -1,6 +1,7 @@ import wav from 'node-wav'; import AV from 'av'; import 'mp3'; +import Sound from 'react-native-sound'; import type { Buffer } from 'buffer'; const SCALING_FACTOR = Math.sqrt(2); @@ -11,6 +12,18 @@ export interface AudioData { channels: number; } +export const play = (uri: string): Promise => { + return new Promise((resolve, reject) => { + const sound = new Sound(uri, '', (err) => { + if (err) return reject(err); + sound.play(() => { + sound.release(); + resolve(); + }); + }); + }); +}; + export const encodeBuffer = (audio: AudioData): Buffer => { const { data, sampleRate, channels } = audio; const lenPerChannel = data.length / channels; diff --git a/yarn.lock b/yarn.lock index 9451556..64d2f04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6951,10 +6951,10 @@ react-native-select-dropdown@^3.3.3: resolved "https://registry.npmjs.org/react-native-select-dropdown/-/react-native-select-dropdown-3.4.0.tgz" integrity sha512-9xK1z4tYpwMxp3hsM0y1+xfqq/JoMh+l7sBsJ81b2eikFwT6ZbndFBqupQXrsQ1akce9hgC/10Nkzj0LjVjeXQ== -react-native-sound-player@^0.13.2: - version "0.13.2" - resolved "https://registry.npmjs.org/react-native-sound-player/-/react-native-sound-player-0.13.2.tgz" - integrity sha512-m457mjp496sARAED7epYzbhorfiLZ3j6HzJn7zEL9RU+ZJKcans/0gFNUEBvlh31fSDZRliQ2WwE4mJ6836bNg== +react-native-sound@^0.11.2: + version "0.11.2" + resolved "https://registry.yarnpkg.com/react-native-sound/-/react-native-sound-0.11.2.tgz#e542dc5b9e16ab4b3ac7e6eaddb1fc8d98da9038" + integrity sha512-LmGc8lgOK3qecYMVQpyHvww/C+wgT6sWeMpVbOe4NCRGC2yKd4fo4U0KBUo9PO7AqKESO3I/2GZg1/C0+bwiiA== react-native@0.71.8: version "0.71.8"