0

我正在尝试在 android 上运行提供的 Spotify 存根示例,但本机代码在尝试创建会话时似乎“崩溃”。这是我的代码的样子。

本机代码:

JNIEXPORT int JNICALL
Java_com_test_spotify_SpotifyActivity_spotifyInit( JNIEnv*  env, jobject  this,jstring     username, jstring     password,jstring cache,jstring tracefile)
{

const char* name = (*env)->GetStringUTFChars(env, username, NULL);
const char* pwd = (*env)->GetStringUTFChars(env, password, NULL);
const char* cache_location = (*env)->GetStringUTFChars(env, cache, NULL);
const char* trace_file = (*env)->GetStringUTFChars(env, tracefile, NULL);

if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "User name: %s", name);
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "Password: %s", pwd);
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "Cache %s", cache_location);
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "Trace file %s", trace_file);

return spotify_init(name,pwd,cache_location,trace_file);
}

int spotify_init(const char *username,const char *password,const char* cache_location,const char* trace_file)
{
int next_timeout = 0;
pthread_mutex_init(&notify_mutex, NULL);
pthread_cond_init(&notify_cond, NULL);

sp_session_config config;
sp_error error;
sp_session *session;

config.api_version = SPOTIFY_API_VERSION;
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "SPOTIFY_API_VERSION: %d",config.api_version);

config.cache_location = cache_location;
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "config.cache_location: %s",config.cache_location);

config.settings_location = cache_location;
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "config.settings_location: %s",config.settings_location);

config.tracefile = trace_file;
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "config.tracefile: %s",config.tracefile);

config.application_key = g_appkey;

config.application_key_size = g_appkey_size;
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "g_appkey_size: %d",config.application_key_size);

config.user_agent = USER_AGENT;
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "USER_AGENT: %s",config.user_agent);

// Register the callbacks.
config.callbacks = &callbacks;
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "defined callbacks");
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "creating session...");
error = sp_session_create(&config, &session);

if (SP_ERROR_OK != error) {
    fprintf(stderr, "failed to create session: %s\n",
                    sp_error_message(error));
    if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "error code: %d",error);
    return 2;
}
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "Session created!");

// Login using the credentials given on the command line.
error = sp_session_login(session, username, password, 0,NULL);

if (SP_ERROR_OK != error) {
    fprintf(stderr, "failed to login: %s\n",
                    sp_error_message(error));
    if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "error code: %d",error);
    return 3;
}
if(DEBUG_MODE) __android_log_print(ANDROID_LOG_INFO, "spotifyInit", "User is logged in!");

g_session = session;
//session has been created

pthread_mutex_lock(&notify_mutex);
    for (;;) {
        if (next_timeout == 0) {
            while(!notify_events)
                pthread_cond_wait(&notify_cond, &notify_mutex);
        } else {
            struct timespec ts;

    #if _POSIX_TIMERS > 0
            clock_gettime(CLOCK_REALTIME, &ts);
    #else
            struct timeval tv;
            gettimeofday(&tv, NULL);
            TIMEVAL_TO_TIMESPEC(&tv, &ts);
    #endif

            ts.tv_sec += next_timeout / 1000;
            ts.tv_nsec += (next_timeout % 1000) * 1000000;

            while(!notify_events) {
                if(pthread_cond_timedwait(&notify_cond, &notify_mutex, &ts))
                    break;
            }
        }

        // Process libspotify events
        notify_events = 0;
        pthread_mutex_unlock(&notify_mutex);

        do {
            sp_session_process_events(g_session, &next_timeout);
        } while (next_timeout == 0);

        pthread_mutex_lock(&notify_mutex);
    }
    return 0;
}

安卓活动:

public class SpotifyActivity extends Activity {
EditText logText;

static {
System.loadLibrary("spotify");
System.loadLibrary("spotifystub");
}

public native int spotifyInit(String username, String password, String cacheLocation, String traceFile);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
logText = (EditText) findViewById(R.id.editTextLog);

String cache = File.separator + "data" + File.separator + "data" + File.separator + "com.test.spotify" + File.separator + getCacheDir().getName();//set to empty string to disable cache
String traceFile = cache + File.separator + "trace_file.txt";

try {
    File dir = new File(cache);
    if (!dir.exists()) {
    dir.mkdir();
    }
    File f = new File(traceFile);
    if (!f.exists()) {
    Log.w(getClass().getSimpleName(), "trace file does not exist, creating...");
    f.createNewFile();
    }
    int response = spotifyInit("test@mail.com", "testpwd", cache, traceFile);
    if (response == 0) {
    log("Session created!, code: " + response);
    } else {
    log("Something went wrong!, code: " + response);
    }
} catch (Exception e) {
    e.printStackTrace();
}

}

private void log(String logString) {
logText.append(logString);
logText.append("\n");
}
}

所以当我运行应用程序时,这是我得到的 logcat 输出,然后是崩溃

