Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop audio playing in background in exoplayer

**

Need to implement video stream within a fragment using exoplayer. Streaming works fine but when moved from the fragment activity to other activity or if the screen is rotated, multiple copys of same audio plays in background continuously until the app is killed.how to handle this please help. Thanks in advance.

**

    public class VideoFragment extends Fragment {
    private SimpleExoPlayerView simpleExoPlayerView;
    public SimpleExoPlayer player;
    Uri mp4VideoUri;
    String TAG ="LOGG";
    SharedPrefs sharedPrefs;
    TextView notavailable;
    String url;
    Connection connection;
    long media_length;

    public VideoFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.video_fragment,container,false);
        sharedPrefs = new SharedPrefs(getContext());
        connection = new Connection(getContext());
        notavailable= (TextView) rootView.findViewById(R.id.videonotavailable);
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
        LoadControl loadControl = new DefaultLoadControl();
        player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
        simpleExoPlayerView = new SimpleExoPlayerView(getContext());
        simpleExoPlayerView = (SimpleExoPlayerView) rootView.findViewById(R.id.player_view);
        simpleExoPlayerView.setUseController(true);
        simpleExoPlayerView.requestFocus();
        simpleExoPlayerView.setPlayer(player);
        DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "exoplayer2example"), bandwidthMeterA);
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        url=sharedPrefs.getvideoUrl();
        if (url==null || url.equals("")){
            simpleExoPlayerView.setVisibility(View.GONE);
            notavailable.setVisibility(View.VISIBLE);
        }else if (connection.isInternet()){
            mp4VideoUri=Uri.parse(sharedPrefs.getvideoUrl());
            MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri,dataSourceFactory,extractorsFactory,null,null);
            final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);

            player.prepare(loopingSource);

            player.addListener(new ExoPlayer.EventListener() {
                @Override
                public void onLoadingChanged(boolean isLoading) {
                    Log.v(TAG,"Listener-onLoadingChanged...");
                }

                @Override
                public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                    Log.v(TAG,"Listener-onPlayerStateChanged...");
                }

                @Override
                public void onTimelineChanged(Timeline timeline, Object manifest) {
                    Log.v(TAG,"Listener-onTimelineChanged...");

                }

                @Override
                public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

                }

                @Override
                public void onPlayerError(ExoPlaybackException error) {
                    Log.v(TAG,"Listener-onPlayerError...");
                    player.stop();
                }

                @Override
                public void onPositionDiscontinuity() {
                    Log.v("LOG","Listener-onPositionDiscontinuity...");

                }

                @Override
                public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

                }
            });
            player.setPlayWhenReady(true);
        }else {
            simpleExoPlayerView.setVisibility(View.GONE);
            notavailable.setVisibility(View.VISIBLE);
            notavailable.setText(getString(R.string.nointernet));
        }
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        if(player != null) {
            player.stop();
            player.release();
            media_length = player.getCurrentPosition();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(player != null) {
            player.seekTo(media_length);
            player.setPlayWhenReady(true);
        }
    }
}
like image 355
Bhanu Teja Avatar asked Nov 07 '22 21:11

Bhanu Teja


1 Answers

In your fragment-holding activity, you need to handle the situation in the onPause method:

@Override
protected void onPause() {
    super.onPause();
    mPlayerFragment.pause();
}

You can define a method in your fragment to pause playback by passing false to setPlayWhenReady().

public void pausePlayback() {
        mExoPlayer.setPlayWhenReady(false); //pauses the playback if it's playing
    }
like image 176
djnotes Avatar answered Nov 15 '22 11:11

djnotes