间歇性崩溃,音频以本机模式出现 [英] Intermittent crashes with audio in react-native

查看:106
本文介绍了间歇性崩溃,音频以本机模式出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

播放音频时崩溃:

我正在创建一个具有几个不同屏幕的音频剪辑的应用程序.我们正在使用Testflight在iPhone/iPad上测试该应用程序,并且在播放音频片段时会间歇性崩溃.音频剪辑本身似乎没有什么问题,因为它们大多数时候都在工作,而且并非总是同一音频剪辑会出现问题.

I am creating an app with several different screens of audio clips. We are testing out the app on iPhone/iPad using Testflight and are getting intermittent crashes when audio clips are played. There seems to be nothing wrong with the audio clips themselves as most of the time they work and it is not always the same audio clip that has the problem.

我猜测使用 new Sound 可能是内存泄漏,但是我不确定如何对此进行测试.我还以为我可以通过在播放后以及屏幕卸载时确保释放声音组件来解决这个问题.

I am guessing it might be a memory leak to do with new Sound but I am not sure how to test for this. I also thought that I covered this by making sure that I released the sound component after it played and when the screen unmounts.

在确保正确清理 this.sound 组件时,我缺少某些东西吗?

Is there something that I am missing in making sure that I correctly clean up the this.sound component?

我的代码:

import React, { Component } from 'react';

var Sound = require('react-native-sound');
Sound.setCategory("Playback"); //Needed for audio to play on IOS devices

import {
    Image,
    ImageBackground,
    SafeAreaView,
    ScrollView,
    View,
    Text,
    TouchableOpacity
} from 'react-native';

export default class AudioList extends Component {


    constructor(props) {
        super(props)

    }

    playAudio = (file) => {
        console.log(file)

        if (this.sound) {
            console.log("SOUND")

            try {
                this.sound.release()
            } catch (error) {
                console.log("A sound release error has occured 111")
            } finally {
                this.sound = null
            }
        }

        this.sound = new Sound('audio/' + file, Sound.MAIN_BUNDLE, (error) => {
            if (error) {
                console.log('error', error);
                this.sound = null;
            } else {
                this.sound.play(() => {
                    try {
                        if (this.sound) {
                            this.sound.release()
                        }
                    } catch (error) {
                        console.log("A sound release error has occured 222")
                    } finally {
                        this.sound = null
                    }
                })
            }
        })


        this.willBlurSubscription = this.props.navigation.addListener(
            'blur',
            () => {
                try {
                    if (this.sound) {
                        this.sound.release()
                    }
                } catch (error) {
                    console.log("A sound release error has occured 333")
                } finally {
                    this.sound = null
                }

            }
        )



    }

    componentWillUnmount() {

        try {
            this.willBlurSubscription &&
                this.willBlurSubscription.remove &&
                this.willBlurSubscription.remove()
        } catch (error) {} finally {
            this.willBlurSubscription = null
        }
    }


    render() {

        /* list and new_list removed for brevity */
        /* styles removed for brevity */

        let audio_clips = new_list.map((item, index) => {
            return <View key={index}>
                {item.audio ? 
                    <TouchableOpacity onPress={()=>this.playAudio(item.audio)}>
                        <Image source={require('../assets/images/audio-icon.png')} />
                    </TouchableOpacity>
                    :
                    <View></View>
                }
                <Text>{item.text}</Text>
        </View>
        })



        return (
            <SafeAreaView>
            <Text>{list.category_name}</Text>
            <ImageBackground source={require('../assets/images/background.jpg')}>
                <ScrollView>
                    <View>
                        {audio_clips}
                    </View>
                </ScrollView>
            </ImageBackground>
        </SafeAreaView>
        )



    }
}

推荐答案

在这里只是一个简单的猜测,但是您确定将声音对象设置为null会将其从内存中删除吗?可能只是另一个指针"

Just a quick guess here, but are you sure that setting null to the object of sound deltes it from memory? might just have another "pointer" to it.

您是否尝试过将相同的代码与Delete一起尝试,如果这可能是问题所在?this.sound = null试一试删除这种声音或东西吗?

have you tried the same code with delete just to try and if this might be the issue? this.sound = null instead try delete this.sound or somthing of this sorts?

我知道删除是最差的优化做法,但可能对您有所帮助.

I know delete counts as bad optimization practice but might just help you here.

这篇关于间歇性崩溃,音频以本机模式出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