From e23123be0c43ef4ef0707d60757817fb2b243386 Mon Sep 17 00:00:00 2001 From: Michal Sek Date: Tue, 17 Dec 2024 16:47:55 +0100 Subject: [PATCH] fix: first tutorial wanna-be urls and finish example code --- .../fundamentals/lets-make-some-noise.mdx | 38 +++++++++++++++++-- .../examples/LetsMakeSomeNoise/Component.tsx | 3 +- .../src/examples/LetsMakeSomeNoise/Source.tsx | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/audiodocs/docs/fundamentals/lets-make-some-noise.mdx b/packages/audiodocs/docs/fundamentals/lets-make-some-noise.mdx index 797f2270..0ff007d6 100644 --- a/packages/audiodocs/docs/fundamentals/lets-make-some-noise.mdx +++ b/packages/audiodocs/docs/fundamentals/lets-make-some-noise.mdx @@ -45,7 +45,7 @@ export default function App() { const audioContext = new AudioContext(); const audioBuffer = await FileSystem.downloadAsync( - 'https://docs.swmansion.com/audio-api/audio/music/example-music-01.mp3', + 'https://software-mansion-labs.github.io/react-native-audio-api/audio/music/example-music-01.mp3', `${FileSystem.documentDirectory}/example-music-01.mp3` ).then(({ uri }) => audioContext.decodeAudioDataSource(uri)); }; @@ -62,8 +62,40 @@ We have used `decodeAudioDataSource` method of the `AudioContext`, which takes u ## Play the audio -Last and final step is to create `AudioBufferSourceNode`. It is one of few nodes that are starting points in the audio graph and can "produce" sounds. All of them have `start` and `stop` functions. -We can assign the `audioBuffer` obtained in previous step to finally play it! +Last and final step is to create `AudioBufferSourceNode`, connect it to the `AudioContext` destination and start playing the sound. For the purpose of this guide, we will play the sound for only 10 seconds. + +```jsx {15-16,18-20} +import React from 'react'; +import * as FileSystem from 'expo-file-system'; +import { View, Button } from 'react-native'; +import { AudioContext } from 'react-native-audio-api'; + +export default function App() { + const handlePlay = async () => { + const audioContext = new AudioContext(); + + const audioBuffer = await FileSystem.downloadAsync( + 'https://software-mansion-labs.github.io/react-native-audio-api/audio/music/example-music-01.mp3', + `${FileSystem.documentDirectory}/example-music-01.mp3` + ).then(({ uri }) => audioContext.decodeAudioDataSource(uri)); + + const playerNode = audioContext.createBufferSource(); + playerNode.buffer = audioBuffer; + + playerNode.connect(audioContext.destination); + playerNode.start(audioContext.currentTime); + playerNode.stop(audioContext.currentTime + 10); + }; + + return ( + +