gpt4 book ai didi

android - 如何在 Android 中嵌入 SoundCloud 播放器?

转载 作者:太空狗 更新时间:2023-10-29 14:08:01 26 4
gpt4 key购买 nike

我想在我的 android 应用程序中嵌入 SoundCloud 播放器以播放 SoundCloud URL。

我曾尝试使用 SoundCloud Java API 包装器。但是当我尝试获取轨道时,那个东西给我一个错误:

这一行导致错误

 HttpResponse trackResp = wrapper.get(Request.to("/tracks/60913196"));

Error - 13781-13781/ com.example.DDS.soundcloud E/Trace﹕ error opening trace file: No such file or directory (2)

如果有人在 Android 应用程序中拥有 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();
}

最佳答案

您可以在WebView中轻松播放SoundCloud的HTML5播放器,您只需要做以下操作。

//在Activity_layout.xml中

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

//在 ActivityClass.java 中

mSoundCloudPlayer =(WebView) findViewById(R.id.webview);

String VIDEO_URL = "Set Your Embedded URL";

String html = "<!DOCTYPE html><html> <head> <meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"target-densitydpi=high-dpi\" /> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link rel=\"stylesheet\" media=\"screen and (-webkit-device-pixel-ratio:1.5)\" href=\"hdpi.css\" /></head> <body style=\"background:black;margin:0 0 0 0; padding:0 0 0 0;\"> <iframe id=\"sc-widget " +
"\" width=\"100%\" height=\"50%\"" + // Set Appropriate Width and Height that you want for SoundCloud Player
" src=\"" + VIDEO_URL // Set Embedded url
+ "\" frameborder=\"no\" scrolling=\"no\"></iframe>" +
"<script src=\"https://w.soundcloud.com/player/api.js\" type=\"text/javascript\"></script> </body> </html> ";

mSoundCloudPlayer.setVisibility(View.VISIBLE);
mSoundCloudPlayer.getSettings().setJavaScriptEnabled(true);
mSoundCloudPlayer.getSettings().setLoadWithOverviewMode(true);
mSoundCloudPlayer.getSettings().setUseWideViewPort(true);
mSoundCloudPlayer.loadDataWithBaseURL("",html,"text/html", "UTF-8", "");

关于android - 如何在 Android 中嵌入 SoundCloud 播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31354754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com