커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
버튼누름 같은 것 없이 앱이 켜져 있다면 오디오를 재생시키려고 합니다
앱개발 종합반 - 리액트 네이티브
기타
북마크
김*현
댓글
1
추천
1
조회수
6
조회수
6
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


앱이 켜져있다면 항상 오디오를 재생시키고

다른 일을 하러갈땐 이 앱을 켜놓은 상태로 화면을 꺼서

누군가가 폰을 킨다면 소리가 바로 나게 하려는데요


일단 expo -av에 있는 예시로 만들어보는중인데

함수가 버튼 누름 같은 트리거 없이 즉시 호출되게 하려면 어떻게 해야할까요?

스파르타 즉문즉답



import * as React from 'react';

import { Text, View, StyleSheet, Button } from 'react-native';

import { Audio } from 'expo-av';


export default function App() {

  const [sound, setSound] = React.useState();


  async function playSound() {

    console.log('Loading Sound');

    const { sound } = await Audio.Sound.createAsync( require('./assets/Hello.mp3')

    );

    setSound(sound);


    console.log('Playing Sound');

    await sound.playAsync();

  }


  React.useEffect(() => {

    return sound

      ? () => {

          console.log('Unloading Sound');

          sound.unloadAsync();

        }

      : undefined;

  }, [sound]);


  return (

    <View style={styles.container}>

      <Button title="Play Sound" onPress={playSound} />

    </View>

  );

}


const styles = StyleSheet.create({

  container: {

    flex: 1,

    justifyContent: 'center',

    backgroundColor: '#ecf0f1',

    padding: 10,

  },

});



취소
 공유
취소
댓글 0
댓글 알림
나의얼굴