Enable sharing on Android

We use a modified version of cengiz-pz's godot android share plugin[0],
which allows us to not only share text/images, but also sund files (see
PR here[1]). With this, the "Write WAV" button (bad naming, I know) will
allow the user to share a wav file of the morsed data to another app,
e.g. for sending some morse via Signal. It looks like Telegram doesn't
like wav files in general, so another audio format, might be interesting
(though it's hard to choose, as we don't have a proper encoder on board,
mp3 is a pain and opus or ogg is not compatible with very old Iphones).

Using the share plugin requires us to enable gradle build. An explicitly
set unique name is required as well, else sharing will fail (see [2]).

I'm not sure if the Share node needs to be an extra node or if we could
also use it as an autoload script instead, but I went with the way the
docs described it for now.

[0] https://github.com/cengiz-pz/godot-android-share-plugin/
[1] https://github.com/cengiz-pz/godot-android-share-plugin/pull/8
[2] https://github.com/cengiz-pz/godot-android-share-plugin/issues/7

 * requires gradle build
 * requires app name
This commit is contained in:
Sebastian Lohff 2025-02-16 19:19:06 +01:00
parent 8c1891a12e
commit 7f06f39f7e
5 changed files with 29 additions and 14 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
.godot/
*.swp
*.swo
export/
android/

View File

@ -1,6 +1,6 @@
[preset.0]
name="Android"
name="Android (Gradle Build)"
platform="Android"
runnable=true
advanced_options=false
@ -20,7 +20,7 @@ script_export_mode=2
custom_template/debug=""
custom_template/release=""
gradle_build/use_gradle_build=false
gradle_build/use_gradle_build=true
gradle_build/gradle_build_directory=""
gradle_build/android_source_template=""
gradle_build/compress_native_libraries=false
@ -33,7 +33,7 @@ architectures/x86=false
architectures/x86_64=false
version/code=1
version/name=""
package/unique_name="com.example.$genname"
package/unique_name="de.sebageek.cwgenerator"
package/name=""
package/signed=true
package/app_category=2

View File

@ -26,6 +26,10 @@ window/size/viewport_width=786
window/size/viewport_height=1024
window/handheld/orientation=1
[editor_plugins]
enabled=PackedStringArray("res://addons/SharePlugin/plugin.cfg")
[rendering]
renderer/rendering_method="mobile"

View File

@ -4,7 +4,7 @@ var sample_hz = 22050.0 # Keep the number of samples to mix low, GDScript is not
var pulse_hz = 440.0 * 2
var phase = 0.0
var morse_state := false
var playback: AudioStreamPlayback = null
var playback: AudioStreamPlayback = null
var vol_on := -30
func _process(_delta):
@ -24,7 +24,7 @@ func _ready():
$Player.play()
playback = $Player.get_stream_playback()
fill_buffer()
OS.open_midi_inputs()
print(OS.get_connected_midi_inputs())
@ -77,17 +77,17 @@ func _on_wav_button_pressed() -> void:
var data: Array[int] = []
var _use_test_data := false
var amp := 100
if not _use_test_data:
data.append_array(_make_tone(0, 0.2, sample_hz, amp))
var _morse_on := true
for n in range(1, MorseState.states.size()):
var _freq = pulse_hz if _morse_on else 0.0
print(_freq, " of length ", MorseState.states[n] / 1000.0)
data.append_array(_make_tone(_freq, MorseState.states[n] / 1000.0, sample_hz, amp))
_morse_on = not _morse_on
data.append_array(_make_tone(0, 0.2, sample_hz, amp))
else:
data.append_array(_make_tone(pulse_hz, 0.5, sample_hz, 100))
@ -95,23 +95,28 @@ func _on_wav_button_pressed() -> void:
data.append_array(_make_tone(pulse_hz, 0.5, sample_hz, 100))
data.append_array(_make_tone(0, 0.5, sample_hz, 100))
data.append_array(_make_tone(pulse_hz, 0.5, sample_hz, 100))
# save wav
var wav := AudioStreamWAV.new()
wav.format = AudioStreamWAV.FORMAT_8_BITS
wav.mix_rate = sample_hz
wav.stereo = false
wav.data = data
print("Writing WAV with ", data.size(), " length")
var proposed_fname := "morse-" + Time.get_datetime_string_from_system(true) + ".wav"
match OS.get_name():
["Linux", "macOS", "Windows"]:
"Linux", "macOS", "Windows":
wav.save_to_wav("/tmp/foo.wav")
print("GLobalized path: " + ProjectSettings.globalize_path("user://morse.wav"))
"Android":
print("Whaaat")
print("Sharing file on android")
var tmp_file_path := OS.get_user_data_dir().path_join(proposed_fname)
wav.save_to_wav(tmp_file_path)
$Share.share_file(tmp_file_path, "audio/vnd.wave", proposed_fname, "", "")
# DirAccess.remove_absolute(tmp_file_path)
"Web":
print("download")
print("Download on Web")
# we either write our own wave header method or we use a temporary file
# FIXME: find out if we have our own temp / if our filename needs to be random
var tmp_file := "/tmp/morse.wav"

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://ctak1goemnnc5"]
[gd_scene load_steps=5 format=3 uid="uid://ctak1goemnnc5"]
[ext_resource type="Script" path="res://scenes/main.gd" id="1_8bx00"]
[ext_resource type="PackedScene" uid="uid://xqic6oa5d7oc" path="res://scenes/MorseBanner.tscn" id="2_v02md"]
[ext_resource type="Script" path="res://addons/SharePlugin/Share.gd" id="3_ci1yg"]
[sub_resource type="AudioStreamGenerator" id="AudioStreamGenerator_kvn5v"]
mix_rate = 11025.0
@ -66,6 +67,9 @@ text = "Reset"
stream = SubResource("AudioStreamGenerator_kvn5v")
volume_db = -80.0
[node name="Share" type="Node" parent="."]
script = ExtResource("3_ci1yg")
[connection signal="button_down" from="VBoxContainer/MorseButton" to="." method="_on_morse_button_down"]
[connection signal="button_up" from="VBoxContainer/MorseButton" to="." method="_on_morse_button_up"]
[connection signal="pressed" from="VBoxContainer/WavButton" to="." method="_on_wav_button_pressed"]