Doing an audio loop on Android - java

I have a track I want to play 'megadeth', I'm calling it by:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.megadeth);
And playing it by using 'mp.start'.
And I just want to know, how can I get this audio mp3 to loop?

Before adding
mp.start();
Add
MediaPlayer mp = MediaPlayer.create(MainMenu.this, R.raw.megadeth);
mp.setLooping(true);
the mp.setLooping(true); automatically allows the raw file to be used over and over again.

Related

MediaPlayer delay 0.4 seconds for Bluetooth headset

I'm developing an application with playing audio. In several cases I change the AudioStreamType to AudioManager.STREAM_RING(is mode when music is playing both: speakers and headset). When I use Bluetooth headset, I have a small annoying delay.
So i was reading that Bluetooth has a buffer and and this is logical
, but how I can solve this? Also i tried to change buffer size or delay for mediaPlayer.
So, i create a mediaPlayer like as:
#Override
public void onInit() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setLooping(true);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
songManager = new SongsManager(context);
}
#Override
public void onPrepare() {
// prepare media player
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
absolutePathToPlayingSong = /*some place*/;
mediaPlayer.setDataSource(/*some place*/);
mediaPlayer.prepare();
mediaPlayer.setVolume(volumeLvl, volumeLvl);
}
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
}
then i just start player:
mediaPlayer.start();
So, i already tried:
to change a prepare() to prepareAsync() - probably it was tiny help, to may be i had a sound hallucinations. Unfortunately it broke a seekTo() in one place
added onPrepared() and start playing from listener - I not sure that it help
Give me please any advance^ how to fix delay for Bluetooth headset? I checked this device in YouTube application and it work great. So problem in my application. sorry my bad English!)
I have 2 target devices android 7.0(SDK 24) and android 4.2 (SDK 16)

how to pause and play music player

I want to write a program which can pause and play every media players in android. How can i set priority of this program higher than the others? also i wrote the following code
public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDPAUSE = "pause";
public static final String CMDPLAY = "play";
public static final String CMDTOGGLEPAUSE = "togglepause";
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME, CMDPAUSE);
MainActivity.this.sendBroadcast(i);
Toast.makeText(this, "media player is pause!", Toast.LENGTH_LONG).show();
}
if (!mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME, CMDPLAY);
MainActivity.this.sendBroadcast(i);
Toast.makeText(this, "media player is play!", Toast.LENGTH_LONG).show();
}
and it can pause every players but i dont know how to play them again. I can play some players but not all of them i dont know why ???
Have an onClick for a play and pause button
You'll have to find a way to retrieve all of the MediaPlayer Objects.
Pausing:
mMediaPlayer.pause(); // Pause the MediaPlayer
Playing:
mMediaPlayer.prepare(); // Prepares the MediaPlayer to play a song
** Please have an onPrepare method as well
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start(); // play!
}
Also, here's a reference for you:
https://github.com/naman14/Timber
This open source music app should have most of the relevant resources you need to creating a music player controller or etc. You'll be able to understand how it works.
Also, the code you've presented won't allow me to help you with identifying any issues unless you have not implemented the MediaPlayer Object.
Here's why: Difference between Audiomanager and MediaPlayer
-- EDIT --
Here's how you can stop other MediaPlayer instances via the MediaPlayerRegistry.
Android: How to stop other active media players?

MediaPlayer stopping when starting another activity

I have a problem with the MediaPlayer.
I have a MediaPlayer class that starts as soon as the user launches the App, but as soon as the user taps on the "credits" button another activity starts with the credits screen, but the MediaPlayer stops playing the audio stream.
Is there a way to avoid this?
MediaPlayer setup:
player = new MediaPlayer();
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
I change page with this:
Intent intent = new Intent(this, Credits.class);
startActivity(intent);
Create a local service and put the MediaPlayer in that Service. Bind to the Service from whatever Activity needs to control the MediaPlayer.
This is not possible since the mediaActivity will be pause. You might consider using a dialog instead of an intent for the credits.

How to start a second media player ..?

I am developing an App where I want to play two mp3 files one is background music and another audio plays after 20 seconds.
mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec);
mediaPlayer1 = MediaPlayer.create(this,R.raw.sound3 );
private void buttonClick(){
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
try
{
mediaPlayer.start();
mediaPlayer1.start();
startPlayProgressUpdater();
}
catch (IllegalStateException e) {
mediaPlayer.pause();
}
}
Use MediaPlayer to play background music and SoundPool for everything else.
You can play two files in Mediaplayer by using this piece of code in a thread(don't use UI thread for this) :
mediaplayer = new MediaPlayer();
mediaplayer.reset();
//For media file 1
mediaplayer.setDataSource(dataSourceOne);
mediaplayer.prepare();
mediaplayer.start();
Thread.sleep(500);//Set the time as per your need.
//For media file 2
mediaplayer.reset();
mediaplayer.setDataSource(dataSourceTwo);
mediaplayer.setLooping(true);
mediaplayer.prepare();
mediaplayer.start();
As per the better implementation perspective, write an util class with all the common methods (play(),pause(),stop()) and call every method from your class based on the requirement using a thread.

Sound plays back in Android Emulator but not on my G1

For my Android game prototype, I'm playing back .wav resources using:
MediaPlayer mp = MediaPlayer.create(context, soundID);
mp.start();
This works fine in the Eclipse Android emulator, but when I run the same program on my G1, no sound happens. What could be the cause of this?
Try doing it this way:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
instead of MediaPlayer.create()

Categories

Resources