How to Make the Music Loop After the Music Ends Android - android

I just wanted the music in my activity to play endlessly (i.e. when my 38-second music ends, i wanted to play it again and again until the user leaves the page). By the way, i am using MediaPlayer.
ourSound = MediaPlayer.create(NewKFCActivity.this, R.raw.start);
ourSound.start();
Cheers!

You can call MediaPlayer#setLooping(boolean):
ourSound = MediaPlayer.create(NewKFCActivity.this, R.raw.start);
ourSound.setLooping(true);
ourSound.start();

Related

Why music won't play on Chrome for Android?

My website plays a background song and some audio effects at the same time. Although the song loads fine (I can see its duration via Audio.duration property), it doesn't play, while the audio effects play (I have already clicked on the screen to activate audio playing). On Firefox in my PC it plays without problem. The audio file is a 10 MB mp3 (but my Android is at home wi-fi). Below is part of my code.
var music,clicked=false,downloaded=false;
function play() {
music.play();
}
function canPlay() {
downloaded = true;
if (clicked) {
play();
}
}
function touchStart(e) {
if (!clicked) {
clicked = true;
if (downloaded) {
play();
}
}
}
function load() {
music = new Audio('mp3/music.mp3');
music.addEventListener('canplaythrough',canPlay,false);
window.addEventListener('touchstart',touchStart,false);
}
The load function is called in the onload of body. The clicked and downloaded variables are required because I never know what happens first: the user click or the audio finish downloading.
On Google mobile-friendly test, it says the audio file couldn’t be loaded (Status: Other error). I've read this test doesn't wait for more than 3 seconds. However, reading the time of the song in seconds doesn't prove it has downloaded?
Why won't the song play on mobile?
EDIT
I created a timer interval that tells me the position (music.currentTime) of the song once every minute. On desktop, it gives the right time. On mobile, it always gives 0, meaning that the song, although loaded (?), isn't really being played.
EDIT 2
I investigated the promise returned by music.play(), and noticed it was giving the error: "(NotAllowedError: play() failed because the user didn't interact with the document first)". It seems that touchStart doesn't count as a "user interaction" the way that mouseDown does...
In the end, I removed both touchStart and touchEnd (each with his own preventDefault), leaving the song to be played after the resulting mouseDown. I've kept the touchMove, however, because it's different from the mouseMove, and required for my site.

Play single track spotify sdk

I am using the Spotify android SDK, and I am trying to get a single song to play, and I would like to potentially play a song after the current one is completed. The issue is that after the song completes Spotify plays a random song afterwards. Is it possible to play the song and not have anything else automatically play after it?
I am simply calling the app remotes player API play function,
mSpotifyAppRemote?.getPlayerApi()?.play(uri)
#Mikee you are doing nothing wrong, Spotify messes around with track replay if you are only using the Free version, if you use Premium it will work correctly. Funny enough if you try playing by an alblum or artist that works with the Free version.
I'm not an expert but from their documentation it looks like you could watch the PlayerState. I'm also not sure when a PlayerState event would trigger but if it's coming back relatively often you could check the track value and see if it's gone to null, or another value and work using that.
Here's a Java example from their website:
// Subscribe to PlayerState
mSpotifyAppRemote.getPlayerApi()
.subscribeToPlayerState()
.setEventCallback(new Subscription.EventCallback<PlayerState>() {
public void onEvent(PlayerState playerState) {
// See what values are in playerState, might be able to determine
// if it's now randomly playing?
final Track track = playerState.track;
if (track != null) {
Log.d("MainActivity", track.name + " by " + track.artist.name);
// If the track is now different, your song has finished, stop it?
}
}
});
I've put a few extra comments in the code above that might yield some results!

How to determine media completion in VideoCastControllerActivity?

In my app, I am using the CastCompanionLibrary to cast videos to a chromecast device. I want to perform some action when the video has finished playing. How do I determine as to when the video has completed playing? I did not find any suitable callback for this.
Register a VideoCastConsumer that implements onRemoteMediaPlayerStatusUpdated(). This will inform you when the playback status changes.
In that callback, get the mediaStatus = mCastManager.getMediaStatus(); this will give you the updated status.
If mediaStatus == MediaStatus.PLAYER_STATE_IDLE and mCastManager.getIdleReason() == MediaStatus.IDLE_REASON_FINISHED, then it means the currently playing item is just finished.
Note that a version of this is already being done in the associated fragment so please tell us what you are doing exactly; what I suggested allows you to generically find out when the playback of a single media items reaches its end.

Android Chromecast: Call messageStream.playFrom(position) does not work in MediaProtocolCommand onCompleted

I had a media player, when user connect to chromecast in the middle of the video, I would like to cast the video to chromecast at the same position in the video where the user left off.
mediaProtocolCommand.setListener(new MediaProtocolCommand.Listener() {
public void onCompleted(MediaProtocolCommand mPCommand) {
messageStream.playFrom(currentVideoPosition);
}
});
Using the code snippet above I manage to get the video playing but it always start at the beginning of the video
Works fine for me using the Cast Android sample app. Make sure the parameter value for playFrom is the time in seconds.

Using Mediaplayer to replay the same file

Im hoping you can provide some guidance. I created a 'final' instance of a mediaplayer (mp1) and on a button click it plays a mp3 file. I then need to stop the file when I click a second button. This works fine until I try to play the file again - nothing happens. I think that because the mp1 instance is 'final', when I stop it, it stops for good until I relaunch the app. I dont want to pause the file, I want to stop it and then restart it afresh. Any ideas welcome. I tried putting the mp1 creation within the button. This worked until the app crashed - probably because multiple mediaplayer creations used all the device memory?
Thanks!!!
// const mediaplayer
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.mysound);
...
// in button 1
if (radSound1.isChecked()) {
radSound2.setChecked(false); // ...set radiobutton 2 to false
mp1.start(); // ...play the mp3 file
}
...
// in button 2
if (mp1 != null){
mp1.reset();
//mp1.setDataSource();
// mp1.prepare();
}
Follow the state diagram here. stop() stops playing and puts you in stopped state. Before starting to play for the next time, you should call prepare() and then start() again.
So in button 1, you should call start() and in button 2, you should call stop() and prepare(). The initialization is fine, do it once and keep it outside the buttons.
Also accept answers to your questions including this so that people like me will be more motivated to reply to them in the future.

Categories

Resources