如何从服务器的Andr​​oid播放音频文件 [英] how to play audio file from server in android

查看:126
本文介绍了如何从服务器的Andr​​oid播放音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下网址下载音频文件并播放,在我的device.how音频文件来实现这个概念在我application.please帮我

I want to download audio file from url and play that audio file in my device.how to implement this concept in my application.please help me

感谢朋友

推荐答案

从服务器播放音频文件试试这个

to play audio file from server try this

  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文件形式的服务器,然后尝试这个。

and if you want to download a .mp3 file form server then try this..

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

也是在manifest文件使用此权限。

also in your manifest file use this permission..

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

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

这篇关于如何从服务器的Andr​​oid播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