10-30 09:27:42.390: INFO/spotifyInit(552): User name: test@mail.com
10-30 09:27:42.390: INFO/spotifyInit(552): Password: testpwd
10-30 09:27:42.390: INFO/spotifyInit(552): Cache /data/data/com.test.spotify/cache
10-30 09:27:42.390: INFO/spotifyInit(552): Trace file /data/data/com.test.spotify/cache/trace_file.txt
10-30 09:27:42.400: INFO/spotifyInit(552): SPOTIFY_API_VERSION: 12
10-30 09:27:42.400: INFO/spotifyInit(552): config.cache_location: /data/data/com.test.spotify/cache
10-30 09:27:42.400: INFO/spotifyInit(552): config.settings_location: /data/data/com.test.spotify/cache
10-30 09:27:42.400: INFO/spotifyInit(552): config.tracefile: /data/data/com.test.spotify/cache/trace_file.txt
10-30 09:27:42.400: INFO/spotifyInit(552): g_appkey_size: 321
10-30 09:27:42.400: INFO/spotifyInit(552): USER_AGENT: spotifydemo
10-30 09:27:42.400: INFO/spotifyInit(552): defined callbacks
10-30 09:27:42.400: INFO/spotifyInit(552): creating session...
10-30 09:27:42.520: INFO/DEBUG(28): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-30 09:27:42.520: INFO/DEBUG(28): Build fingerprint: 'generic/sdk/generic/:2.1-update1/ECLAIR/35983:eng/test-keys'
10-30 09:27:42.520: INFO/DEBUG(28): pid: 552, tid: 552  >>> com.test.spotify <<<
10-30 09:27:42.520: INFO/DEBUG(28): signal 11 (SIGSEGV), fault addr 000000a9
10-30 09:27:42.520: INFO/DEBUG(28):  r0 0000000b  r1 80000000  r2 80808080  r3 00000073
10-30 09:27:42.520: INFO/DEBUG(28):  r4 bec56894  r5 80dd9740  r6 000000a9  r7 bec566c8
10-30 09:27:42.520: INFO/DEBUG(28):  r8 00118638  r9 bec56888  10 001239e0  fp 00000000
10-30 09:27:42.520: INFO/DEBUG(28):  ip 006f6d65  sp bec566b8  lr 80d6b8bc  pc 80d6b8dc  cpsr 20000010
10-30 09:27:42.622: INFO/DEBUG(28):          #00  pc 0016b8dc  /data/data/com.test.spotify/lib/libspotify.so
10-30 09:27:42.622: INFO/DEBUG(28):          #01  lr 80d6b8bc  /data/data/com.test.spotify/lib/libspotify.so
10-30 09:27:42.622: INFO/DEBUG(28): code around lr:
10-30 09:27:42.622: INFO/DEBUG(28): 80d6b8ac e3560000 0a0000fd e1a00006 ebfaa9c2 
10-30 09:27:42.622: INFO/DEBUG(28): 80d6b8bc e3500c01 8a0000f9 e5d63000 e3530000 
10-30 09:27:42.622: INFO/DEBUG(28): 80d6b8cc 0a0000f6 e5946024 e3560000 0a00000d 
10-30 09:27:42.622: INFO/DEBUG(28): stack:
10-30 09:27:42.630: INFO/DEBUG(28):     bec56678  c2e19b5e  
10-30 09:27:42.630: INFO/DEBUG(28):     bec5667c  7e587492  
10-30 09:27:42.630: INFO/DEBUG(28):     bec56680  4a7d74f0  
10-30 09:27:42.630: INFO/DEBUG(28):     bec56684  57b1fb47  
10-30 09:27:42.630: INFO/DEBUG(28):     bec56688  1c3e3207  
10-30 09:27:42.630: INFO/DEBUG(28):     bec5668c  e0fcf49a  
10-30 09:27:42.630: INFO/DEBUG(28):     bec56690  47ff6b89  
10-30 09:27:42.630: INFO/DEBUG(28):     bec56694  24bc6c79  
10-30 09:27:42.630: INFO/DEBUG(28):     bec56698  bbe6e9ce  
10-30 09:27:42.641: INFO/DEBUG(28):     bec5669c  dc9e8379  
10-30 09:27:42.641: INFO/DEBUG(28):     bec566a0  58221fd4  
10-30 09:27:42.641: INFO/DEBUG(28):     bec566a4  d3460398  
10-30 09:27:42.641: INFO/DEBUG(28):     bec566a8  bec56894  [stack]
10-30 09:27:42.641: INFO/DEBUG(28):     bec566ac  80dd9740  /data/data/com.test.spotify/lib/libspotify.so
10-30 09:27:42.641: INFO/DEBUG(28):     bec566b0  df002777  
10-30 09:27:42.641: INFO/DEBUG(28):     bec566b4  e3a070ad  
10-30 09:27:42.641: INFO/DEBUG(28): #00 bec566b8  00000007  
10-30 09:27:42.641: INFO/DEBUG(28):     bec566bc  0000098c  
10-30 09:27:42.641: INFO/DEBUG(28):     bec566c0  4003c290  /dev/ashmem/mspace/dalvik-heap/zygote/0 (deleted)
10-30 09:27:42.641: INFO/DEBUG(28):     bec566c4  bec56664  [stack]
10-30 09:27:42.650: INFO/DEBUG(28):     bec566c8  7362696c  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566cc  69746f70  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566d0  74207966  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566d4  65636172  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566d8  6f726620  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566dc  3231206d  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566e0  352e312e  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566e4  38672e31  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566e8  32396336  
10-30 09:27:42.650: INFO/DEBUG(28):     bec566ec  20333462  
10-30 09:27:42.660: INFO/DEBUG(28):     bec566f0  656c6552  
10-30 09:27:42.660: INFO/DEBUG(28):     bec566f4  20657361  
10-30 09:27:42.660: INFO/DEBUG(28):     bec566f8  72646e41  
10-30 09:27:42.660: INFO/DEBUG(28):     bec566fc  2d64696f  

查看 logcat 输出,代码运行到我们想要创建会话的位置 (error = sp_session_create(&config, &session);) ,然后它崩溃了。我花了几个小时试图让这个简单的例子工作,我做错了什么?

4

1 回答 1

1

确保sp_session_config在使用之前将结构设置为 0。否则,你会得到一个充满垃圾的结构,这会导致崩溃。

例如:

sp_session_config config;
memset(&config, 0, sizeof(config));

对所有结构执行此操作是最佳实践。

于 2012-10-30T09:02:52.303 回答