1

I am not sure where I have missed something, I expect the playPauseButton to respond when I click it but nothing happens when I do. I have noticed some errors in my debug console log, which are mostly related to mediaPlayer, but I don't exactly know what they mean, since an quite new to android programming! Below is my debug console log, and a bit of related code:

// Removed Logcat!!

Some code related from a class Player.java:

import android.media.MediaPlayer;
import android.media.AudioManager;
import android.util.Log;

import java.io.IOException;

public class Player {

    // Creating new MediaPlayer

    MediaPlayer mediaPlayer = new MediaPlayer();

    // Creating a public static player as reference to this Player class

    public static Player player;
    String url = "";

    public Player () {
        this.player = this;
    }

    public void playStream (String url) {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.stop();
            } catch (Exception e) {

            }

            // Releasing everything from the mediaPlayer

            mediaPlayer = null;
        }

        // Creating new Media Player

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        // Try & Catch errors

        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void pausePlayer () {
        try {
            mediaPlayer.pause();
        } catch (Exception e) {
            Log.d("EXCEPTION", "Failed to pause Media Player");
        }
    }

    public void playPlayer () {
        try {
            mediaPlayer.start();
        } catch (Exception e) {
            Log.d("EXCEPTION", "Failed to start Media Player");
        }
    }

    public void togglePlayer () {
        try {
            if (mediaPlayer.isPlaying())
                pausePlayer();
            else
                playPlayer();
        } catch (Exception e) {
            Log.d("Exception", "Failed to toggle Media Player");
        }
    }
}

the other related code from MainActivity.java

public class MainActivity extends AppCompatActivity {
    static FloatingActionButton playPauseButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        playPauseButton = (FloatingActionButton) findViewById(R.id.fab);
        playPauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        // Songs location on our server

        String url = // "*My website/file.mp3";

        // Passing above url to our MediaPlayer

        if (Player.player == null)
            new Player();
        Player.player.playStream(url);
    }

    public static void flipPlayPauseButton (boolean isPlaying) {
        if (isPlaying) {
            playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
        } else {
            playPauseButton.setImageResource(android.R.drawable.ic_media_play);
        }
    }
MMG
  • 3,226
  • 5
  • 16
  • 43
Klem Lloyd Mwenya
  • 388
  • 1
  • 6
  • 17

2 Answers2

1

Try for example:

  playPauseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
if(video.isplaying()){pause function()}
else{play function()}

                }
            });
MMG
  • 3,226
  • 5
  • 16
  • 43
0

Okay, I'd like to thank MohammadMoeinGolchin, who has helped me figure this out. It so happen's that I did not complete my function inside of my public void onClik(View view) function. But you will see that is is written somewhere in a separate Player.java class. So to resolve this, what I did instead, is to call the togglePlayer() function inside my onClick(View view) like this:

        @Override
        public void onClick (View view) {
           Player.player.togglePlayer();
        }
Klem Lloyd Mwenya
  • 388
  • 1
  • 6
  • 17
  • Your welcome, it is correct but you are invoking togglePlayer function and you should set commands in that function. – MMG May 06 '20 at 10:17