0

I have a list view, upon clicking an item in listview it opens up a new activity. The xml of the new activity is given below:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/youtube_layout"></RelativeLayout>

<SurfaceView

    android:id="@+id/surfaceView"
    android:layout_width="match_parent"
    android:visibility="gone"
    android:layout_height="match_parent" />

<WebView
    android:id="@+id/webView1"
    android:layout_below="@+id/rel"
    android:layout_width="match_parent"
    android:visibility="gone"
    android:layout_height="match_parent"
    />
</RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeLayout"
    android:layout_alignParentBottom="true">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnPrev"
        android:text="Previous Module"
        android:onClick="prev"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnNext"
        android:text="Next Module"
        android:onClick="next"
         />
 </RelativeLayout> 

Depends on the type of item I send as intent it opens up the activity. eg. If I send item name as video it opens video in the activity. Up on clicking the next item it shows the next item of the list. On pressing previous button it shows previous item.

What my problem is, When I click the video item in the list it shows me video. That is working fine. But when I open the some other item say webview, and upon clicking next/ previous button doesn`t play the video. If I put the log I could see that surface view is getting created and then goes to surface changed and it goes to destroy immediately and goes to mediaplayer setOnCompletionListener.

        public void call(){
        webView.setVisibility(View.GONE);

        surfaceView.setVisibility(View.VISIBLE);
        youtube_layout.setVisibility(View.GONE);
        surfaceHolder=surfaceView.getHolder();
        surfaceHolder.addCallback(this);

        setController();
        pd=new ProgressDialog(this);
        pd.setMessage("Streaming...");
        pd.setIndeterminate(false);
        pd.setCancelable(true);}    
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    //pd.dismiss();
    Log.d("mlearning", "surfaceChanged");
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d("mlearning", "surfaceDestroyed");
}

  private  void setController(){
  controller = new MediaController(this);
  controller.setPrevNextListeners(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    }, new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    controller.setMediaPlayer(this);
    controller.setAnchorView(findViewById(R.id.surfaceView));
    controller.setSaveEnabled(true);
    controller.setEnabled(true);


}   
@Override
public void surfaceCreated(SurfaceHolder holder) {
    pd.show();

    try{

        mediaPlayer=new MediaPlayer();
        mediaPlayer.setDisplay(surfaceHolder);
        mediaPlayer.setDataSource(url);//Url of the video
        mediaPlayer.setDisplay(holder);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {

                return false;
            }
        });

        mediaPlayer.prepareAsync();

        mediaPlayer.setOnPreparedListener(new  MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                if (isPaused) {
                    mediaPlayer.start();
                    mediaPlayer.seekTo(length);
                    isPaused = false;
                    pd.dismiss();

                } else {

                    mediaPlayer.start();
                    controller.show();
                    pd.dismiss();
                }

            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {}}
Krishna
  • 129
  • 1
  • 2
  • 9

1 Answers1

0

You can add surface view dynamically on your view.

Example :layout.xml

<FrameLayout
    android:id="@+id/fragment_file_videoplayer_surface_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</FrameLayout>

MainActivity.java

FrameLayout fl_surfaceview_container = (FrameLayout)findViewById(R.id.fragment_file_videoplayer_surface_container);

//Add surfaceView on Framelayout
SurfaceView videoSurface = new SurfaceView(getActivity());
fl_surfaceview_container.addView(videoSurface);

//if remove or destroy surfaceview
fl_surfaceview_container.removeAllViews();
pacholik
  • 8,607
  • 9
  • 43
  • 55