2011-12-12 84 views
5

是否有任何方法將歌曲標題從我的應用程序發送到Spotify應用程序,以便它可以通過Spotify播放歌曲?將歌曲標題發送到Spotify以開始從Android應用程序播放

我嘗試使用我在另一個代碼中找到的波紋管代碼,但沒有任何反應。

Intent intent = new Intent(Intent.ACTION_MAIN); 
       intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH); 
       intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher")); 
       intent.putExtra(SearchManager.QUERY, "michael jackson smooth criminal"); 

我知道shazam能夠做到這一點。

回答

7

你只是創建一個意圖,但你不啓動意圖。

加入這一行設置你的意圖

startActivity(intent); 

所以完整的代碼看起來像

Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH); 
intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher")); 
intent.putExtra(SearchManager.QUERY, "michael jackson smooth criminal"); 
try { 
    startActivity(intent); 
}catch (ActivityNotFoundException e) { 
    Toast.makeText(context, "You must first install Spotify", Toast.LENGTH_LONG).show(); 
    Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=com.spotify.mobile.android.ui")); 
    startActivity(i); 
} 
+0

這樣一個愚蠢的錯誤,我不能相信我錯過了,謝謝了! – Peter