4
A
回答
15
從服務器播放音頻文件試試這個
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://xty/MRESC/images/test/xy.mp3"
);
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}
,如果你想下載一個MP3播放文件格式服務器,然後嘗試這個..
private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL("url of your .mp3 file");
URLConnection conexion = url.openConnection();
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
也清單檔案中的使用這個權限..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
1
試試下面的代碼: -
private void PlayFile() {
try {
mp.reset();
Uri uri = "http://soundcloud.com/storynory/the-valentine-witch-mp3/download.mp3";
mp.setDataSource(this, uri);
mp.prepare();
mp.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
如果你被要求「下載MP3」的代碼,然後我會張貼代碼也。
+0
Uri!=字符串,請確認 –
相關問題
- 1. Android的 - 如何從網絡服務器
- 2. 如何從Android Studio中設置Android模擬器DNS服務器
- 3. 如何從android中的Intent服務中的服務器獲取Json響應?
- 4. Android的 - 從服務器
- 5. 從android服務器中的php服務器下載pdf
- 6. 如何在android中使用backgroundtask中的服務從服務器下載數據?
- 7. 如何從服務器中保存Android HTC中的視頻?
- 8. 如何安裝從Android中的服務器獲取的layout xml
- 9. 如何打開從服務器的PDF文件中的android
- 10. 如何將服務器上的數據從Android服務器轉換爲RecyclerView
- 11. 如何在Android中ping服務器?
- 12. 如何從android中的服務器播放mp4視頻?
- 13. 如何從android中的服務器獲取實時日期?
- 14. 如何從android中的SFTP服務器獲取文件列表?
- 15. 如何從android中的服務器下載視頻
- 16. 如何從android中獲取服務器的時間?
- 17. 如何從Android中的另一臺服務器獲取通知
- 18. 如何從'n'個服務器中識別MongoDB主服務器
- 19. 如何從服務器
- 20. 如何從FTP服務器
- 21. 如何從服務器
- 22. 如何從服務器
- 23. 如何從服務器
- 24. 如何從服務器
- 25. 如何從服務器
- 26. 如何從服務器
- 27. 如何從服務器AngularJS
- 28. 如何從服務器
- 29. 如何從服務器
- 30. 如何從服務器
[播放音頻文件](http://stackoverflow.com/search?q=android+play+audio+file)| [流音頻文件](http://stackoverflow.com/search?q=android+stream+audio) –
@謝謝你的回覆,我需要播放音頻沒有下載。 – John
@John:請看我的答案。 – user370305