SetLooping(boolean) function won't work in android - android

I created a sample audio player application in android. In my application I used Play, Pause and Stop functions. Now I wish to add Loop functionality in my application. i.e., when I click the Loop button, recently played audio will play continuously, when I click Stop button the audio file will be stop. I go through the android developer site, I got a function setLooping(boolean). I set this my media player nothing will happen, how can I use this, in my code I used like this,
MediaPlayer mPlayer= new MediaPlayer();
mPlayer.setLooping(false);

you are setting the setLooping to false ,if you want to be repeted you must set it to true .You want it to be playing again and again right?
Use:
mPlayer.setLooping(true)

Related

Android Music player in Kotlin

I have developed a music player app in kotlin language. At present when I start another app's song, my app's song is also playing. both songs are playing simultaneously. I would like to add a functionality where the song should stop automatically when another app's media (audio/video) starts playing. How to add this functionality. What concept is behind it? Any hint or suggestion would be appreciated. thanks
You can take a look at this Documentation, https://developer.android.com/guide/topics/media-apps/audio-focus#audio-focus-change. where you will be given the option to add listener in the AudioFocusRequest Builder
to listen to Audio Focus Event and add your callback there.
alternatively ExoPlayer Library gives you the option to handle Audio Focus automatically on setAudioAttributes method on its Player implementation

Android ExoPlayer demo: continue playback with screen locked

I want to modify the ExoPlayer demo to allow audio playback to continue when the screen gets locked (this should work regardless of whether the media being played is audio or video). Based on some hints (e.g. 1, 2) this is what I came up with so far:
https://github.com/google/ExoPlayer/compare/release-v2...sedubois:background-playback
This is directly based on the ExoPlayer demo code, currently in version 2.12.1. It adds a "foreground" permission, registers a service, and creates a notification in that service (according to the documentation this notification is required for background playback). The service is started when initializing the player.
I can start the player and the demo looks as illustrated below with an audio stream. At this point the notification gets created properly, it shows the player details (title, description, progress bar) and control buttons (see picture below), which work (I can play/pause, restart from the beginning and skip backwards/forwards).
However the playback still stops when I minimize the application or when I lock the screen by pressing the power button. What is the proper way to make this work?
Just to make sure we are on same page. I'm testing on dev-v2 branch of exoplayer repo here
onStop() method in PlayerActivity.java is releasing player, So when app goes in background then onStop() is being called and it is releasing player with method releasePlayer.
If you'll comment out that method i.e. dont release player in onStop(), then player will play in background.

How to check if external media player is playing in android?

I want to stop my media player if external media player is playing.
I've implemented following code but it is called even when my app media player is playing.
if (audioManager.isMusicActive()) {
return;
}
How to distinguish between in-app media player and external media player?
Any help would be appreciated.
Two or more Android apps can play audio to the same output stream
simultaneously. The system mixes everything together. While this is
technically impressive, it can be very aggravating to a user. To avoid
every music app playing at the same time, Android introduces the idea
of audio focus. Only one app can hold audio focus at a time.
Further,
A well-behaved audio app should manage audio focus according to these
general guidelines:
Call requestAudioFocus() immediately before starting to play and
verify that the call returns AUDIOFOCUS_REQUEST_GRANTED. If you design
your app as we describe in this guide, the call to requestAudioFocus()
should be made in the onPlay() callback of your media session. When
another app gains audio focus, stop or pause playing, or duck the
volume down. When playback stops, abandon audio focus. Audio focus is
handled differently depending on the the version of Android that is
running:
You can check the more detail from developer link.

Resume music from another android app

I have an app that runs the Youtube API and plays a video in a certain view. When the video plays, the music from any background apps are paused. I've tried to resume the music with the following code which is run onBackPressed():
private void resumeMusic() {
if(wasMusicPlaying){
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
ExerciseViewer.this.sendBroadcast(i);
}
}
When I go back, I can't get the music to resume. I know its possible because when using snapchat, the background music automatically resumes after a snap video finishes playing but I can't seem to get this functionality on my app.
I've also tried to use "togglepause" instead of play but that does not work either.
I've not tried this, but I think this can be achieved using Android - MediaPlayer.
When entering to your app at the begining of the code, you can check audio is playing. (this should done before calling to youtube api)
mediaPlayer.isPlaying()
if true, you know that a media was playing when someone enter into your application. Store that in your application logic. maybe call it boolean wasplaying,
boolean wasPlaying = mediaPlayer.isPlaying();
when exiting your application, check whether there's was an any audio playing. if any audio is playing, resume it.
if(wasPlaying){
mediaPlayer.start(); //resumes mediaplayer audio
}
I'm not sure about the code and media player does the exact thing, but you can try to use a logic like that.
You can have a look at https://developer.android.com/reference/android/media/MediaPlayer.html
if mediaplayer doesn't work getting the current playing audio, this would help Track info of currently playing music

how to detect when Music is played using Native Music Player?

I am making an app in which I've made a Service which plays Music from URLs. The thing is that my music Service is playing music correctly BUT when user plays any song with Native music player then BOTH(Native Player and My Music Service) are playing music simultaneously. I want to stop My Music Service when user started playing music with native player.
Is there any Broadcast Intent which i can register to Detect the
music player is started?
Is it possible to detect Music player Started?
Any Other Solution?
Any suggestions would appreciated.
I'll suggest a different approach, that I believe it's the correct approach.
the issue on your approach is that you're suggesting to check for one specific app. And there're tons of different music players, plus radio players, plus video players, plus games... and all of those should stop your music in case they want to play something.
So how you do it?
It's all explained in the Android Developers website.
You have to register an OnAudioFocusChangeListener, so, whenever a different app request to have the audio focus, your app can stop the music.
Step 1: Detect if the user has opened native music app. For this , you need to know the package name of your native music app.
Then refer to my answer here: Android how to know an app has been started and range apps priority according the starting times
Using that , the list taskinfo will have the list of all running activities, and as explained there, the first element of the list will be the activity in the foreground.
STEP 2: Once you detect native music app being activated using STEP 1 (by polling for it in the background) , then stop your app's service.
NOTE: You should do this in a background (using asynctask) or another service.
NOTE 2: The limitation of this method is that you can't actually stop the music player when the user clicks play in the native music app, since this method will help you detect only if the native music app is opened or not.

Categories

Resources