Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop exo player video from playing when exit from activity android

Tags:

android

I am using Exo-Player.My video is playing fine but when i press back button and exit from activity my video keeps on playing in background.I don't how to stop it from playing when activity is finished.Please help

This is my code for exo player in recycler view adaptor

 String my_date = data.get(position).getCreated_at();
    String returneDate = setDateAndTime(my_date);
    String img_url = data.get(position).getUser().getProfileUrl();
    String videoUrl = data.get(position).getFile_url();

    if (videoUrl != null) {
        uri = Uri.parse(videoUrl);
    } else {
        holder.videoFrame.setVisibility(View.GONE);
    }

    holder.userVideoStatus.setText(data.get(position).getText());
    holder.userNameVideoStatus.setText((CharSequence) data.get(position).getUser().getName());
    holder.timeVideoStatus.setText(returneDate);

    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
    player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector);

    DefaultHttpDataSourceFactory defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory("exo player");
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    final MediaSource mediaSource = new ExtractorMediaSource(uri, defaultHttpDataSourceFactory, extractorsFactory, null, null);


    holder.playVideo.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            holder.playVideo.setVisibility(View.GONE);
            holder.userVideoUpdate.setPlayer(player);
            player.prepare(mediaSource);
            player.setPlayWhenReady(true);
            holder.volumeUp.setVisibility(View.VISIBLE);

            return true;
        }
    });
like image 566
vagish dixit Avatar asked Mar 08 '23 00:03

vagish dixit


1 Answers

Ok Pavel B. gave the right answer but I am elaborating and making it clearer...

Adapter

public class ChatsAdapter extends RecyclerView {

    public SimpleExoPlayer exoPlayer; // Do this at top

}

Activity

public class TestActivity extends AppCompatActivity {

    private ChatsAdapter chatsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        chatsAdapter = new ChatsAdapter();

    }

    @Override
    protected void onPause() {

        super.onPause();
        pausePlayer(chatsAdapter.exoPlayer);

    }

    @Override
    protected void onStop() {

        super.onStop();
        pausePlayer(chatsAdapter.exoPlayer);

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        releaseExoPlayer(chatsAdapter.exoPlayer);
    }

    @Override
    protected void onResume() {

        super.onResume();
        startPlayer(chatsAdapter.exoPlayer);
    }

    public static void startPlayer(SimpleExoPlayer exoPlayer) {

        if (exoPlayer != null) {
            exoPlayer.setPlayWhenReady(true);

        }
    }

    public static void pausePlayer(SimpleExoPlayer exoPlayer) {

        if (exoPlayer != null) {
            exoPlayer.setPlayWhenReady(false);

        }
    }

    public static void releaseExoPlayer(SimpleExoPlayer exoPlayer) {

        if (exoPlayer != null) {
            exoPlayer.release();

        }

    }
}
like image 159
DragonFire Avatar answered Mar 12 '23 12:03

DragonFire