Home > Mobile >  How to play encode base64 string in react-native
How to play encode base64 string in react-native

Time:09-27

I want to play encoded base64 string music. I've tried several attempts but failed. Please tell me how to solve this problem.

Here is my code.

const handleAnswerCall = async () => {
  try {
    let a = await new Sound(userInfo?.audioRecord1   userInfo?.audioRecord2);
    a.play();
  } catch (e) {
    console.log(e);
  }
};

I've also attempt writeFilestram(1.mp3, "base64 string", "base64") but failed.

How to solve my problem?

CodePudding user response:

In case of iOS

const path = `${RNFS.DocumentDirectoryPath}/myFile.aac`;

Or in case of windows

const path = `${RNFS.DocumentDirectoryPath}/myFile.mp4`;

It would be appreciated if this code help you.

Thanks.

CodePudding user response:

Assuming you're using react-native-sound it doesn't support file streams only physical files.

You'll need to write your base64 data to a file for it to read from you can do this with react-native-fs

Something a bit like this

const RNFS = require("react-native-fs");
const Sound = require("react-native-sound");

const someBase64 = "...";

const path = `${RNFS.DocumentDirectoryPath}/myFile.aac`;

await RNFS.writeFile(path, someBase64, "base64");
const sound = new Sound(path, "", (error) => {
  if (error) console.error(error);
  else sound.play();
});
  • Related