Browse Source

New MIDI framework: Android (try)

LouYihua 7 years ago
parent
commit
658cedf41b

+ 30 - 21
android/jni/src/android_jni.c

@@ -12,8 +12,8 @@
 #include "global.h"
 #include "palcfg.h"
 
-char externalStoragePath[255];
-char midiInterFile[255];
+static char externalStoragePath[1024];
+static char midiInterFile[1024];
 
 static int jstring_to_utf8(JNIEnv *env, jstring j_str, char *buffer, int capacity)
 {
@@ -155,9 +155,8 @@ JNIEnv *getJNIEnv()
  */  
 JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setExternalStorage(JNIEnv *env, jclass cls, jstring j_str)  
 {
-    jstring_to_utf8(env, j_str, externalStoragePath, 255 - 8);
+    jstring_to_utf8(env, j_str, externalStoragePath, sizeof(externalStoragePath) - 8);
     strncat(externalStoragePath, "/sdlpal/", 8);
-    return;
 }
 
 /* 
@@ -167,42 +166,52 @@ JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setExternalStorage(JNIE
  */  
 JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setMIDIInterFile(JNIEnv *env, jclass cls, jstring j_str)  
 {
-    jstring_to_utf8(env, j_str, midiInterFile, 255);
+    jstring_to_utf8(env, j_str, midiInterFile, sizeof(externalStoragePath));
     LOGV("JNI got midi inter filename:%s", midiInterFile);
-    return;
 }
 
-void JNI_mediaplayer_load(const char *filename)
+void* JNI_mediaplayer_load(const char *filename)
 {
     JNIEnv *env = getJNIEnv();
     jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
-    jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_load", "(Ljava/lang/String;)V");
-    (*env)->CallStaticVoidMethod(env, clazz, mid, jstring_from_utf8(env, filename));
+    jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_load", "(Ljava/lang/String;)Landroid/media/MediaPlayer;");
+    jobject player = (*env)->CallStaticObjectMethod(env, clazz, mid, jstring_from_utf8(env, filename));
+    return (*env)->NewGlobalRef(env, player);
 }
 
-void JNI_mediaplayer_play()
+void JNI_mediaplayer_free(void *player)
+{
+    (*env)->DeleteGlobalRef(env, (jobject)player);
+}
+
+void JNI_mediaplayer_play(void *player, int looping)
 {
     JNIEnv *env = getJNIEnv();
-    jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
-    jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_play", "()V");
-    (*env)->CallStaticVoidMethod(env, clazz, mid);
+    jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
+    (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "setLooping", "(Z)V"), looping ? JNI_TRUE : JNI_FALSE);
+    (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "start", "()V"));
 }
 
-void JNI_mediaplayer_stop()
+void JNI_mediaplayer_stop(void *player)
 {
     JNIEnv *env = getJNIEnv();
-    jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
-    jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_stop", "()V");
-    (*env)->CallStaticVoidMethod(env, clazz, mid);
+    jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
+    (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "stop", "()V"));
 }
 
-int JNI_mediaplayer_isplaying()
+int JNI_mediaplayer_isplaying(void *player)
 {
-    return 0;
+    JNIEnv *env = getJNIEnv();
+    jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
+    return (*env)->CallBooleanMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "isPlaying", "()Z"));
 }
 
-void JNI_mediaplayer_setvolume(int volume)
+void JNI_mediaplayer_setvolume(void *player, int volume)
 {
+    float vol = (float)volume / 127.0f;
+    JNIEnv *env = getJNIEnv();
+    jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
+    return (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "setVolume", "(FF)V"), vol, vol);
 }
 
 LPCSTR
@@ -255,4 +264,4 @@ UTIL_Platform_Quit(
    VOID
 )
 {
-}
+}

+ 6 - 5
android/jni/src/android_jni.h

@@ -4,10 +4,11 @@
 extern char externalStoragePath[255];
 extern char midiInterFile[255];
 
-void JNI_mediaplayer_load();
-void JNI_mediaplayer_play();
-void JNI_mediaplayer_stop();
-int JNI_mediaplayer_isplaying();
-void JNI_mediaplayer_setvolume(int);
+void* JNI_mediaplayer_load(const char *)
+void JNI_mediaplayer_free(void *)
+void JNI_mediaplayer_play(void *);
+void JNI_mediaplayer_stop(void *);
+int JNI_mediaplayer_isplaying(void *);
+void JNI_mediaplayer_setvolume(void *, int);
 
 #endif // SDLPAL_JNI_H

+ 31 - 42
android/jni/src/native_midi.c

@@ -32,13 +32,10 @@
 /* Native Midi song */
 struct _NativeMidiSong
 {
-    int _placeholder;
-    int playing;
+    void *player;
+    int   volume;
 };
 
