0

我在单击按钮时打开一个活动,如果我单击按钮,下一个活动是从服务器播放视频,它发生黑色,5-6 秒后它开始播放视频,如何避免黑屏。?

这是我的代码:

mPreview=new SurfaceView(getApplicationContext()); 
l1.addView(mPreview); 
holder = mPreview.getHolder(); 
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
extras = getIntent().getExtras(); 

以下内容为表面混凝土法。

        path = "http://www.pixintro.com/upload/video/Benetton.mp4";
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/Benetton.mp4");
        mMediaPlayer.setDataSource(path);
        mMediaPlayer.setDisplay(holder);
        mMediaPlayer.prepare();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

public void onPrepared(MediaPlayer mediaplayer){
       mIsVideoReadyToBePlayed = true;
       if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown){
               holder.setFixedSize(mVideoWidth, mVideoHeight);
               mMediaPlayer.start();
       }
}
4

2 回答 2

2

您可以使用异步任务 。在 doInBackground() 即在后台进行处理时。您可以显示一个进度对话框(onPreExecute()),一旦任务完成(onPostExecute)就会关闭。

public class DisplayActivity extends Activity {
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
          // create surface view etc
              new Operation().execute();
}



}

 private class Operation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
    // code to be executed in background thread 

    for(int i=0;i<5;i++) {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    return "Executed";
    } 

    @Override
    protected void onPostExecute(String result) {
    // runs on UI thread and updated UI after executing doInBackground
    progressDialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
    ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");
    progressDialog.show();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
    // runs on UI thread and starts first

    }
    } 
于 2013-03-02T11:51:56.660 回答
0

看看这个界面。当媒体准备好播放时,它包含一个回调。

MediaPlayer 也有一个 prepareAsync 方法。阅读本文了解更多详情

于 2013-03-02T11:38:48.520 回答