Ask coding questions

← Back to all posts
How to add music to a C++ Repl?
Typhlotion (1)

Hi,
I'm very new to coding and a lot of the explanations on how to make a repl have music and sound completely flew over my head. Is there just no way to get audio without being on JavaScript/Python or using NoVNC?
Asking for a text-based-game project i've been working on for school, and i've already read https://docs.replit.com/repls/audio , I just don't understand how it is that I could apply that to C++ (if that's even possible to begin with).
Thanks in advance.

EDIT: What i've gathered is that in order to have audio work in C++, you'd have to call a code from python that would run the audio. My problem is that i've only got a fundamental understanding of C++ and absolutely zero knowledge on python. So, how would you call a python file from C++? I tried the system("") method suggested by 19ecal to no avail and I couldn't make sense of the ostream method. Could you dumb your answers down to my level, or maybe present another, even simpler method?
Again, thanks in advance.

Comments
hotnewtop
Coder100 (18882)

The docs are pretty clear about this, you write to a file.

Try this:

#include <fstream>

int main() {
  std::ostream file("/tmp/audio");

  // write to file
}
19ecal (233)

Well, you could make a .py file like this

import sys
from replit import audio
source=audio.play_file(sys.argv[1])

Then you can call it from C++

system("python sound.py file.mp3")

All of this is untested
You might want to make a function to play a sound file you pass it in c++ though
And you might have to #include <cstdlib> as well

Typhlotion (1)

@19ecal I tried this method and it runs, but I'm getting no audio. Is there something that i'm missing?