-static NativeMidiSong *currentsong = NULL;
-static int latched_volume = 128;
-
 int native_midi_detect()
 {
     return 1;  /* always available. */
@@ -46,79 +43,71 @@ int native_midi_detect()
 
 NativeMidiSong *native_midi_loadsong(const char *midifile)
 {
-    NativeMidiSong *retval = (NativeMidiSong *)malloc(sizeof(NativeMidiSong));
-    if (retval)
+    NativeMidiSong *song = (NativeMidiSong *)malloc(sizeof(NativeMidiSong));
+    if (song)
     {
-        memset(retval, 0, sizeof(NativeMidiSong));
-        JNI_mediaplayer_load(midifile);
+        song->volume = 127;
+        song->player = JNI_mediaplayer_load(midifile);
     }
-    return retval;
+    return song;
 }
 
 NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
 {
-    NativeMidiSong *retval = (NativeMidiSong *)malloc(sizeof(NativeMidiSong));
-    if (retval)
+    FILE *fp = fopen(midiInterFile, "wb+");
+    if (fp)
     {
-        FILE *fp = fopen(midiInterFile, "wb+");
-        if (fp)
-        {
-            char buf[4096];
-            size_t bytes;
-            while(bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf)))
-                fwrite(buf, sizeof(char), bytes, fp);
-            fclose(fp);
-
-            memset(retval, 0, sizeof(NativeMidiSong));
-            JNI_mediaplayer_load(midiInterFile);
-        }
+        char buf[4096];
+        size_t bytes;
+        while(bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf)))
+            fwrite(buf, sizeof(char), bytes, fp);
+        fclose(fp);
+
+        return native_midi_loadsong(midiInterFile);
     }
-    return retval;
+    return NULL;
 }
 
 void native_midi_freesong(NativeMidiSong *song)
 {
     if (song != NULL)
     {
-        if (currentsong == song)
-            currentsong = NULL;
+        JNI_mediaplayer_free(song->player);
         free(song);
     }
 }
 
-void native_midi_start(NativeMidiSong *song)
+void native_midi_start(NativeMidiSong *song, int looping)
 {
-    native_midi_stop();
+    native_midi_stop(song);
     if (song != NULL)
     {
-        currentsong = song;
-        currentsong->playing = 1;
-        JNI_mediaplayer_play();
+        JNI_mediaplayer_play(song->player, looping);
     }
 }
 
-void native_midi_stop()
+void native_midi_stop(NativeMidiSong *song)
 {
-    if (currentsong) {
-        currentsong->playing = 0;
-        JNI_mediaplayer_stop();
+    if (song)
+    {
+        JNI_mediaplayer_stop(song->player);
     }
 }
 
-int native_midi_active()
+int native_midi_active(NativeMidiSong *song)
 {
-    return currentsong ? currentsong->playing : 0;
+    return song ? JNI_mediaplayer_play(song->player) : 0;
 }
 
-void native_midi_setvolume(int volume)
+void native_midi_setvolume(NativeMidiSong *song, int volume)
 {
-    if (latched_volume != volume)
+    if (song)
     {
-        JNI_mediaplayer_setvolume(latched_volume = volume);
+        JNI_mediaplayer_setvolume(song->player, song->volume = volume);
     }
 }
 
-const char *native_midi_error(void)
+const char *native_midi_error(NativeMidiSong *song)
 {
     return "";  /* !!! FIXME */
 }

+ 4 - 19
android/src/io/github/sdlpal/PalActivity.java

@@ -10,38 +10,23 @@ import java.util.*;
 
 public class PalActivity extends SDLActivity {
     private static final String TAG = "sdlpal-debug";
-    private static final MediaPlayer mediaPlayer = new MediaPlayer();
 
-    private static void JNI_mediaplayer_load(String filename){
+    private static MediaPlayer JNI_mediaplayer_load(String filename){
         Log.v(TAG, "loading midi:" + filename);
+        MediaPlayer mediaPlayer = new MediaPlayer();
         mediaPlayer.reset();
-        mediaPlayer.setLooping(true);
         try {
             mediaPlayer.setDataSource(mSingleton.getApplicationContext(), Uri.fromFile(new File(filename)));
             mediaPlayer.prepare();
         } catch(IOException e) {
             Log.e(TAG, filename + " not available for playing, check");
         }
-    }
-
-    private static void JNI_mediaplayer_play() {
-        mediaPlayer.start();
-    }
-
-    private static void JNI_mediaplayer_stop() {
-        mediaPlayer.stop();
-    }
-
-    private static int JNI_mediaplayer_playing() {
-        return mediaPlayer.isPlaying() ? 1 : 0;
-    }
-
-    private static void JNI_mediaplayer_setvolume(int volume) {
-        mediaPlayer.setVolume((float)volume/256, (float)volume/256);
+        return mediaPlayer;
     }
 
     public static native void setExternalStorage(String str);
     public static native void setMIDIInterFile(String str);
+
     @Override
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);