Search code examples
androidfragmentmediacontroller

MediaController inside Fragment


I have an example of setting the Media Controller from Main Activity, but i expended my Activity with 3 fragments. Everything works except Media Controller is not displayed on my Fragment. How do I set the Media Controller with Fragment? This is from Activity.

private void setController() {
    controller = new MusicController(this);
    // set previous and next button listeners
    controller.setPrevNextListeners(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playNext();
        }
    }, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playPrev();
        }
    });
    // set and show
    controller.setMediaPlayer(this);
    //getSupportFragmen

    controller.setAnchorView(findViewById(R.id.songList));
    controller.setEnabled(true);

Solution

  • You have to place that code inside your onCreateView() Fragment method, so your MusicController is inflated in your Fragment layout.