以全屏模式观看视频

时间:2013-09-10 06:28:17

标签: java javascript android

有人可以帮助我。我正在做一个用户可以观看视频的项目,我使用表面视图和媒体播放器..现在,我想要一个小屏幕播放视频并继续在大屏幕上播放..我拥有的是什么代码现在,当我点击播放按钮播放视频时,我在同一个活动上有两个不同的表面视图但是当我点击全屏时它不会继续播放,它只是从一开始播放视频。

public class MainActivity extends Activity implements OnClickListener ,  
SurfaceHolder.Callback{
//For surface
 MediaPlayer mediaPlayer, mediaPlayer_fullscreen;
 SurfaceView surfaceView, surfaceView_fullscreen;
 SurfaceHolder surfaceHolder, surfaceholder_fullscreen;
 boolean pausing = false;
 boolean pausing_2 = false;
 boolean reset = false;
 public static int media_length;


//FULL SCREEN
 Button play, pause;
 ImageButton mExit, resume;

 public static int mediaPlayer_length;


    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


   //For Surface
     getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
      surfaceHolder = surfaceView.getHolder();
      surfaceHolder.addCallback(this);
      surfaceHolder.setFixedSize(176, 144);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      mediaPlayer = new MediaPlayer();

    //FULLSCREEN

    //For Surface
        surfaceView_fullscreen =    
 (SurfaceView)findViewById(R.id.surfaceView2);
        surfaceholder_fullscreen = surfaceView_fullscreen.getHolder();
        surfaceholder_fullscreen.addCallback(this);
        surfaceholder_fullscreen.setFixedSize(300, 300);

 surfaceholder_fullscreen.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mediaPlayer_fullscreen = new MediaPlayer();

        media_length = mediaPlayer.getCurrentPosition();

}

@Override
public void onClick(View v) {

    switch(v.getId()){
   case R.id.play:
        playvideo();
    break;
   case R.id.view_fullscreen:

        mediaPlayer.release();
        linearlayout_head.setVisibility(View.GONE);

        linearlayout_fullscreen.setVisibility(View.VISIBLE);

        playvideo_fullscreen();

    break;
 }

 public void playvideo() {
    path_path = EditText_path.getText().toString();
    pausing = false;
    //reset = false;

     if(mediaPlayer.isPlaying()){
      mediaPlayer.reset();
     }

     mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
     mediaPlayer.setDisplay(surfaceHolder);

     try {
      mediaPlayer.setDataSource(getDataSource(path_path));
      Log.d("LOGPLAY", path_path);
      mediaPlayer.prepare(); 
     } catch (IllegalArgumentException e) {
      e.printStackTrace();
     } catch (IllegalStateException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     }

     mediaPlayer.start();
     //mediaPlayer.pause();
     //media_length = mediaPlayer.getCurrentPosition();
}



public void playvideo_fullscreen() {
    pausing_2 = false;
    //reset = false;

     if(mediaPlayer_fullscreen.isPlaying()){
        mediaPlayer_fullscreen.reset();
       //mediaPlayer_fullscreen.seekTo(media_length);
     }

     mediaPlayer_fullscreen.setAudioStreamType(AudioManager.STREAM_MUSIC);
     mediaPlayer_fullscreen.setDisplay(surfaceholder_fullscreen);

     try {
      mediaPlayer_fullscreen.setDataSource(getDataSource(path_path));
      Log.d("LOGPLAYFullscreen", path_path);
      mediaPlayer_fullscreen.prepare(); 
     } catch (IllegalArgumentException e) {
      e.printStackTrace();
     } catch (IllegalStateException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     }

      //mediaPlayer_fullscreen.start();

      mediaPlayer_fullscreen.seekTo(media_length);
}   

有人可以帮我这个。谢谢。

1 个答案:

答案 0 :(得分:0)

当您在两个视图之间切换时,媒体播放器将无法恢复您暂停的位置。所以你必须手动完成。您将使用

获取当前视频时间
MediaPlayer mp=new MediaPlayer();
int currentTime=mp.getCurrentPosition();

切换视图后,只需将视频搜索到'currentTime'。

mp.seekTo(currentTime);