Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AudioStreamOggVorbis.load_from_file Unable to Open File After Export #88226

Open
JNatheriver opened this issue Feb 11, 2024 · 2 comments · May be fixed by #88314
Open

AudioStreamOggVorbis.load_from_file Unable to Open File After Export #88226

JNatheriver opened this issue Feb 11, 2024 · 2 comments · May be fixed by #88314

Comments

@JNatheriver
Copy link

JNatheriver commented Feb 11, 2024

Tested versions

v4.2.1.stable

System information

Windows 10.0.22631 - GLES3 (Compatibility) - NVIDIA GeForce GTX 1050 (NVIDIA; 31.0.15.5123) - Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz (8 Threads)

Issue description

Similar to #87494 but this issue arises from the use of FileAccess under the hood by AudioStreamOggVorbis.load_from_file.

Ref<AudioStreamOggVorbis> ResourceImporterOggVorbis::load_from_file(const String &p_path) {
Vector<uint8_t> file_data = FileAccess::get_file_as_bytes(p_path);
ERR_FAIL_COND_V_MSG(file_data.is_empty(), Ref<AudioStreamOggVorbis>(), "Cannot open file '" + p_path + "'.");
return load_from_buffer(file_data);
}

The code functions correctly within the editor:

func play_sample():
	var audio_stream = AudioStreamOggVorbis.load_from_file(sample_path)
	audio_player.stream = audio_stream
	audio_player.play()

However, upon exporting the project, it displays the classic message "Can't open file from path."
I resolved this issue by utilizing ResourceLoader, as suggested in #87494 (comment) The amended code is as follows:

func play_sample():
    var audio_stream = \
    ResourceLoader.load(sample_path) as AudioStreamOggVorbis
    audio_player.stream = audio_stream
    audio_player.play()

It took me some time to identify and rectify this issue because the documentation lacks clarification regarding this behavior in the load_from_file method of the AudioStreamOggVorbis class. Perhaps a quick solution could be to add appropriate comments in the documentation to address this discrepancy like in FileAccess and DirAccess docs.

Steps to reproduce

  • Open a Godot project containing a script that utilizes the AudioStreamOggVorbis.load_from_file method to load an OGG audio file.
  • Ensure the project functions correctly within the Godot editor environment.
  • Export the project to an executable file (e.g., Windows, macOS, Linux) using the public export templates
  • Run the exported executable.
  • Attempt to execute the script that uses the AudioStreamOggVorbis.load_from_file method to load the OGG audio file.
  • Observe the error message "Can't open file from path" or any other relevant error prompts indicating the failure to load the audio file.

Minimal reproduction project (MRP)

Test.zip

@aaroncatlin
Copy link

aaroncatlin commented Feb 13, 2024

Found this myself yesterday. If you are dynamically loading audio files you need to change the Import settings in the editor to “No Import”. It will then keep them in the correct folders during Export.

Ignore me. The ResourceLoader method is obviously the intended way. As the reporter mentioned, it would be good to update documentation so this is clear. Alternatively, load_from_file should also not work in un-exported code so that’s it behaviour is consistent once exported.

@Calinou
Copy link
Member

Calinou commented Feb 13, 2024

For imported files, you should keep using preload() or load(). Runtime loading can only be used on non-imported files, as imported files are replaced with their remapped variant in the .godot/imported folder within the PCK.

This is why changing the import type to Keep File (No Import) works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants