Works in debug but not in release | expo-av | react-native-unimodules

百般思念 提交于 2021-02-11 17:15:41

问题


Environment

Expo CLI 3.11.1 environment info: System: OS: Windows 10 Binaries: Yarn: 1.22.4 - C:\Users\user\AppData\Roaming\npm\yarn.CMD npm: 6.12.0 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: Version 3.6.0.0 AI-192.7142.36.36.6308749

React-native android on device

Step to repro :

1.

npx react-native init ReactRelease --version 0.62.2
npm install react-native-unimodules@0.9.0 --save 
expo install expo-av

2.

Configure your files according to the doc in each library

3.

Edit 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.

Create assets folder and add song_sound.m4a like this : bug

5

Connect your android device via USB

Test in debug :

//POWERSHELL 1
npx react-native run-android   

//POWERSHELL 2
npm start 

Behavior

Works perfectly : the sound is loaded and is played

Test in release

cd android 
./gradlew assembleRelease
./gradlew installRelease

Behavior

The sound doesn't load and the error displayed is :

{
    "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"
}

What I want ?

Just load and play an audio in release juste like in debug.


回答1:


I finally found the answer.

How it works

require()

not works well in release.


I don't know why require() works well in release with Image but not with Audio

So you need to use

{ uri: 'asset:/my_sound.m4a' }

and put your sound file (like my_sound.m4a) in android/app/src/main/assets:

And it will work.



来源:https://stackoverflow.com/questions/61338639/works-in-debug-but-not-in-release-expo-av-react-native-unimodules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!