网址流媒体播放器?

时间:2011-12-26 09:41:17

标签: android

我是Android的新手,我想从网址获取视频文件并显示视频,但我想在我的SD卡中下载文件然后播放,但我有例外。

try
        {
            Runnable r = new Runnable() {
                public void run() {
                    try 
                    {
                        //mPlayer.setDataSource(item.getStrMusicUrl());
                        //////////////////////////
                        Log.i("Music Stream","Strats");
                        URL url = new URL(item.getStrMusicUrl());
                        URLConnection cn = url.openConnection();
                        cn.connect();
                        Log.i("Music Stream","Connected");
                        InputStream stream = cn.getInputStream();
                        if (stream != null)
                        {
                            Log.i("Music Stream","Getting the Stream");
                            File temp = File.createTempFile("replicaMedia", "dat");
                            String tempPath = temp.getAbsolutePath();
                            Log.i("Music File Path",tempPath);
                            FileOutputStream out = new FileOutputStream(temp);
                            Log.i("Music Stream","Stream saved");
                            byte buf[] = new byte[16384];
                            Log.i("Music Stream","reading the saved Stream");
                            int INTIAL_KB_BUFFER = 96*10/8;
                            int totalBytesRead = 0, incrementalBytesRead = 0;
                            do {
                                int numread = stream.read(buf);
                                if (numread <= 0)
                                    break;
                                out.write(buf, 0, numread);
                                Log.i("Stream Buffer"," "+numread);
                                totalBytesRead += numread;
                                incrementalBytesRead += numread;
                                totalKbRead = totalBytesRead/1000;
                                ////  Call MP to start 
                                if(totalKbRead >= INTIAL_KB_BUFFER)
                                {
                                    totalBytesRead = 0;
                                    if(mPlayer != null && mPlayer.isPlaying())
                                    {
                                        mPlayer.pause();
                                    }

                                    Log.i("Music Stream","Setting the stream path to MP");
                                    mPlayer.setDataSource(tempPath);    
                                    mPlayer.prepare();
                                    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                    Log.i("Music Stream","Playing");
                                }

                            } while (true);

                            stream.close();                                

                        }
                        else
                        {
                            Log.i("Music Stream ","No Music Stream");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            new Thread(r).start();
        } catch (Exception e) {
            e.printStackTrace();
            if (mPlayer != null) 
            {
                mPlayer.stop();
                mPlayer.release();
            }
        }

1 个答案:

答案 0 :(得分:2)

 public class VedioViewActivity extends Activity {
 private ProgressDialog mProgress;
private VideoView mVideoView;
private String url;
private String videoName;
private boolean flag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vedio_view_xml);

    flag=false;
    url=(String)getIntent().getExtras().getString("VideoUrl");


    mVideoView=(VideoView)findViewById(R.id.vedio_view);
    //File root = Environment.getExternalStorageDirectory();

    videoName=url.substring(url.lastIndexOf("/")+1);

    File file=new File("/mnt/sdcard/");

    File []fileArr=file.listFiles();
    int i;
    for(i=0;i<fileArr.length;i++){
        if(videoName.equals(fileArr[i].toString().substring(fileArr[i].toString().lastIndexOf("/")+1))){
            flag=true;
            break;
        }
    }

    if(flag){
        try{
            flag=false;
            URL u = new URL(url);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            //Log.i("Main", "content "+c.getContentLength()+" "+fileArr[i].length());
            int fileSize=(int)fileArr[i].length();
            if(fileSize==c.getContentLength()){
                mVideoView.setVideoPath("/mnt/sdcard/"+videoName);
                mVideoView.setMediaController(new MediaController(this));
                mVideoView.start();
            }else{
                new NetCom().execute(url);
            }
            c.disconnect();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }else {
        new NetCom().execute(url);
    }
}

class NetCom extends AsyncTask<String, String, String>{

    public NetCom() {
        mProgress=new ProgressDialog(VedioViewActivity.this);
        mProgress.setIcon(R.drawable.icon_72);
        mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgress.setMessage("Buffering Video...");
        mProgress.show();
    }

    @Override
    protected String doInBackground(String... param) {

        try{

            long availableByte=0;
            URL u = new URL(param[0]);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            File root = Environment.getExternalStorageDirectory();
            FileOutputStream f = new FileOutputStream(new File(root,videoName));
            InputStream in = c.getInputStream();
            availableByte=c.getContentLength();

            int bufferSize=(int)availableByte/100;

            byte[] buffer = new byte[1024];
            int len1 = 0;
            int total=0;
            int i=1;
            while ((len1 = in.read(buffer)) > 0 ) {
                total+=len1;
                if(total>=bufferSize){
                    total=0;
                    mProgress.setProgress(i++);
                }
                f.write(buffer,0, len1);
            }
            f.flush();
            f.close();
            in.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        handler.handleMessage(null);

    }
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            try {
                mProgress.dismiss();

                 String path = "/mnt/sdcard/"+videoName;
                 mVideoView.setVideoPath(path);
                 mVideoView.setMediaController(new MediaController(VedioViewActivity.this));
                 mVideoView.start();

            }catch(Exception ex){

            }
        }
};
      }
      }