2012-10-30 2 views
0

제공된 Spotify 스텁 예제를 android에서 실행하려고하는데 세션을 만들 때 기본 코드가 '충돌'한 것 같습니다. 다음은 내 코드의 모습입니다.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("[email protected]", "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"); 
} 
} 

그래서 이것은 내가 보면 충돌

10-30 09:27:42.390: INFO/spotifyInit(552): User name: [email protected] 
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 0e0 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 (& 설정, & 세션);), 충돌이 발생합니다. 나는이 간단한 예제를 작동 시키려고 몇 시간을 보냈다. 내가 뭘 잘못하고 있니?

답변

1

사용하기 전에 sp_session_config 구조체를 0으로 memset해야합니다. 그렇지 않으면 충돌을 일으키는 쓰레기로 채워진 구조체가 생성됩니다. 예를 들어

는 :

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

그것은 모든 구조체에 대해이 작업을 수행하는 가장 좋은 방법입니다.

+0

감사합니다. 작동합니다! – kev

관련 문제