Android播放来自Url surfaceview Progressdialog的视频

时间:2014-05-06 11:26:53

标签: android surfaceview progressdialog

我正在使用surfaceview。我可以从URL加载视频,我可以播放此视频。现在我想使用ProgressDialog.i想要显示ProgressDialog,同时开始视频。这是我的源码。我可以添加这个吗? P.s这段代码工作正常,但我想使用progressdialog

public class MainActivity extends Activity implements
    OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
    OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl {
String url = "http://www.pocketjourney.com/downloads/pj/video/famous.3gp";
MediaPlayer mMediaPlayer;
SurfaceView mSurfaceView;
SurfaceHolder holder;
private static ProgressDialog progressDialog;

MediaController mcontroller;
Handler handler;
SimpleSideDrawer slide_me;

String log = "";


Button b;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    progressDialog = ProgressDialog.show(MainActivity.this, "",
            "Loading.....", true);
    b = (Button) findViewById(R.id.slide);
    slide_me = new SimpleSideDrawer(this);
    slide_me.setLeftBehindContentView(R.layout.activity_slide_menu);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            slide_me.toggleLeftDrawer();

        }
    });
    SurfaceView v = (SurfaceView) findViewById(R.id.screen_tutorial_video_surface);
    handler = new Handler();
    holder = v.getHolder();
    holder.addCallback(this);
    playVideo();

    ArrayList<ItemDetails> image_details = ArrayItemDetails
            .GetSearchResults();

    ListView lv1 = (ListView) findViewById(R.id.listV_main);
    lv1.setAdapter(new ItemListBaseAdapter(this, image_details));



}

private void playVideo() {
    try {
        mcontroller = new MediaController(this);
        mcontroller.setVisibility(View.INVISIBLE);

        mMediaPlayer = MediaPlayer.create(this, Uri.parse(url));
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);

        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.setOnVideoSizeChangedListener(this);

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    handler = new Handler();
    mMediaPlayer.setDisplay(holder);
    try {
        progressDialog.dismiss();
        mMediaPlayer.start();

    } catch (IllegalStateException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {

}

@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    // TODO Auto-generated method stub
}

@Override
public void onPrepared(MediaPlayer mp) {

    mcontroller.setMediaPlayer(this);
    mcontroller
            .setAnchorView(findViewById(R.id.screen_tutorial_video_surface));
    mcontroller.setEnabled(true);

    handler.post(new Runnable() {
        public void run() {
            mcontroller.show();
        }
    });
}

@Override
public void onCompletion(MediaPlayer mp) {

}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {

}

public void start() {
    mMediaPlayer.start();
}

public void pause() {
    mMediaPlayer.pause();
}

public int getDuration() {
    return mMediaPlayer.getDuration();
}

public int getCurrentPosition() {
    return mMediaPlayer.getCurrentPosition();
}

public void seekTo(int i) {
    mMediaPlayer.seekTo(1);
}

public boolean isPlaying() {
    return mMediaPlayer.isPlaying();
}

public int getBufferPercentage() {
    return 0;
}

public boolean canPause() {
    return true;
}

public boolean canSeekBackward() {
    return true;
}

public boolean canSeekForward() {
    return true;
}


}

2 个答案:

答案 0 :(得分:0)

progressDialog方法中显示onPlay(),并在onPrepared()方法中将其关闭。 在onCreate()中声明progressDialog。

progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading");

在onPlay()中显示progressDialog

progressDialog.show();

在onPrepared()中,当准备工作完成并且视频已准备就绪时,取消了progressDialog。

progressDialog.dismiss();

答案 1 :(得分:0)

我不知道(并试图在此之前找到它一段时间)一个独特的聆听者,以获得进展,例如当前字节和总字节数。但您可以使用MediaPlayer.OnBufferingUpdateListener来执行此操作。

在MediaPlayer的文档中,您可以找到它:

  

处于“已启动”状态时,内部播放器引擎会调用用户   提供了OnBufferingUpdateListener.onBufferingUpdate()回调方法   如果事先已通过注册OnBufferingUpdateListener   setOnBufferingUpdateListener(OnBufferingUpdateListener)。这个回调   允许应用程序跟踪缓冲状态   流音频/视频。

详见:http://developer.android.com/reference/android/media/MediaPlayer.html

因此,如果您实施MediaPlayer.OnBufferingUpdateListener,该听众的percent参数可以为您提供帮助。

  

百分比已缓冲内容的百分比(0-100)   或到目前为止播放

详见:http://developer.android.com/reference/android/media/MediaPlayer.OnBufferingUpdateListener.html#onBufferingUpdate(android.media.MediaPlayer, int)

希望我帮助过你。