2012年6月8日金曜日

Video再生



private MediaController mc;
private VideoView videoView;
private ProgressDialog waitDialog;
 
 String url;   //ここに再生したいビデオのURLを入れる


        try {
            //ビデオビューの生成
            videoView = (VideoView) findViewById(R.id.videoview);
            videoView.requestFocus();
            mc = new MediaController(this);
            videoView.setMediaController(mc);
            mc.show(0); //常にコントローラ表示 ==なぜか効かない!

            //プログレスダイアログ(くるくる回る)
            waitDialog = new ProgressDialog(this);
            waitDialog.setMessage("読込中... しばらくお待ちください");
            waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            waitDialog.show();

            //ローディング終了
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
       public void onPrepared(MediaPlayer arg0) {
      // 読込終わったらプログレスダイアログ終了
            if(waitDialog != null){
                waitDialog.dismiss();
                waitDialog = null;
            }
      }
    });

            //エラー
            videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
       public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
             if(waitDialog != null){
                 waitDialog.dismiss();
                 waitDialog = null;
             }
        return false;
       }
     });
         
             
            //再生終了処理
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
               public void onCompletion(MediaPlayer arg0) {
                 finish();
              }            
            });

          
            //動画の再生      
            videoView.setVideoURI(Uri.parse(url));
            videoView.start();
         
        } catch (Exception e) {
            android.util.Log.e("",e.toString());
            if(waitDialog != null){
                waitDialog.dismiss();
                waitDialog = null;
            }
        }


Layout xml

        <VideoView
        android:id="@+id/videoview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />


     

0 件のコメント:

コメントを投稿