forked from LeenkxTeam/LNXSDK
Update Files
This commit is contained in:
11
Kha/Backends/WPF/kha/audio1/Audio.hx
Normal file
11
Kha/Backends/WPF/kha/audio1/Audio.hx
Normal file
@ -0,0 +1,11 @@
|
||||
package kha.audio1;
|
||||
|
||||
class Audio {
|
||||
public static function play(sound: Sound, loop: Bool = false, stream: Bool = false): kha.audio1.AudioChannel {
|
||||
return new WpfAudioChannel(cast(sound, kha.wpf.Sound).filename);
|
||||
}
|
||||
|
||||
public static function stream(sound: Sound, loop: Bool = false): kha.audio1.AudioChannel {
|
||||
return new WpfAudioChannel(cast(sound, kha.wpf.Sound).filename);
|
||||
}
|
||||
}
|
80
Kha/Backends/WPF/kha/audio1/WpfAudioChannel.hx
Normal file
80
Kha/Backends/WPF/kha/audio1/WpfAudioChannel.hx
Normal file
@ -0,0 +1,80 @@
|
||||
package kha.audio1;
|
||||
|
||||
import system.io.Path;
|
||||
import system.Uri;
|
||||
import system.UriKind;
|
||||
import system.windows.controls.MediaElement;
|
||||
import system.windows.controls.MediaState;
|
||||
|
||||
class WpfAudioChannel implements kha.audio1.AudioChannel {
|
||||
var player: MediaElement;
|
||||
var hasFinished: Bool = false;
|
||||
|
||||
public function new(filename: String) {
|
||||
this.player = new MediaElement();
|
||||
addEventHandlers();
|
||||
player.LoadedBehavior = MediaState.Manual;
|
||||
player.UnloadedBehavior = MediaState.Manual;
|
||||
// MediaElement needs Absolute URI. Relative won't work
|
||||
player.Source = new Uri(Path.GetFullPath(filename), UriKind.Absolute);
|
||||
// TODO: perhaps files should be checked for validity?
|
||||
|
||||
play();
|
||||
}
|
||||
|
||||
public function play(): Void {
|
||||
hasFinished = false;
|
||||
player.Play();
|
||||
}
|
||||
|
||||
public function pause(): Void {
|
||||
player.Pause();
|
||||
}
|
||||
|
||||
public function stop(): Void {
|
||||
hasFinished = true;
|
||||
player.Stop();
|
||||
}
|
||||
|
||||
public var length(get, never): Float;
|
||||
|
||||
@:functionCode('
|
||||
if (player.NaturalDuration.HasTimeSpan) return player.NaturalDuration.TimeSpan.TotalMilliseconds * 1000.0;
|
||||
else return float.MaxValue;
|
||||
')
|
||||
function get_length(): Float {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public var position(get, never): Float; // Seconds
|
||||
|
||||
@:functionCode('return Math.round(player.Position.TotalMilliseconds) * 1000.0;')
|
||||
function get_position(): Float {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public var volume(get, set): Float;
|
||||
|
||||
function get_volume(): Float {
|
||||
return player.Volume;
|
||||
}
|
||||
|
||||
function set_volume(value: Float): Float {
|
||||
return player.Volume = value;
|
||||
}
|
||||
|
||||
public var finished(get, never): Bool;
|
||||
|
||||
function get_finished(): Bool {
|
||||
return hasFinished;
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
player.MediaEnded += OnMediaEnded;
|
||||
')
|
||||
function addEventHandlers() {}
|
||||
|
||||
function OnMediaEnded(obj: Dynamic, e: RoutedEventArgs) {
|
||||
hasFinished = true;
|
||||
}
|
||||
}
|
21
Kha/Backends/WPF/kha/audio1/WpfMusicChannel.hx
Normal file
21
Kha/Backends/WPF/kha/audio1/WpfMusicChannel.hx
Normal file
@ -0,0 +1,21 @@
|
||||
package kha.audio1;
|
||||
|
||||
import system.windows.controls.MediaElement;
|
||||
|
||||
class WpfMusicChannel extends WpfSoundChannel implements kha.audio1.MusicChannel {
|
||||
var looping: Bool = false;
|
||||
|
||||
public function new(filename: String, looping: Bool) {
|
||||
super(filename);
|
||||
this.looping = looping;
|
||||
}
|
||||
|
||||
override function OnMediaEnded(obj: Dynamic, e: RoutedEventArgs): Void {
|
||||
if (looping) {
|
||||
play();
|
||||
}
|
||||
else {
|
||||
hasFinished = true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user