How to implement a function to play music in Internet

I want to implement a function that can play music with URL, but I don’t know how to do it. I tried to search with search engines, but found nothing helpful. Is it possible to implement this with Godot?

You can use an HTTPRequest to fetch a URL. You can then create an AudioStream and load the audio into it. Finally, an AudioStreamPlayer can be used to play the stream.
See Making HTTP Requests for a tutorial on how to use HTTP requests, and see here for more information about audio streams.

Note that this is just a high-level overview. Feel free to ask any more questions you may have.

2 Likes
@onready var audio_player = $AudioStreamPlayer

func _ready() -> void:
	var music_url = ""
	var temp_req = HTTPRequest.new()
	add_child(temp_req)
	var stream = AudioStreamMP3.new()
	temp_req.request_completed.connect(func(result, code, headers, body): 
		if code == 200:
			stream.data = body
			audio_player.stream = stream
			audio_player.play()
		temp_req.queue_free()
	)
	temp_req.request(music_url)

Thank you, but I tried this method and found a problem. That is, I need to download the entire audio file before I can play it. If the audio file is large, I need to wait for a while. My ideal state is to be able to download and play at the same time.

It seems like AudioStreamGenerator could be coerced into doing that, but you’d probably have to write a bunch of code to make it happen. Godot is also capable of calling other executables (see OS.execute_with_pipe()), so you could potentially call out to another program to do this…

It’s not possible to stream audio from the Internet and I’m not aware of any plugin that could do that.

That’s called streaming audio. As @hexgrid and @mrcdk have pointed out, you’re going to have to write that code yourself. That’s going to include dealing with buffering and dropped connections.

What kind of game are you envisioning where the person is going to have to be connected to the internet to play it, and is going to be wanting to stream audio?

I want to make a 3D adventure game, and I’d like to have a place in my game where I can play online music and watch online movies, although it’s not necessary.

Can’t you just fake it? Does it have to be really streamed from online?
You know that radio in GTA is not a real radio, right? :slight_smile:

1 Like

I think you would struggle from a licencing and rights point of view with that. I really wouldn’t bother wasting your time. Thats a compete non starter.

1 Like

Ok, why? What are the benefits to doing this inside a game for your players, since they can do those things anyway if they want to already while playing the game. Most Millenials and younger already multitask while playing games.

@wchc and @OriginalBadBoy make good points. But I still think you answering the why for yourself is important.

I just think it would be fun to have this feature in the game. I am the developer of my game as well as the player of this game, I can add and implement the features I want to play.

Absolutely. The reason I ask is you have a number of hurdles to overcome, so being clear on why it’s important to you is helpful as you encounter difficulties.

From a legal point of view, keep in mind that to stream music from a server, you will have to have a streaming license and may have to pay a streaming fee for every song that your players play. These are usually fractions of a penny, but they can add up. It would be good to talk to a lawyer about this to know your rights and responsibilities. You will also need to know any applicable laws for players that are streaming music from countries other than your own. You will also be responsible for blocking content in any places that do not allow particular songs (for example China).

This can be avoided if you are using music and videos that either you create or are in the public domain. Also, legally you can use clips of music and video under certain circumstances as long as they are less than 30 seconds long.

If you violate any of these laws, people may request that your game is delisted from whatever store(s) it is on. Once that happens, it can be hard to get put back, and you can get your entire developer account banned.

I strongly recommend you retain a lawyer. We are not lawyers, and any legal advice you get here isn’t binding. Nor are we familiar with all the applicable laws. There are two large organizations in the U.S. that hold the copyright for most music, and they offer deals on licensing music. Their only job is to make money by suing people.

As far as streaming videos, that’s a whole other ball of wax, and I really don’t have any knowledge of that. Again, a lawyer could help you there.

From a technical point of view, you’ll probably want to write your streaming plugin in C++ or C#, because most other languages are going to be too slow to deal with streaming audio and video (per the warning in the Godot docs). You will probably get more help on StackOverflow with that, as there are lots of web developers who have experience with that.

This is an ambitious project, but could really help other people in the future who want to stream audio and video over the internet.

Good luck!

2 Likes