Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Certainly! Here's the modified code that returns the `base64data` from the function:
- ```javascript
- // Function to fetch audio from URL and convert it to base64
- async function fetchAndConvertToBase64(url) {
- try {
- const response = await fetch(url);
- const audioBlob = await response.blob();
- const reader = new FileReader();
- reader.readAsDataURL(audioBlob);
- return new Promise((resolve, reject) => {
- reader.onloadend = function () {
- const base64data = reader.result.split(',')[1]; // Extract base64 data
- resolve(base64data);
- };
- });
- } catch (error) {
- console.error('Error fetching or converting audio to base64:', error);
- return null;
- }
- }
- // Example usage:
- const audioURL = 'YOUR_AUDIO_URL_HERE';
- fetchAndConvertToBase64(audioURL)
- .then(base64data => {
- if (base64data) {
- // Use the base64 data here as needed
- console.log('Base64 data:', base64data);
- // Call function to play audio using Audio5js or perform other actions
- // playAudioUsingAudio5js(base64data);
- } else {
- console.error('No base64 data available.');
- }
- });
- ```
- This updated function `fetchAndConvertToBase64()` now returns a Promise that resolves with the `base64data` of the fetched audio. You can use this function and handle the returned `base64data` accordingly in your application, such as playing it using Audio5js or performing any other necessary actions.
Add Comment
Please, Sign In to add comment