PlayPauseButton | Animated Play & Pause Button | iOS library
kandi X-RAY | PlayPauseButton Summary
kandi X-RAY | PlayPauseButton Summary
Animated Play & Pause Button, subclass of UIButton written in Swift. .
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of PlayPauseButton
PlayPauseButton Key Features
PlayPauseButton Examples and Code Snippets
Community Discussions
Trending Discussions on PlayPauseButton
QUESTION
I'm trying to adapt someone's code from codepen.io that made this pretty visual audio player playlist. My goal I want to achieve is to have multiple of these on a single page since the original code only supported one. I'm trying to figure out how to get these audio players to behave on their own accord rather than, when I press play on one it will pause the previous or vice versa. At the moment, the problem is that they're all sharing the same audio controls and I'm trying to figure out a way to target each audio player uniquely.
Been trying at this for a couple days so I'm reaching out for help. My initial thought which you'll see in the codepen.io below was to wrap each div of the audio player in its own unique div. Then in JS, call an array of the divs and with forEach iterate the JS function for the audio playlist on each div. I know there is something wrong with my functions or perhaps my approach in general and I would love to have some feedback and direction.
Currently, I have just two audio players to start with so I can even see if my idea is working. Right now their both accessing the same songs/album covers, however I just wanted to figure out how to get them to play differently first before figuring out the rest of the stuff haha. So apologies if you notice that problem too.
2 Audio Players Independently -- Codepen.io
Lastly, I'm implementing this with some AHK scripts & Edge/IE, so I'm forced for the time being to use Legacy JS.
Thank you. Cheers!
Here is the code for the JS in case you don't want to visit codepen.io.
...ANSWER
Answered 2021-Aug-31 at 14:45Alright, I've figured out why the players won't play independently, and it seems to be related to some type of JS scoping issue, and how the audio variable was being assigned in the init
function. I'm still not exactly sure why, but it seems as every audio player created was referencing the same exact audio instance.
If you change two things, then it should work for you.
First, at the very beginning of the immediately invoked function expression, declare an audio variable, set to nothing.
QUESTION
I am trying to declare a ClientFunction on a Factory page and then call it on a test page. But I am doing something wrong and it doesn’t work. I have two pages one is factory page, second test page. In test page i have ClientFunction and it works fine. When i trying move to Factory Page it doesn't working.
...ANSWER
Answered 2021-May-19 at 11:02You can do this:
QUESTION
public class SongPlayerActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener {
static MediaPlayer mediaPlayer;
TextView songName, artistName, albumName, durationPlayed, totalDuration;
ImageView next, previous, songImage, shuffle, repeat;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
SeekBar seekBar;
FloatingActionButton floatingActionButton;
List songList = new ArrayList<>();
Handler handler = new Handler();
Thread playPauseThread, nextThread, previousThread;
private Uri uri;
private int position;
boolean shuffleBoolean, repeatBoolean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song_player);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
songList = getIntent().getParcelableArrayListExtra("songsList");
position = getIntent().getIntExtra("position", 0);
songImage = findViewById(R.id.album_cover);
songName = findViewById(R.id.music_name);
albumName = findViewById(R.id.album_name);
artistName = findViewById(R.id.artist_name);
seekBar = findViewById(R.id.seek_bar);
durationPlayed = findViewById(R.id.played_duration);
totalDuration = findViewById(R.id.music_duration);
repeat = findViewById(R.id.repeat);
shuffle = findViewById(R.id.shuffle);
floatingActionButton = findViewById(R.id.play_pause);
next = findViewById(R.id.next);
previous = findViewById(R.id.previous);
playMusic();
mediaPlayer.setOnCompletionListener(this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaPlayer != null && fromUser) {
mediaPlayer.seekTo(progress * 1000);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
SongPlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(currentPosition);
durationPlayed.setText(durationConversion(currentPosition));
}
handler.postDelayed(this, 200);
}
});
if (sharedPreferences.getBoolean("playerShuffle", false)) {
shuffle.setImageResource(R.drawable.ic_baseline_shuffle_24);
shuffleBoolean = true;
}
else {
shuffle.setImageResource(R.drawable.ic_baseline_shuffle_off_24);
shuffleBoolean = false;
}
if (sharedPreferences.getBoolean("playerRepeat", false)) {
repeat.setImageResource(R.drawable.ic_baseline_repeat_24);
repeatBoolean = true;
}
else {
repeat.setImageResource(R.drawable.ic_baseline_repeat_24_off);
repeatBoolean = false;
}
shuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sharedPreferences.getBoolean("playerShuffle", false)) {
editor = sharedPreferences.edit();
editor.putBoolean("playerShuffle", false);
editor.commit();
shuffleBoolean = false;
shuffle.setImageResource(R.drawable.ic_baseline_shuffle_off_24);
Toast.makeText(getApplicationContext(), R.string.shuffle_off, Toast.LENGTH_SHORT).show();
} else {
editor = sharedPreferences.edit();
editor.putBoolean("playerShuffle", true);
editor.commit();
shuffleBoolean = true;
shuffle.setImageResource(R.drawable.ic_baseline_shuffle_24);
Toast.makeText(getApplicationContext(), R.string.shuffle_on, Toast.LENGTH_SHORT).show();
}
}
});
repeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sharedPreferences.getBoolean("playerRepeat", false)) {
editor = sharedPreferences.edit();
editor.putBoolean("playerRepeat", false);
editor.commit();
repeatBoolean = false;
repeat.setImageResource(R.drawable.ic_baseline_repeat_24_off);
Toast.makeText(getApplicationContext(), R.string.repeat_off, Toast.LENGTH_LONG).show();
} else {
editor = sharedPreferences.edit();
editor.putBoolean("playerRepeat", true);
editor.commit();
repeatBoolean = true;
repeat.setImageResource(R.drawable.ic_baseline_repeat_24);
Toast.makeText(getApplicationContext(), R.string.repeat_on, Toast.LENGTH_LONG).show();
}
}
});
}
public String durationConversion(int songDuration) {
long s = songDuration % 60;
long m = (songDuration / 60) % 60;
long h = (songDuration / (60 * 60)) % 24;
return String.format("%02d:%02d:%02d", h, m, s);
}
void playPauseButton() {
playPauseThread = new Thread() {
@Override
public void run() {
super.run();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
floatingActionButton.setImageResource(R.drawable.ic_baseline_play_arrow_24);
seekBar.setMax(mediaPlayer.getDuration() / 1000);
SongPlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(currentPosition);
}
handler.postDelayed(this, 200);
}
});
} else {
mediaPlayer.start();
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
seekBar.setMax(mediaPlayer.getDuration() / 1000);
SongPlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(currentPosition);
}
handler.postDelayed(this, 200);
}
});
}
}
});
mediaPlayer.setOnCompletionListener(SongPlayerActivity.this);
}
};
playPauseThread.start();
}
private void nextButton() {
nextThread = new Thread() {
@Override
public void run() {
super.run();
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
if(shuffleBoolean && !repeatBoolean)
position = random(songList.size()-1);
else if (!shuffleBoolean && !repeatBoolean)
position++;
if (position == songList.size())
position = 0;
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(songList.get(position).path));
metaDataRetriever(Uri.parse(songList.get(position).path));
seekBar.setMax(mediaPlayer.getDuration() / 1000);
SongPlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(currentPosition);
}
handler.postDelayed(this, 200);
}
});
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
mediaPlayer.setOnCompletionListener(SongPlayerActivity.this);
} else {
mediaPlayer.stop();
mediaPlayer.release();
if(shuffleBoolean && !repeatBoolean)
position = random(songList.size()-1);
else if (!shuffleBoolean && !repeatBoolean)
position++;
if (position == songList.size())
position = 0;
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(songList.get(position).path));
metaDataRetriever(Uri.parse(songList.get(position).path));
seekBar.setMax(mediaPlayer.getDuration() / 1000);
SongPlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(currentPosition);
}
handler.postDelayed(this, 200);
}
});
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
mediaPlayer.setOnCompletionListener(SongPlayerActivity.this);
}
mediaPlayer.start();
}
});
}
};
nextThread.start();
}
private void previousButton() {
previousThread = new Thread() {
@Override
public void run() {
super.run();
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
if(shuffleBoolean && !repeatBoolean)
position = random(songList.size()-1);
else if (!shuffleBoolean && !repeatBoolean)
position--;
if (position < 0)
position = 0;
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(songList.get(position).path));
metaDataRetriever(Uri.parse(songList.get(position).path));
seekBar.setMax(mediaPlayer.getDuration() / 1000);
SongPlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(currentPosition);
}
handler.postDelayed(this, 200);
}
});
mediaPlayer.setOnCompletionListener(SongPlayerActivity.this);
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
} else {
mediaPlayer.stop();
mediaPlayer.release();
if(shuffleBoolean && !repeatBoolean)
position = random(songList.size()-1);
else if (!shuffleBoolean && !repeatBoolean)
position--;
if (position < 0)
position = 0;
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(songList.get(position).path));
metaDataRetriever(Uri.parse(songList.get(position).path));
seekBar.setMax(mediaPlayer.getDuration() / 1000);
SongPlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(currentPosition);
}
handler.postDelayed(this, 200);
}
});
mediaPlayer.setOnCompletionListener(SongPlayerActivity.this);
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
}
mediaPlayer.start();
}
});
}
};
previousThread.start();
}
private int random(int i) {
Random random = new Random();
return random.nextInt(i+1);
}
public void metaDataRetriever(Uri uri) {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(uri.getPath());
byte[] bytes = mediaMetadataRetriever.getEmbeddedPicture();
final LinearLayout linearLayout = findViewById(R.id.linear_layout);
Bitmap bitmap;
if (bytes != null) {
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
layoutAnimation(getApplicationContext(), songImage, bitmap);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(@Nullable Palette palette) {
Palette.Swatch swatch = Objects.requireNonNull(palette).getDominantSwatch();
if (swatch != null) {
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{0xff000000, swatch.getRgb()});
linearLayout.setBackground(gradientDrawable);
songName.setTextColor(swatch.getTitleTextColor());
albumName.setTextColor(swatch.getTitleTextColor());
artistName.setTextColor(swatch.getTitleTextColor());
durationPlayed.setTextColor(Color.WHITE);
totalDuration.setTextColor(Color.WHITE);
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
floatingActionButton.getBackground().setTint(Color.WHITE);
next.setImageResource(R.drawable.ic_baseline_skip_next_white_24);
previous.setImageResource(R.drawable.ic_baseline_skip_previous_white_24);
seekBar.getThumb().setTint(Color.WHITE);
}
}
});
} else {
layoutAnimation(getApplicationContext(), songImage, null);
linearLayout.setBackgroundResource(R.drawable.gradient_brown);
songName.setTextColor(Color.DKGRAY);
albumName.setTextColor(Color.DKGRAY);
artistName.setTextColor(Color.DKGRAY);
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
next.setImageResource(R.drawable.ic_baseline_skip_next_24);
previous.setImageResource(R.drawable.ic_baseline_skip_previous_24);
seekBar.getThumb().setTint(getResources().getColor(R.color.colorYellow));
durationPlayed.setTextColor(getResources().getColor(R.color.colorYellow));
totalDuration.setTextColor(getResources().getColor(R.color.colorYellow));
}
songName.setText(songList.get(position).songName);
albumName.setText(songList.get(position).albumName);
artistName.setText(songList.get(position).artistName);
totalDuration.setText(Song.durationConversion(songList.get(position).songDuration));
}
public void playMusic() {
if (songList != null) {
floatingActionButton.setImageResource(R.drawable.ic_baseline_pause_24);
uri = Uri.parse(songList.get(position).path);
}
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
mediaPlayer.start();
metaDataRetriever(uri);
seekBar.setMax(mediaPlayer.getDuration() / 1000);
}
public void layoutAnimation(final Context context, final ImageView imageView, final Bitmap bitmap) {
final Animation animationIn = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
Animation animationOut = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
if (bitmap != null)
animationOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Glide.with(context).load(bitmap).into(imageView);
animationIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(animationIn);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
else
animationOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Glide.with(context).load(R.drawable.album_cover).into(imageView);
animationIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(animationIn);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(animationOut);
}
@Override
protected void onResume() {
super.onResume();
playPauseButton();
nextButton();
previousButton();
}
@Override
public void onCompletion(MediaPlayer mp) {
nextButton();
position++;
if(position >= songList.size())
position = 0;
if (mediaPlayer != null) {
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(songList.get(position).path));
seekBar.setMax(mediaPlayer.getDuration() / 1000);
metaDataRetriever(Uri.parse(songList.get(position).path));
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(this);
}
}
}
...ANSWER
Answered 2021-Feb-28 at 05:38The solution is to call next.performClick() in onCompltition() and remove all the other lines. I don't know the reason that the firs way don't work but i thought of it as i need to call onClickListener() without clicking the button.
QUESTION
I have an element in html, inside a button:
HTML:
...ANSWER
Answered 2021-Feb-14 at 18:42This is undoubtedly related to the fact that interactive content is not allowed within a element. Although the browser allows this and does what it can to make the flawed encapsulation work, using HTML inappropriately has consequences. Reference
QUESTION
Im trying to implement an autoplay function into this audio player but I cant get it to work, I have been trying for ages and I feel like I wanna give up. Any help would be helpful. I am pretty new to learning javascript and hope that I will find my answer here. I somehow got it working once but then i forgot to actually save the code and it all went away and now im questioning my life's choices
...ANSWER
Answered 2021-Feb-02 at 15:51Alright, finally I have an answer. Its only two audios played one after another, and you can keep on add more and more. The code is below.
QUESTION
In my web-app, I implemented some tooltips on buttons with images. In Firefox, they work as expected, that is, they appear right below the button when you hover over that button. However, in Chrome, they appear far left of the button.
CSS:
...ANSWER
Answered 2020-Nov-24 at 22:20You need to set position:relative
in or in
QUESTION
I'm working on an audio player and came across this situation: I have a TrackDetailView that opens to play a track when I click on a TableView cell. Also I have implemented background playback and MPNowPlayingInfoCenter. When I press Pause or Play button in MPNowPlayingInfoCenter, I want the button image to change on my TrackDetailView as well, but I just can't do it. I will be glad for any help. Important note(!) TrackDetailView and MPNowPlayingInfoCenter are in different classes. When I put them in one class everything works without problems. My code:
...ANSWER
Answered 2020-Aug-01 at 12:24You need to make sure that for this instance
QUESTION
I have an app that does a countdown with a Timer
. The countdown tracks multiple steps (all at the same intervals) as well as the total time left, and updates 2 separate UILabels accordingly. Occasionally, the labels will be out of sync.
I can't say for sure, but I think it might be only happening when I pause the countdown sometimes, and usually on steps later than the first step. It's most apparent on the last step when the two labels should be displaying the same exact thing, but will sometimes be 1 second off.
The other tricky thing is that sometimes pausing and resuming after the time has gone out of sync will get it back in sync.
My guess is I'm getting something weird happening in the pause code and/or the moving between steps, or maybe the calculating and formatting of TimeIntervals. Also I'm using rounded()
on the calculated TimeIntervals because I noticed only updating the timer every 1s the labels would freeze and skip seconds a lot. But I'm unsure if that's the best way to solve this problem.
Here's the relevant code. (still need to work on refactoring but hopefully it's easy to follow, I'm still a beginner)
...ANSWER
Answered 2020-Jun-09 at 22:52Fixed my issue and posting here for posterity. I ended up making my totalTimeLeft
and currentInterval
global properties. Then, on pause and resume, instead of tracking the paused time and adding it to endTime
, I just used the totalTimeLeft
and currentInterval
values that are still stored from the last Timer
firing and doing endTime = Date().addingTimeInterval(totalTimeLeft)
and the same with the interval time. This got rid of the paused time adding weird amounts that would mess up the rounding.
QUESTION
ANSWER
Answered 2020-Jun-04 at 18:38Your function needs to have the @objc
attribute. This allows it to be looked-up as a selector.
QUESTION
I have problem with assest audio player package when I try to play two songs inside one page both are playing ! The way I want when I press first button,first song play and when I press second button the first song stop and the second song start playing .
I used this code but it doesn't work
HomePage
...ANSWER
Answered 2020-May-12 at 11:48My problem solved by change
final assetsAudioPlayer = AssetsAudioPlayer();
to
final assetsAudioPlayer = AssetsAudioPlayer.withId("0");
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install PlayPauseButton
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page