Video view overlaying another Video View on Tabactivity Android

Asked

Viewed 130 times

2

I have a TabActivity with two Tabs, both have a VideoView (Man|Woman). When I start one of the Tabs and then switch to the other, the video of the first still running underneath the other video. How can I fix this?

Follow the code I’m using:

Tabactivity

public class BarraTabActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TabHost tabHost = getTabHost();

    tabHost.addTab(tabHost.newTabSpec("homem").setIndicator("Homem").setContent(new Intent(this, HomemActivity.class)));
    tabHost.addTab(tabHost.newTabSpec("mulher").setIndicator("Mulher").setContent(new Intent(this, MulherActivity.class)));
    tabHost.setCurrentTab(0);
}

Activity Man

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

    String path = "www.exemplo.com";

    Uri uri = Uri.parse(path);

    videoView = (VideoView) findViewById(R.id.vwHomem);
    videoView.setMediaController(new MediaController(this));
    videoView.setVideoURI(uri);
    videoView.requestFocus();
    videoView.start();
}

Activity Woman

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

    String path = "www.exemplo.com";

    Uri uri = Uri.parse(path);

    videoView = (VideoView) findViewById(R.id.vwMulher);
    videoView.setMediaController(new MediaController(this));
    videoView.setVideoURI(uri);
    videoView.requestFocus();
    videoView.start();
}

Layout (For Man or Woman is the same, only the names change)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <VideoView
        android:id="@+id/vwHomem" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="2sp"
        android:layout_marginTop="2sp"/>

</RelativeLayout>

2 answers

1


When tabs Homem or Mulher are created, it is good to check if the other video not belonging to it is running, if yes, pause it and leave invisible.

Woman Activity

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

    String path = "www.exemplo.com";

    vwMen   = (VideoView) findViewById(R.id.vwMulher);
    vwWoman = (VideoView) findViewById(R.id.vwHomem);

    // setMediaController, setVideoUri e RequestFocus, mas ainda não inicie-o.
}

@Override
protected void onResume (){
    super.onResume();

    if(vwMen.isPlaying()) {
        vwMen.pause();
        vwMen.suspend();
        vwMen.setVisibility(INVISIBLE);
        vwWoman.start();

    } else vwWoman.start();
}

Do it in both activity, always checking if another video is running.

0

Try calling the pause method in the Activity onPause method.

@Override
public void onPause() {
    super.onPause();
    stopPosition = videoView.getCurrentPosition(); 
    videoView.pause();
}
@Override
public void onResume() {
    super.onResume();
    videoView.seekTo(stopPosition);
    videoView.start();
}
  • Thanks for the reply, but it did not work, the video continues one over the other, even being in different activitys.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.