How to detect whether there is other app playing music background? - java

I am developing a Video App, and I need to know whether is any BGM playing?
I tried to use AudioManager.isMusicActive(), but it didn't work, because when I play my video, this method always returns true(I guess it detects my app).
In a word, I need to know BGM playing in other App, not Mine.

Finally, I've solved this problem by detecting other app's music every time before my music is going to start.
Because befeore your music is playing, if AudioManager.isMusicActive() returns true, then it means BGM is playing.
Something like this,
public void start() {
AudioManager am = (AudioManager) weak_context.get().getSystemService(Context.AUDIO_SERVICE);
boolean check = am.isMusicActive();
playMyMusic();
}

Related

Vlcj change video background color

I'm creating a narrative application in which I need to play videos.
When playing a media with MediaPlayer.playMedia(Media media).
In order to avoid the black background I wait the wait for the playing event before showing the media player.
player.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
#Override
public void playing(MediaPlayer mediaPlayer) {
showVideo();
}
}
The problem is that this event is triggered before the video actually starts playing, so it still displays black for a little while before.
How can I find a way around this? Thanks
I don't know for certain, but you could try some other event listener methods - perhaps the videoOutput, elementaryStreamAdded or elementaryStreamSelected (and check the stream type parameter was video), or maybe wait for the very first positionChanged event (whilst not ideal, it might work well enough for your use case).

How to attach default equilizer( Sound effect ) of android with mediaplayer in our android app

I am working on a Music player. I want to add default Equalizer ( Sound effect) in my music player app. I successfully add it to my player app but sound effect not working on the media player of my android app. No effect when I change Jazz, Pop, Rock, Folk etc.. how to attach it which current media player in our Android app.
I open it with Intent
val intent = Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL)
if (intent.resolveActivity(packageManager) != null)
{
startActivityForResult(intent, 1234)
}
This link has quiet a lot of information on how to create an equalizer from scratch
https://www.101apps.co.za/articles/perfect-sound-using-the-equalizer-effect-a-tutorial.html
If this is not what you are looking for, perhaps a bit more info could be helpful.
To clear the basics: make sure you are creating the equalizer and attaching it to the media player. Also make sure you are setting up the UI by calling the setupEqualizerFxAndUI.
Hope this helps.

How can I fix my audio? (Prioritize)

When two audio's are played at the same time...the sound is canceling out. How do I fix this weird phenomenon?
I have some code where there is audio on button click and audio in ten second intervals (in a background service). I have the following code to stop the button audio when the ten second interval plays, and it works fine:
public static void myPop(Context context){
AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(!manager.isMusicActive()) { //Only if there isn't any other audio playing
MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
pop.start();
}
else{
Log.v(TAG, "Audio is already playing");
}
}
This works fine, and it stops one audio from playing (pop) to let the other audio play(The one from the background service). Now, I am getting the issue when they both play at the same time. For example, when I tap the button at the exact same time as when the audio from the background service is about to start. when they are both played at the same time, the audio just gets cut off
Is there any way to give a preference to the background service audio? Somehow say that: If two audio pieces start at the exact same time, I want to let the background service audio to play.
To think of this visually:
(https://upload.wikimedia.org/wikipedia/en/4/49/Preference_example.jpg)
I want to say that if I have the choice between apples and oranges, pick apples. AKA If I have two audioplayers playing at the same time, pick one (the apple).
Thanks for your help,
Ruchir
You may have a race condition. In these three lines
1 if(!manager.isMusicActive()) {
2 MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
3 pop.start();
Between line 1 and line 3, there is opportunity for another player to get the media player and start playing, especially if line 2 is slow.
I would suggest making one media player that both players access (i.e. create the "MediaPlayer pop" elsewhere and share it), or find another way to synchronize the different players. Perhaps issuing a stop right before a play, rather than checking for play state.
You can use code for play or pause audio when other player start or pause.
AudioManager.OnAudioFocusChangeListener afChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) {
// Pause playback
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
For more referernce
http://developer.android.com/training/managing-audio/audio-focus.html

Android - info for create a loop pad - music app

I want to create a music loop pad, something like this: https://www.youtube.com/watch?v=fwBPYwiYp-Y
Right now, i am using MediaPlayer to play the sounds. I have a global variable that stores the current time (position) of the first audio that is playing, and when i press another button the corresponding audio file starts playing at the position given by the global variable.
My proble is: the sounds are playing out of sync.
can you give me some help? How can i sync the sounds? like this app https://www.youtube.com/watch?v=fwBPYwiYp-Y
Can you point me some info or tutorials about how to create a loop pad?
Thanks
Richardd
Programming something like this is harder than you think. Here are two things you must do to have the sounds play in sync:
Make sure all you sounds are at the same tempo, and are cropped properly - that means that the starting and ending of the sound clip must be exactly on beat - another solution would be to store the starting and ending time for each clip (very easy with OOP)
Use a timer to make sure the sound starts playing on beat. For example, if you wanted to make your music run at 120 bpm, you would need to have your timer fire every 500 ms.
Here is some code (only concept, won't compile):
Timer beatTimer;
boolean isDrumLoopEnabled;
boolean isDrumLoopCurrentlyPlaying;
Sound drumloop;
public void startMusic(){
//prepare timer
beatTimer=new Timer();
beatTimer.interval=500;//for 120 bpm
beatTimer.onTick+=onTickHandler;
isDrumLoopPlaying=false;
isDrumLoopCurrentlyPlaying=false;
//load sounds
Sound drumloop=new Sound("/storage/emulated/drumloop.ogg");
//start music
beatTiemr.start();
}
//this method will run on every beat:
public void onTickHandler(){
if(isDrumLoopEnabled){
//make sure drum loop isn't already playing
if(!isDrumLoopCurrentlyPlaying){
//begin looping drumloop in background
drumloop.startloop();
isDrumLoopCurrentlyPlaying=true;
}
}else{
if(isDrumLoopCurrentlyPlaying){
//stop playing drumloop
drumloop.stopPlaying();
}
}
}
//when the button is pressed...
public void drumloop_buttonpressed(){
//this will toggle whether to play the drum loop:
isDrumLoopEnabled=!isDrumLoopEnabled;
}

Simple way to implement a mute button in Android app

I am looking for a simple way to implement a mute button.
Obviously I could have a toggle button, mute_button and in every instance where a sound is played I could check:
if(mute_button.isChecked()){
//dont play
}else{
//play the sound
}
But I have a lot of instances that I would need to sort through and add this in. Is there a simpler way of something along the lines of:
mute_button.setOnClickListener() ...
onClick
if(isChecked){
//set entire application to mute mode
}else{
//keep default application sound settings
}
So, really I am looking for a "higher level" or application wide mute, rather than checking if a button is checked every single time a sound may play.
You can try by setting the media volume to zero
MediaPlayer media = MediaPlayer.create(this, R.raw.backgroundSound);
//media.setVolume(leftVolume , rightVolume);
media.setVolume(0 , 0);
For soundpool you can try like this:
soundpool.setVolume(streamID, 0f, 0f);

Categories

Resources