Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

play or pause function not working for my app between two classes

hey guys i'm finding difficult to get the button to work in my android simulator, i got the icons to change but when playing but the button does not respond when clicked and really doesn't necessarily give me anything credible to fix it in my compiler, really need your proffessional help, i'm new to android studio

Player class

package com.gyro.streamline;

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

import java.io.IOException;

public class Player {

MediaPlayer mediaPlayer = new MediaPlayer();
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) {

        }
        mediaPlayer = null;
    }
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                playPlayer();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                MainActivity.flipPlayPauseButton(false);
            }
        });
        mediaPlayer.prepareAsync();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

public void pausePlayer () {
    try{
    mediaPlayer.pause();
    MainActivity.flipPlayPauseButton(false);
}
catch (Exception e) {
    Log.d("EXCEPTION", "failed to pause media player");
}
}

public void playPlayer () {
    try{
        mediaPlayer.start();
        MainActivity.flipPlayPauseButton(true);
    }
    catch (Exception e) {
        Log.d("EXCEPTION", "failed to play media player");
    }
}

public void togglePlayer () {
    try{
       if (mediaPlayer.isPlaying())
           pausePlayer();
       else
           playPlayer();
    }
    catch (Exception e) {
        Log.d("EXCEPTION", "failed to toggle media player");
    }
   }

}

This is my Mainactivity and most of my main functionality is here, please help me guys. i've really tried my best

package com.gyro.streamline;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.media.MediaPlayer;
import android.media.AudioManager;
import java.io.IOException;
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 =  findViewById(R.id.fab);
    playPauseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    String url = "http://www.naijaloaded.com.ng/wp-content/uploads/2014/09/PSquare_Ft_Don_Jazzy_-_Collabo_Naijaloaded.com_.ng_.mp3";
    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);
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
    }
}

Comments