2011-02-07 9 views
2

前progressdialogを表示:私は次のコードでのビデオの前にProgressDialogを表示しようとしていたビデオ

package com.Boodang; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.URLUtil; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.VideoView; 

public class VideoLoading extends Activity implements Runnable { 
    private static final String TAG = "VideoViewDemo"; 

    private VideoView mVideoView; 
    private EditText mPath; 
    private ImageView mPlay; 
    private ImageView mPause; 
    private ImageButton mReset; 
    private ImageView mStop; 
    private String current; 
    private ProgressDialog dialog; 
    private Context mContext; 
    String s=null; 
    @Override 
    public void onCreate(Bundle state){ 

    super.onCreate(state); 


    dialog=ProgressDialog.show(this,"","Loding.PleaseWait",true); 
    Thread t=new Thread(this); 
    t.start(); 
     Log.e("VideoPlay1 page","OnStart"); 
     setContentView(R.layout.videoplay); 


     mPlay = (ImageView) findViewById(R.id.play); 
     mPause = (ImageView) findViewById(R.id.pause); 

     mStop = (ImageView) findViewById(R.id.stop); 

     mPlay.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 

      } 
     }); 
     mPause.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mVideoView != null) { 
        mVideoView.pause(); 
       } 
      } 
     }); 

     mStop.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       if (mVideoView != null) { 
        current = null; 
        mVideoView.stopPlayback(); 
       } 
      } 
     }); 
    runOnUiThread(new Runnable(){ 
     public void run() { 
       dialog.dismiss(); 
      } 

     }); 


    } 
    public void run(){ 
     Message msg; 
     handler.sendEmptyMessage(0); 
    } 
Handler handler=new Handler(){ 
@Override 
public void handleMessage(Message msg) { 
     try { 

      mVideoView = (VideoView) findViewById(R.id.video); 
      s=getIntent().getStringExtra("id"); 

      final String path = s; 
      Log.v(TAG, "path: " + path); 
      if (path == null || path.length() == 0) { 
      Toast.makeText(VideoLoading.this, "File URL/path is empty", 
         Toast.LENGTH_LONG).show(); 

      } else { 
       // If the path has not changed, just start the media player 
       if (path.equals(current) && mVideoView != null) { 
        mVideoView.start(); 
        mVideoView.requestFocus(); 
       return; 
      } 
      current = path; 
       mVideoView.setVideoPath(getDataSource(path)); 
       mVideoView.start(); 
       mVideoView.requestFocus(); 


      } 
     } catch (Exception e) { 
      Log.e(TAG, "error123: " + e.getMessage(), e); 
      if (mVideoView != null) { 
       mVideoView.stopPlayback(); 
      } 
     } 

    } 
}; 
    private String getDataSource(String path) throws IOException { 
     if (!URLUtil.isNetworkUrl(path)) { 
      return path; 
     } else { 
      URL url = new URL(path); 
      URLConnection cn = url.openConnection(); 
      cn.connect(); 
      InputStream stream = cn.getInputStream(); 
      if (stream == null) 
       throw new RuntimeException("stream is null"); 
      File temp = File.createTempFile("mediaplayertmp", "dat"); 
      temp.deleteOnExit(); 
      String tempPath = temp.getAbsolutePath(); 
      FileOutputStream out = new FileOutputStream(temp); 
     byte buf[] = new byte[128]; 
      do { 
      int numread = stream.read(buf); 
       if (numread <= 0) 
        break; 
       out.write(buf, 0, numread); 


      } while (true); 

      try { 
       stream.close(); 

      } catch (IOException ex) { 
       Log.e(TAG, "error: " + ex.getMessage(), ex); 
      } 
      return tempPath; 
     } 
    } 


} 

が、私はここにプログレスバーを得ることはありません。私が得るのはビデオだけです。

すべてのご協力をいただきありがとうございます。

答えて

4

代わりrunOnUiThreadにプログレスダイアログを却下の、onPreparedListenerでそれを却下:

mVideoView.setOnPreparedListener(new OnPreparedListener() { 

    public void onPrepared(MediaPlayer arg0) { 
     dialog.dismiss(); 
     mVideoView.start(); 
    } 
}); 

runOnUiThreadは、それが見ることができる前に、そのためのダイアログをdimissing、すぐに呼ばれています。このコードは、ビデオを開始する準備が整うまで継続します。

+0

あなたは私を助けました。多くのことをありがとう...進捗ダイアログが表示されない理由を理解しようとしていました。 – Yogamurthy

0

これを行うには、AsyncTaskを使用する必要があります。 this pageをご覧ください。それはあなたが望むことを行う方法に関する詳細な情報を提供します。

0

これを行うには、同期MediaPlayerコールから離れる必要があります。 MediaPlayerには、onPreparedListenerのような一連のプレーヤー状態変更コールバックがあり、ダイアログをいつ表示するかを決定するために使用する必要があります。

あなたが経験していることは、あなたがuiThreadに作業をしているにもかかわらず、基本的にすべての作業をuiThreadに入れていることにあります。

コール:

  mVideoView.setVideoPath(getDataSource(path)); 

は(一見)uiThread上で起こっています。これらの機能を自分で構築する必要がないように、非同期リスナーを使用する必要があります。

関連する問題