2016-11-23 94 views
2

當我按下後退按鈕並且正在播放視頻時,我想關閉VideoView。當我按下返回按鈕時關閉VideoView中的視頻

這裏我的代碼:

public class MainActivity extends AppCompatActivity { 

    public Integer[] mVideo = { 
      R.raw.clesella, R.raw.simonino, 
      R.raw.ennio, R.raw.zip, 
      R.raw.pippo, R.raw.monituri 
    }; 

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

     final GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new VideoAdapter(this)); 

     gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       final VideoView mVideoView2 = (VideoView) findViewById(R.id.video); 
       String uriPath = "android.resource://"+ getPackageName()+"/"+mVideo[position]; 
       Uri uri2 = Uri.parse(uriPath); 
       mVideoView2.setVideoURI(uri2); 
       mVideoView2.requestFocus(); 
       mVideoView2.setVisibility(view.VISIBLE); 
       gridview.setVisibility(view.GONE); 

       mVideoView2.start(); 

       mVideoView2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

        @Override 
        public void onCompletion(MediaPlayer mp) { 
         // TODO Auto-generated method stub 

         //write your code after complete video play 
         mVideoView2.setVisibility(View.GONE); 
         gridview.setVisibility(View.VISIBLE); 
        } 
       }); 

       mVideoView2.setOnTouchListener(new View.OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent motionEvent) { 
         if (mVideoView2.isPlaying()) { 
          mVideoView2.pause(); 
         return false; 
         } 
         else { 
          mVideoView2.start(); 
          return false; 
         } 
        } 
       }); 

      } 
     }); 
    } 
} 

正如你所看到的,我從原始文件夾中的視頻,我填一個GridView。當我點擊該項目時,視頻開始播放。我想讓VideoView GONE和GridView像我在setOnCompletetionListener中一樣可見,但是當視頻正在播放時,我按下後退按鈕。

回答

0
@Override 
public void onBackPressed() { 

    if (mVideoView2.isPlaying()) { 
     mVideoView2.setVisibility(View.GONE); 
     gridview.setVisibility(View.VISIBLE); 
    } else { 
     super.onBackPressed(); 
    } 
} 
+0

感謝您的幫助。確切地說,我必須把這部分代碼放在哪裏?對於這個問題抱歉,我現在開始使用android開發。 –

+0

你應該把它放在你的'MainActivity'中。 剛剛開始輸入'onBack'並使用Ctrl +空格鍵......並且發生了魔法;) –

+0

您的活動中的任何位置。 –

1

讓你如mVideoView2gridview全球到類和再加上你不上每一次點擊創建匿名new MediaPlayer.OnCompletionListener(),只需創建一個全球性的監聽器和應用它。

class XY extends ..{ 
    GridView gridview; 
    VideoView mVideoView2 


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

     gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new VideoAdapter(this)); 
     mVideoView2 = (VideoView) findViewById(R.id.video); 
     mVideoView2.setOnCompletionListener(listner); 

     gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // other code to play and listen 
      } 
    } 

    @Override 
    public void onBackPressed() { 
     if(videoView.isPlaying()){ // check if video is playing then hide views 
      mVideoView2.setVisibility(View.GONE); 
      gridview.setVisibility(View.VISIBLE); 
      }else{ 
       super.onBackPressed(); 
      } 
     } 

     MediaPlayer.OnCompletionListener listner = 
         new new MediaPlayer.OnCompletionListener(){ 
     //... add the code here 
    } 
} 
相關問題