Skip to content

Commit

Permalink
localized sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarov-andrey committed Sep 18, 2012
1 parent feb7af7 commit c15f99a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Lightning.mli
Expand Up @@ -22,8 +22,8 @@ ENDIF;
type remoteNotification = [= `RNBadge | `RNSound | `RNAlert ];
value request_remote_notifications: list remoteNotification -> (string -> unit) -> (string -> unit) -> unit;

(* value getLocale: unit -> string;
value getVersion: unit -> string; *)
value getLocale: unit -> string;
value getVersion: unit -> string;

value addExceptionInfo: string -> unit;
value setSupportEmail: string -> unit;
Expand Down
72 changes: 51 additions & 21 deletions src/android/java/src/ru/redspell/lightning/LightMediaPlayer.java
Expand Up @@ -8,8 +8,10 @@

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File;

public class LightMediaPlayer extends MediaPlayer {
private static ArrayList<LightMediaPlayer> instances;
Expand Down Expand Up @@ -107,49 +109,77 @@ public OffsetSizePair(int offset, int size) {

private static native OffsetSizePair getOffsetSizePair(String path);

private static boolean setMpDataSrc(MediaPlayer mp, String path) {
private static boolean setMpDataSrc(MediaPlayer mp, String assetsDir, String path) throws IOException {
Log.d("LIGHTNING", "setMpDataSrc: " + path);

OffsetSizePair pair = getOffsetSizePair(path);
File f = assetsDir != null ? new File(assetsDir + (assetsDir.charAt(assetsDir.length() - 1) == '/' ? "" : "/") + path) : null;

if (pair != null) {
mp.setDataSource((new FileInputStream(LightView.instance.getExpansionPath(true))).getFD(), pair.offset, pair.size);
return true;
} else if (assetsDir != null) {
mp.setDataSource(assetsDir + (assetsDir.charAt(assetsDir.length() - 1) == '/' ? "" : "/") + path);
} else {
} else if (f != null && f.exists()) {
mp.setDataSource(f.getAbsolutePath());
return true;
}

try {
AssetFileDescriptor afd = LightView.instance.getContext().getAssets().openFd(path);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

return true;
} catch (IOException e) {
return false;
}
}

return false;
private static int soundPoolLoad(SoundPool sndPool, String path) {
Log.d("LIGHTNING", "soundPoolLoad: " + path);

OffsetSizePair pair = getOffsetSizePair(path);
File f = path.charAt(0) == '/' ? new File(path) : null;

if (pair != null) {
try {
return sndPool.load((new FileInputStream(LightView.instance.getExpansionPath(true))).getFD(), pair.offset, pair.size, 1);
} catch (Exception e) {
return -1;
}
} else if (f != null && f.exists()) {
return sndPool.load(path, 1);
}

try {
return sndPool.load(LightView.instance.getContext().getAssets().openFd(path), 1);
} catch (IOException e) {
return -1;
}
}

public static MediaPlayer createMediaPlayer(String assetsDir, String path) throws IOException {
MediaPlayer mp = new LightMediaPlayer();
String locale = Locale.getDefault().getLanguage();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

OffsetSizePair pair = getOffsetSizePair(path);

if (pair != null) {
mp.setDataSource((new FileInputStream(LightView.instance.getExpansionPath(true))).getFD(), pair.offset, pair.size);
} else if (assetsDir != null) {
mp.setDataSource(assetsDir + (assetsDir.charAt(assetsDir.length() - 1) == '/' ? "" : "/") + path);
} else {
AssetFileDescriptor afd = LightView.instance.getContext().getAssets().openFd(path);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
if (!setMpDataSrc(mp, assetsDir, path) && !setMpDataSrc(mp, assetsDir, "locale/" + locale + "/" + path)) {
if (locale != "en" && !setMpDataSrc(mp, assetsDir, "locale/en/" + path)) {
return null;
}
}

return mp;
}

public static int getSoundId(String path, SoundPool sndPool) throws IOException {
OffsetSizePair pair = getOffsetSizePair(path);
String locale = Locale.getDefault().getLanguage();
int retval;

if (pair != null) {
sndPool.load((new FileInputStream(LightView.instance.getExpansionPath(true))).getFD(), pair.offset, pair.size, 1);
} else if (path.charAt(0) == '/') {
return sndPool.load(path, 1);
if ((retval = soundPoolLoad(sndPool, path)) < 0 && (retval = soundPoolLoad(sndPool, "locale/" + locale + "/" + path)) < 0) {
if (locale != "en") {
retval = soundPoolLoad(sndPool, "locale/en/" + path);
}
}

return sndPool.load(LightView.instance.getContext().getAssets().openFd(path), 1);
return retval;
}
}
18 changes: 15 additions & 3 deletions src/android/mlwrapper_android.c
Expand Up @@ -690,6 +690,13 @@ value ml_alsoundLoad(value path) {
char* cpath = String_val(path);
jstring jpath = (*env)->NewStringUTF(env, cpath);
jint sndId = (*env)->CallStaticIntMethod(env, lmpCls, gGetSndIdMthdId, jpath, gSndPool);

if (sndId < 0) {
char mes[255];
sprintf(mes, "cannot find %s when adding to sound pool", cpath);
caml_failwith(mes);
}

(*env)->DeleteLocalRef(env, jpath);

return Val_int(sndId);
Expand Down Expand Up @@ -1110,9 +1117,7 @@ value ml_avsound_create_player(value vpath) {
static jmethodID createMpMid;
jclass lmpCls = get_lmp_class();

if (!createMpMid) {
createMpMid = (*env)->GetStaticMethodID(env, lmpCls, "createMediaPlayer", "(Ljava/lang/String;Ljava/lang/String;)Landroid/media/MediaPlayer;");
}
if (!createMpMid) createMpMid = (*env)->GetStaticMethodID(env, lmpCls, "createMediaPlayer", "(Ljava/lang/String;Ljava/lang/String;)Landroid/media/MediaPlayer;");

const char* cpath = String_val(vpath);
jstring jpath = (*env)->NewStringUTF(env, cpath);
Expand All @@ -1123,6 +1128,13 @@ value ml_avsound_create_player(value vpath) {
}

jobject mp = (*env)->CallStaticObjectMethod(env, lmpCls, createMpMid, jassetsDir, jpath);

if (!mp) {
char mes[255];
sprintf(mes, "cannot find %s when creating media player", cpath);
caml_failwith(mes);
}

jobject gmp = (*env)->NewGlobalRef(env, mp);

if (jassetsDir) {
Expand Down
27 changes: 25 additions & 2 deletions test/example.ml
Expand Up @@ -1375,8 +1375,31 @@ let stage width height =
value bgColor = 0xCCCCCC;
initializer begin

let channel1 = Sound.createChannel (Sound.load "melody0.mp3") in
channel1#play ();
Sound.init ();

let channel1 = Sound.createChannel (Sound.load "achievement1.caf")
and img = Image.load "Russia.png" in
(
self#addChild img;

ignore(Stage.(
img#addEventListener ev_TOUCH (fun ev _ _ ->
match touches_of_data ev.Ev.data with
[ Some [ touch :: _ ] ->
Touch.(
match touch.phase with
[ TouchPhaseEnded -> channel1#play ()
| _ -> ()
]
)
| _ -> ()
]
)
));
);



(* ignore(LightCommon.read_json "LangRU.json"); *)
(* debug "%s %s %d" (Hardware.platform ()) (Hardware.hwmodel ()) (Hardware.total_memory ()); *)
(* debug "pizda";
Expand Down

0 comments on commit c15f99a

Please sign in to comment.