Send song title to spotify to start playing from android app

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

Is there any way to send a song title to the spotify app from my app so that it will start playing the song through spotify?

I tried using the bellow code i found in another code but nothing happens.

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"); 

I know shazam is able to do this.

回答1:

You are just creating an Intent, but you don't start the Intent.

add this line after setting up your intent

startActivity(intent); 

So the complete code would look like that

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); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!