最近用android开发,需要下载远程的mp3文件,但要求是用流媒体实现,也就是说不用等把远程文件全部下载完才播放,而是下载一点就播放一点,就像QQmusic里面的那种,求详细代码!
在此先谢谢各位大牛了 。。。
在此向大牛们 敬礼了
------解决方案--------------------
package com.shadow.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import com.shadow.service.AudioPlayService.LocalBinder;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
/**
* MediaPlayer does not yet support streaming from external URLs so this class provides a pseudo-streaming function
* by downloading the content incrementally & playing as soon as we get enough audio in our temporary storage.
*/
public class StreamingMediaPlayer extends Service{
private static final int INTIAL_KB_BUFFER = 96*10/8;//assume 96kbps*10secs/8bits per byte
private TextView textStreamed;
private ImageButton playButton;
private ProgressBar progressBar;
// Track for display by progressBar
private long mediaLengthInKb, mediaLengthInSeconds;
private int totalKbRead = 0;
// Create Handler to call View updates on the main UI thread.
private final Handler handler = new Handler();
private MediaPlayer mediaPlayer;
private File downloadingMediaFile;
private boolean isInterrupted;
private Context context;
private int counter = 0;
private static Runnable r;
private static Thread playerThread;
private LocalBinder localBinder = new LocalBinder();
private MediaPlayer player;
private boolean isPause = false; //播放器是否处于暂停状态
private boolean isSame = false; //所点播歌曲是否是当前播放歌曲
private Integer position = -1; //设置播放标记
private List<String> music_name; //歌曲列表
private List<String> music_path;
public StreamingMediaPlayer(Context context,TextView textStreamed, ImageButton playButton, Button streamButton,ProgressBar progressBar)
{
this.context = context;
this.textStreamed = textStreamed;
this.playButton = playButton;
this.progressBar = progressBar;