我也嘗試過使用webview的嵌入式播放器解決方案,但這不起作用。
現在我正在使用Soundcloud Java API Wrapper,並且工作正常。 按照GitHub回購指示實施API:https://github.com/soundcloud/java-api-wrapper
該代碼然後是非常簡單的。你只需要一個客戶端ID和一個客戶端密碼,都必須在soundcloud開發者網站上獲得。
然後,該代碼是真的簡單:
String id = getResources().getString(R.string.sc_client_id);
String secret = getResources().getString(R.string.sc_client_secret);
ApiWrapper wrapper = new ApiWrapper(id,secret, null, null);
try {
//Only needed for user-specific actions;
//wrapper.login("<user>", "<pass>");
//HttpResponse resp = wrapper.get(Request.to("/me"));
//Get a track
HttpResponse trackResp = wrapper.get(Request.to("/tracks/60913196"));
//Track JSON response OK?
if(trackResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
JSONObject trackJSON = new JSONObject(EntityUtils.toString(trackResp.getEntity()));
//If track is streamable, fetch the stream URL (mp3-https) and start the MediaPlayer
if(trackJSON.getBoolean("streamable"))
{
HttpResponse streamResp = wrapper.get(Request.to("/tracks/60913196/stream"));
JSONObject streamJSON = new JSONObject(EntityUtils.toString(streamResp.getEntity()));
String streamurl = streamJSON.getString("location");
Log.i("SoundCloud", trackJSON.getString("streamable"));
Log.i("SoundCloud", streamurl);
m_soundcloudPlayer.stop();
m_soundcloudPlayer = new MediaPlayer();
m_soundcloudPlayer.setDataSource(streamurl);
m_soundcloudPlayer.prepare();
m_soundcloudPlayer.start();
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
目的m_soundcloudPlayer
是android.media.MediaPlayer
。
請詳細說明什麼沒有工作,什麼工作。 – Cheesebaron 2012-01-05 12:15:04
只工作過一次,但我沒有拿出這首歌。也許這是更好的解決方案。 – realuser 2012-01-05 12:29:54