lib: some update towards more use of new constructors
[nit.git] / lib / android / audio.nit
index d5cee8b..e640621 100644 (file)
@@ -22,7 +22,7 @@
 module audio
 
 import java
-import java_io
+import java::io
 import assets_and_resources
 import app
 
@@ -41,7 +41,7 @@ extern class NativeAudioManager in "Java" `{ android.media.AudioManager `}
        redef type SELF: NativeAudioManager
 
        fun mode: Int in "Java" `{ return recv.getMode(); `}
-       fun mode=(i: Int) in "Java" `{ recv.setMode(i); `}
+       fun mode=(i: Int) in "Java" `{ recv.setMode((int)i); `}
        fun wired_headset_on: Bool in "Java" `{ return recv.isWiredHeadsetOn(); `}
        fun wired_headset_on=(b: Bool) in "Java" `{ recv.setWiredHeadsetOn(b); `}
        fun speakerphone_on: Bool in "Java" `{ return recv.isSpeakerphoneOn(); `}
@@ -74,7 +74,7 @@ extern class NativeMediaPlayer in "Java" `{ android.media.MediaPlayer `}
                }
        `}
 
-       fun create(context: NativeActivity, id: Int): NativeMediaPlayer in "Java" `{ return recv.create(context, id); `}
+       fun create(context: NativeActivity, id: Int): NativeMediaPlayer in "Java" `{ return recv.create(context, (int)id); `}
        fun pause in "Java" `{ recv.pause(); `}
        fun stop in "Java" `{ recv.stop(); `}
        fun playing: Bool in "Java" `{ return recv.isPlaying(); `}
@@ -84,7 +84,7 @@ extern class NativeMediaPlayer in "Java" `{ android.media.MediaPlayer `}
        fun looping=(b: Bool) in "Java" `{ recv.setLooping(b); `}
        fun volume=(vol: Float) in "Java" `{ recv.setVolume((float)vol, (float)vol); `}
        fun both_volume(left_volume, right_volume: Float) in "Java" `{ recv.setVolume((float)left_volume, (float)right_volume); `}
-       fun stream_type=(stream_type: Int) in "Java" `{ recv.setAudioStreamType(stream_type); `}
+       fun stream_type=(stream_type: Int) in "Java" `{ recv.setAudioStreamType((int)stream_type); `}
        fun data_source_fd(fd: NativeFileDescriptor, start_offset, length: Int) in "Java"  `{
                try {
                        recv.setDataSource(fd, start_offset, length);
@@ -101,6 +101,7 @@ extern class NativeMediaPlayer in "Java" `{ android.media.MediaPlayer `}
                        e.printStackTrace();
                }
        `}
+       fun reset in "Java" `{ recv.reset(); `}
 end
 
 # Sound Pool from Java, used to play sounds simultaneously
@@ -110,50 +111,51 @@ extern class NativeSoundPool in "Java" `{ android.media.SoundPool `}
        redef type SELF: NativeSoundPool
 
        new(max_streams, stream_type, src_quality: Int) in "Java" `{
-               return new SoundPool(max_streams, stream_type, src_quality);
+               return new SoundPool((int)max_streams, (int)stream_type, (int)src_quality);
        `}
-       fun load_asset_fd(afd: NativeAssetFileDescriptor, priority: Int): Int in "Java" `{ return recv.load(afd, priority); `}
-       fun load_id(context: NativeActivity, resid, priority: Int): Int in "Java" `{ return recv.load(context, resid, priority); `}
-       fun load_path(path: JavaString, priority: Int): Int in "Java" `{ return recv.load(path, priority); `}
+       fun load_asset_fd(afd: NativeAssetFileDescriptor, priority: Int): Int in "Java" `{ return recv.load(afd, (int)priority); `}
+       fun load_id(context: NativeActivity, resid, priority: Int): Int in "Java" `{ return recv.load(context, (int)resid, (int)priority); `}
+       fun load_path(path: JavaString, priority: Int): Int in "Java" `{ return recv.load(path, (int)priority); `}
        fun play(sound_id: Int, left_volume, right_volume: Float, priority, l: Int, rate: Float): Int in "Java" `{
-               return recv.play(sound_id, (float)left_volume, (float)right_volume, priority, l, (float)rate);
+               return recv.play((int)sound_id, (float)left_volume, (float)right_volume, (int)priority, (int)l, (float)rate);
        `}
-       fun pause(stream_id: Int) in "Java" `{ recv.pause(stream_id); `}
+       fun pause(stream_id: Int) in "Java" `{ recv.pause((int)stream_id); `}
        fun auto_pause in "Java" `{ recv.autoPause(); `}
        fun auto_resume in "Java" `{ recv.autoResume(); `}
-       fun resume(stream_id: Int) in "Java" `{ recv.resume(stream_id); `}
-       fun set_loop(stream_id, l: Int) in "Java" `{ recv.setLoop(stream_id, l); `}
-       fun set_priority(stream_id, priority: Int) in "Java" `{ recv.setPriority(stream_id, priority); `}
-       fun set_rate(stream_id: Int, rate: Float) in "Java" `{ recv.setRate(stream_id, (float)rate); `}
-       fun set_volume(stream_id: Int, left_volume, right_volume: Float) in "Java" `{ recv.setVolume(stream_id, (float)left_volume, (float)right_volume); `}
-       fun stop(stream_id: Int) in "Java" `{ recv.stop(stream_id); `}
-       fun unload(sound_id: Int): Bool in "Java" `{ return recv.unload(sound_id); `}
+       fun resume(stream_id: Int) in "Java" `{ recv.resume((int)stream_id); `}
+       fun set_loop(stream_id, l: Int) in "Java" `{ recv.setLoop((int)stream_id, (int)l); `}
+       fun set_priority(stream_id, priority: Int) in "Java" `{ recv.setPriority((int)stream_id, (int)priority); `}
+       fun set_rate(stream_id: Int, rate: Float) in "Java" `{ recv.setRate((int)stream_id, (float)rate); `}
+       fun set_volume(stream_id: Int, left_volume, right_volume: Float) in "Java" `{ recv.setVolume((int)stream_id, (float)left_volume, (float)right_volume); `}
+       fun stop(stream_id: Int) in "Java" `{ recv.stop((int)stream_id); `}
+       fun unload(sound_id: Int): Bool in "Java" `{ return recv.unload((int)sound_id); `}
+       fun release in "Java" `{ recv.release(); `}
 end
 
 
 # Used to play sound, best suited for sounds effects in apps or games
 class SoundPool
-       private var nsoundpool: NativeSoundPool
+       private var nsoundpool: NativeSoundPool is noinit
        # The maximum number of simultaneous streams for this SoundPool
-       var max_streams writable = 10
+       var max_streams = 10 is writable
 
        # The audio stream type, 3 is STREAM_MUSIC, default for game application
-       var stream_type writable = 3
+       var stream_type = 3 is writable
 
        # The sample-rate converter quality, currently has no effect
-       var src_quality writable = 0
+       var src_quality = 0 is writable
 
        # Left volume value, range 0.0 to 1.0
-       var left_volume writable = 1.0
+       var left_volume = 1.0 is writable
 
        # Right volume value, range 0.0 to 1.0
-       var right_volume writable = 1.0
+       var right_volume = 1.0 is writable
 
        # Playback rate, 1.0 = normal playback, range 0.5 to 2.0
-       var rate writable = 1.0
+       var rate = 1.0 is writable
 
        # Loop mode, 0 = no loop, -1 = loop forever
-       var looping writable = 0
+       var looping = 0 is writable
 
        # Stream priority
        private var priority = 1
@@ -221,17 +223,19 @@ class SoundPool
        fun stop_stream(stream_id: Int) do nsoundpool.stop(stream_id)
 
        # Unload a sound from a sound ID
-       fun unload(sound: SoundSP): Bool do return nsoundpool.unload(sound.soundpool_id.as(not null))
+       fun unload(sound: SoundSP): Bool do return nsoundpool.unload(sound.soundpool_id)
+
+       fun destroy do nsoundpool.release
 end
 
 # Used to play sounds, designed to use with medium sized sounds or streams
 # The Android MediaPlayer has a complex state diagram that you'll need to
 # respect if you want your MediaPlayer to work fine, see the android doc
 class MediaPlayer
-       private var nmedia_player: NativeMediaPlayer
+       private var nmedia_player: NativeMediaPlayer is noinit
 
        # The sound associated with this mediaplayer
-       var sound: nullable Sound
+       var sound: nullable Sound = null
 
        # Create a new MediaPlayer, but no sound is attached, you'll need
        # to use `load_sound` before using it
@@ -288,6 +292,9 @@ class MediaPlayer
                self.sound = null
        end
 
+       # Reset MediaPlayer to its initial state
+       fun reset do nmedia_player.reset
+
        # Sets the datasource (file-pathor http/rtsp URL) to use
        fun data_source(path: String): Sound do
                sys.jni_env.push_local_frame(1)
@@ -317,7 +324,6 @@ class MediaPlayer
 
        # Sets the audio stream type for this media player
        fun stream_type=(stream_type: Int) do nmedia_player.stream_type = stream_type
-
 end
 
 # Represents an android sound that can be played by a SoundPool or a MediaPlayer