2012-08-22 36 views
5

Tôi đang cố phát video từ url phát trực tuyến. mã như sauVấn đề về xem video trên Android 4.0 trở lên

public class VideoPlayer extends Activity 
{ 
    private VideoView mVideoView;    
    String videoURL=""; 
    static Utility utility; 
    static Context context; 
    MediaController mediaController; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    {   
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.video_player);   
     setupViews(); 
    } 

    private void setupViews() 
    { 
     context=VideoPlayer.this; 
     utility=new Utility(VideoPlayer.this); 
     isActivityisRunning=true; 
     showProgressDialog("Loading video.."); 
     videoURL=getIntent().getExtras().getString("url");    
     mVideoView=(VideoView)findViewById(R.id.video_view); 
     mediaController=new MediaController(context);    
     mVideoView.setMediaController(mediaController); 

     mVideoView.setOnPreparedListener(new OnPreparedListener() 
     { 
      @Override 
      public void onPrepared(MediaPlayer mp) 
      { 
       hideProgressDialog(); 

       if(bIsOnPausedCalled) 
        mVideoView.seekTo(LastDuration); 
        mVideoView.start();      
        mVideoView.requestFocus(); 
        bIsOnPausedCalled=false; 
        LastDuration=0; 
       } 
      }); 

      mVideoView.setOnCompletionListener(new OnCompletionListener() 
      { 

       @Override 
       public void onCompletion(MediaPlayer mp) 
       { 
        finish(); 
       } 
      }); 


      mVideoView.setOnErrorListener(new OnErrorListener() 
      { 

      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) 
      { 
       utility.hideProgressDialog(); 
       return false; 
      } 
     }); 

      playVideoFile();  
     } 

    public static void showDialog(String Message) 
    {  
       alertDialog = new AlertDialog.Builder(context).create(); 
       alertDialog.setTitle(Constant.DialogTitle); 
       alertDialog.setMessage(Message); 
       alertDialog.setCancelable(false); 
       alertDialog.setButton("Ok", new DialogInterface.OnClickListener() 
       { 
          public void onClick(DialogInterface dialog, int which) 
          {    
          dialog.dismiss();       
          } 
       }); 
       if(isActivityisRunning) 
        alertDialog.show(); 
       else 
        utility.showToast(Message); 
    } 

    static ProgressDialog progressDialog; 
    static AlertDialog alertDialog; 

    public void showProgressDialog(String Message) 
    { 
      hideProgressDialog(); 
      progressDialog=new ProgressDialog(context); 
      progressDialog.setTitle(Constant.DialogTitle); 
      progressDialog.setMessage(Message);       
      if(isActivityisRunning) 
       progressDialog.show(); 
      else 
       utility.showToast(Message); 
    } 

    public static boolean isActivityisRunning; 
    public static boolean showProgressDialog; 


    public static boolean bIsOnPausedCalled=false; 
    public static int LastDuration=0; 

     @Override 
     protected void onPause() 
     { 

      hideProgressDialog(); 
      bIsOnPausedCalled=true; 
      isActivityisRunning=false; 
      if (mVideoView != null) 
      {  
       if(LastDuration==0) 
       { 
        LastDuration=mVideoView.getCurrentPosition(); 
        mVideoView.suspend(); 
        mVideoView.setVisibility(View.GONE);  
       }    
      } 
      super.onPause(); 
     } 

     @Override 
     protected void onResume() 
     { 

      isActivityisRunning=true; 
       if(bIsOnPausedCalled) 
       {      
        setupViews();    
       } 
       super.onResume();  
     } 

     @Override 
     protected void onDestroy() 
     { 
      super.onDestroy(); 
      try 
      {    
       if (mVideoView != null) 
       { 
        mVideoView.stopPlayback(); 
        mVideoView=null;     
        hideProgressDialog(); 
        isActivityisRunning=false; 
        bIsOnPausedCalled=false; 
        LastDuration=0; 
       } 

      } catch (Exception e) 
      {}    
     } 


    public static void hideProgressDialog() 
    { 
     if(progressDialog!=null) 
     { 
      if(progressDialog.isShowing()) 
      { 
       progressDialog.dismiss(); 
      }   
     } 
    } 


    private void playVideoFile() 
     { 

       try 
       {     
        mVideoView.setVideoURI(Uri.parse(videoURL)); 
       } 
       catch (Exception e) 
       { 
        utility.hideProgressDialog(); 
        if (mVideoView != null) 
        {     
         mVideoView.stopPlayback(); 
        } 
       } 
      } 

Điều này hoạt động tốt trên thiết bị Android có phiên bản dưới 4.0 (với tạm dừng và tiếp tục với nút chế độ ngủ). nhưng khi tôi cố gắng phát video trên điện thoại Android có phiên bản4.0 hoặc mới hơn video sẽ phát tốt nhưng khi điện thoại chuyển sang chế độ nghỉ và tiếp tục quay lại chế độ ngủ kích thước lượt xem video trở thành một nửa màn hình. như sau enter image description here

vui lòng trợ giúp?

Cảm ơn trước .....

Trả lời

0

bạn đang làm là đúng tất cả nhưng bạn đã áp dụng setContentView (R.layout.video_player); chỉ những cái duy nhất, mặc dù video được tiếp tục. do đó, mã nên

@Override 
public void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);     
    setupViews(); 
} 

private void setupViews() 
{ 
    setContentView(R.layout.video_player); 
    context=VideoPlayer.this; 
    ----- 
    ------ 
} 

Thay vì

@Override 
public void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.video_player);  
    setupViews(); 
} 

private void setupViews() 
{ 
    context=VideoPlayer.this; 
    ----- 
    ------ 
} 
+0

Cảm ơn người đàn ông .... gr8 –

Các vấn đề liên quan