在视频之前显示进度对话框

发布于 2024-10-16 08:58:19 字数 4708 浏览 2 评论 0原文

我试图使用以下代码在视频之前显示 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;
        }
    }


}

但我在这里没有看到进度条。我直接得到的只是视频。

感谢所有帮助。

I am trying to displaying a ProgressDialog before a video with the following code:

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;
        }
    }


}

but I do not get the progress bar here. all I get is the the video directly.

All help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情旳空白 2024-10-23 08:58:20

您必须放弃同步 MediaPlayer 调用才能执行此操作。 MediaPlayer 有一系列播放器状态更改回调,例如 onPreparedListener 您需要用于确定何时显示对话框。

您所遇到的情况是由于以下事实:虽然您将工作放入 uiThread,但您基本上将所有工作都放入了 uiThread。

调用:

            mVideoView.setVideoPath(getDataSource(path));

正在 uiThread 上进行(貌似)。您需要使用异步侦听器来避免自己构建这些设施。

You'll have to move away from the synchronous MediaPlayer calls to do this. The MediaPlayer has series of player state change callbacks such as onPreparedListener which you'll need to make use of to determine when to display your dialog.

What you are experiencing is due to the fact that, although you are putting work to the uiThread, you are basically putting all your work to the uiThread.

The call:

            mVideoView.setVideoPath(getDataSource(path));

is going on the uiThread (seemingly). You need to use the async listeners to avoid having to build those facilities for yourself.

吻风 2024-10-23 08:58:19

不要在 runOnUiThread 中关闭进度对话框,而是在 onPreparedListener 中关闭它:

mVideoView.setOnPreparedListener(new OnPreparedListener() {

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

立即调用 runOnUiThread,因此在对话框可见之前将其关闭。此代码将使其继续运行,直到视频准备好开始为止。

Instead of dismissing the progress dialog in runOnUiThread, dismiss it in the onPreparedListener:

mVideoView.setOnPreparedListener(new OnPreparedListener() {

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

runOnUiThread is called right away, therefore dimissing the dialog before it can be seen. This code will keep it going until the video is ready to start.

梦言归人 2024-10-23 08:58:19

你应该使用 AsyncTask 来做到这一点。看看这个页面。它为您提供有关如何做您想做的事情的详细信息。

you should use an AsyncTask to do that. have a look on this page. it gives you detailed information on how to do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文