Janaka R Rajapaksha
Janaka R Rajapaksha

Reputation: 3764

use SurfaceView addCallback() in another method instead of using in onCreate()

I want to get paths for videos from server and then play them using a surfaceView.

I can do it if I use another activity class to play videos after received paths. But I am trying to play in the same activity.

I am getting paths in the background using an AsyncTask. This AsyncTask is executed in onCreate() method. After got paths in onPostExecute(), I want to play videos in SurfaceView.

But SurfaceView is created before I receive those paths. So I tried to use SurfaceView's addCallback() in onPostExecute(). But it looks not possible.

How I do this? Using SurfaceView, is there no way to play videos on the same activity after got resource paths from server?

Upvotes: 1

Views: 1714

Answers (1)

Janaka R Rajapaksha
Janaka R Rajapaksha

Reputation: 3764

Oh yeah, I found a solution. I will post it here so that it will help to someone like me. Because everywhere I check a solution for this, didn't followed this way.

Your activity class:

public class PlayActivity extends AppCompatActivity implements SurfaceHolder.Callback {
    private SurfaceHolder sh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //getResource is your AsyncTask
        //I will not post it here because it is not the problem we had
        getResource _gR = new attemptGetStream(p1, p2);
        _gR.execute();

        //initiate components
        SurfaceView sV = (SurfaceView) findViewById(R.id.main);
        assert null != sV : "Surface is null";
        sV.getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder sHolder) {
        //this is the solution.
        //only assign holder to a variable but don’t initiate MediaPlayer here
        //because we don’t have resource paths yet
        //we will initiate MediaPlayer in postExecute method and
        //attach to this holder from there
        sh = sHolder;
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        log("Surface changed");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        log("Surface destroyed");
    }

    //this is our method called from onPostExecute() in our getResource AsyncTask
    public void play(String path){
        //here initiate the MediaPlayer and assign dataSource
        //attach the player to sh(which is our surface holder
        try{
            MediaPlayer mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDisplay(sh);//sh is the surface holder
            mMediaPlayer.setDataSource(getApplicationContext(), path);//path is what we got from onPostExecute method in AsyncTask

            mMediaPlayer.prepareAsync();//i use prepareAsync method here so it will not hog the phone
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mP.start();
                }
            });
        }catch (Exception e){//use exception types as you like
            e.printStackTrace();
        }
    }


}

Upvotes: 2

Related Questions