Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中从服务器播放音频文件_Android - Fatal编程技术网

如何在android中从服务器播放音频文件

如何在android中从服务器播放音频文件,android,Android,我想从url下载音频文件并在我的设备中播放该音频文件。如何在我的应用程序中实现此概念。请帮助我 感谢朋友们从服务器播放音频文件,请尝试此操作 try { MediaPlayer player = new MediaPlayer(); player.setAudioStreamType(AudioManager.STREAM_MUSIC); player.setDataSource("http://xty/MRESC/images/test/xy.mp3"

我想从url下载音频文件并在我的设备中播放该音频文件。如何在我的应用程序中实现此概念。请帮助我


感谢朋友们从服务器播放音频文件,请尝试此操作

  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;
}
私有类下载文件扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…url){
整数计数;
试一试{
URL=新URL(“您的.mp3文件的URL”);
URLConnection conexion=url.openConnection();
conexion.connect();
//这将非常有用,以便您可以显示典型的0-100%进度条
int lenghtOfFile=conexion.getContentLength();
//下载文件
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(“/sdcard/somewhere/nameoffile.mp3”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
出版进度(整数)(总计*100/长办公室);
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){}
返回null;
}
在清单文件中也使用此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

尝试以下代码:-

private void PlayFile() {
    try {
        mp.reset();
        Uri uri = Uri.parse("http://soundcloud.com/storynory/the-valentine-witch-mp3/download.mp3");
        mp.setDataSource(this, uri); // "this" refers to context
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

如果您需要“下载MP3”代码,那么我也会发布该代码。

|@谢谢您的回复,我需要在不下载的情况下播放音频。@John:请查看我的答案。@John请查看我的答案,它会解决您的问题。Uri!=字符串,请确保什么是publishProgress();