在调试中工作但在发布中不工作 |世博会 |反应本机单模块
Works in debug but not in release | expo-av | react-native-unimodules
环境
Expo CLI 3.11.1 环境信息:
系统:
OS: Windows 10
二进制文件:
纱线:1.22.4 - C:\Users\user\AppData\Roaming\npm\yarn.CMD
npm: 6.12.0 - C:\Program Files\nodejs\npm.CMD
集成开发环境:
Android 工作室:版本 3.6.0.0 AI-192.7142.36.36.6308749
React-native android 在设备上
重现步骤:
1.
npx react-native init ReactRelease --version 0.62.2
npm install react-native-unimodules@0.9.0 --save
expo install expo-av
2.
根据每个库中的文档配置文件
3.
编辑App.js
import React, { Component } from 'react';
import { Text, StyleSheet, View, Button, Alert } from 'react-native';
import { Audio } from 'expo-av';
import * as Permissions from 'expo-permissions';
var audioObjectActions = new Audio.Sound();
export default class App extends Component {
constructor(props) {
super(props);
this.state = {};
}
async componentDidMount() {
await Permissions.askAsync(Permissions.AUDIO_RECORDING);
await Audio.setAudioModeAsync({
staysActiveInBackground: true,
allowsRecordingIOS: false,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DUCK_OTHERS,
playsInSilentModeIOS: true,
playThroughEarpieceAndroid: false,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
shouldDuckAndroid: true
});
}
async _playRandomAction() {
try {
audioObjectActions.setOnPlaybackStatusUpdate(this._onPlaybackStatusUpdateAction);
await audioObjectActions.loadAsync(require('./assets/song_sound.m4a'));
await audioObjectActions.playAsync();
} catch (error) {
Alert.alert('ERROR : ', '' + JSON.stringify(error));
}
}
_onPlaybackStatusUpdateAction = async (playbackStatus) => {
if (!playbackStatus.isLoaded) {
// Update your UI for the unloaded state
if (playbackStatus.error) {
console.log(`Encountered a fatal error during playback: ${playbackStatus.error}`);
// Send Expo team the error on Slack or the forums so we can help you debug!
}
} else {
if (playbackStatus.didJustFinish && !playbackStatus.isLooping) {
await audioObjectActions.stopAsync();
await audioObjectActions.unloadAsync();
}
}
};
render() {
return (
<View style={styles.container}>
<Text> Test Audio </Text>
<Button title="On joue le morceau" onPress={async () => this._playRandomAction()} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#555555'
}
});
4.
创建资产文件夹并添加 song_sound.m4a,如下所示:
5
通过 USB 连接您的 android 设备
在调试中测试:
//POWERSHELL 1
npx react-native run-android
//POWERSHELL 2
npm start
行为
完美运行:声音已加载并播放
在发布
中测试
cd android
./gradlew assembleRelease
./gradlew installRelease
行为
声音未加载,显示的错误是:
{
"nativeStackAndroid":[],
"userInfo":null,
"message":"com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: assets_song_sound: open fialed: ENOENT (No such file or directory)",
"code":"E_LOAD_ERROR",
"line": 18,
"column": 1111,
"sourceURL":"index.android.bundle"
}
我想要什么?
只需像在调试中一样在发布时加载和播放音频。
终于找到答案了
工作原理
require()
在发行版中效果不佳。
我不知道为什么 require() 在与 Image 一起发布时效果很好,但与 Audio
所以你需要使用
{ uri: 'asset:/my_sound.m4a' }
并将您的声音文件(如 my_sound.m4a)放入 android/app/src/main/assets:
它会起作用。
环境
Expo CLI 3.11.1 环境信息: 系统: OS: Windows 10 二进制文件: 纱线:1.22.4 - C:\Users\user\AppData\Roaming\npm\yarn.CMD npm: 6.12.0 - C:\Program Files\nodejs\npm.CMD 集成开发环境: Android 工作室:版本 3.6.0.0 AI-192.7142.36.36.6308749
React-native android 在设备上
重现步骤:
1.
npx react-native init ReactRelease --version 0.62.2
npm install react-native-unimodules@0.9.0 --save
expo install expo-av
2.
根据每个库中的文档配置文件
3.
编辑App.js
import React, { Component } from 'react';
import { Text, StyleSheet, View, Button, Alert } from 'react-native';
import { Audio } from 'expo-av';
import * as Permissions from 'expo-permissions';
var audioObjectActions = new Audio.Sound();
export default class App extends Component {
constructor(props) {
super(props);
this.state = {};
}
async componentDidMount() {
await Permissions.askAsync(Permissions.AUDIO_RECORDING);
await Audio.setAudioModeAsync({
staysActiveInBackground: true,
allowsRecordingIOS: false,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DUCK_OTHERS,
playsInSilentModeIOS: true,
playThroughEarpieceAndroid: false,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
shouldDuckAndroid: true
});
}
async _playRandomAction() {
try {
audioObjectActions.setOnPlaybackStatusUpdate(this._onPlaybackStatusUpdateAction);
await audioObjectActions.loadAsync(require('./assets/song_sound.m4a'));
await audioObjectActions.playAsync();
} catch (error) {
Alert.alert('ERROR : ', '' + JSON.stringify(error));
}
}
_onPlaybackStatusUpdateAction = async (playbackStatus) => {
if (!playbackStatus.isLoaded) {
// Update your UI for the unloaded state
if (playbackStatus.error) {
console.log(`Encountered a fatal error during playback: ${playbackStatus.error}`);
// Send Expo team the error on Slack or the forums so we can help you debug!
}
} else {
if (playbackStatus.didJustFinish && !playbackStatus.isLooping) {
await audioObjectActions.stopAsync();
await audioObjectActions.unloadAsync();
}
}
};
render() {
return (
<View style={styles.container}>
<Text> Test Audio </Text>
<Button title="On joue le morceau" onPress={async () => this._playRandomAction()} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#555555'
}
});
4.
创建资产文件夹并添加 song_sound.m4a,如下所示:
5
通过 USB 连接您的 android 设备
在调试中测试:
//POWERSHELL 1
npx react-native run-android
//POWERSHELL 2
npm start
行为
完美运行:声音已加载并播放
在发布
中测试cd android
./gradlew assembleRelease
./gradlew installRelease
行为
声音未加载,显示的错误是:
{
"nativeStackAndroid":[],
"userInfo":null,
"message":"com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: assets_song_sound: open fialed: ENOENT (No such file or directory)",
"code":"E_LOAD_ERROR",
"line": 18,
"column": 1111,
"sourceURL":"index.android.bundle"
}
我想要什么?
只需像在调试中一样在发布时加载和播放音频。
终于找到答案了
工作原理
require()
在发行版中效果不佳。
我不知道为什么 require() 在与 Image 一起发布时效果很好,但与 Audio
所以你需要使用
{ uri: 'asset:/my_sound.m4a' }
并将您的声音文件(如 my_sound.m4a)放入 android/app/src/main/assets:
它会起作用。