public class BackGroundMusic extends Service {
MediaPlayer mediaPlayer;
AudioManager audioManager;
int volume;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer = MediaPlayer.create(this, R.raw.tune);
mediaPlayer.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}
@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
and if You need to play the music
startService(new Intent(MainActivity.this, BackGroundMusic.class))
if you want to stop the music
stopService(new Intent(MainActivity.this, BackGroundMusic.class))..
Answer from Mr.Popular on Stack OverflowQuora
quora.com › Is-there-an-Android-music-player-that-plays-always-in-the-background
Is there an Android music player that plays always in the background? - Quora
Answer: Tried some standalone players and none could do this, they only worked at the forefront. However, ES File Explorer built-in music player can play music in the background. It looks very basic but that’s deceiving because it can play ...
Top answer 1 of 2
7
public class BackGroundMusic extends Service {
MediaPlayer mediaPlayer;
AudioManager audioManager;
int volume;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer = MediaPlayer.create(this, R.raw.tune);
mediaPlayer.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}
@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
and if You need to play the music
startService(new Intent(MainActivity.this, BackGroundMusic.class))
if you want to stop the music
stopService(new Intent(MainActivity.this, BackGroundMusic.class))..
2 of 2
2
1. Make Background service
package service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import java.io.IOException;
public class BackgroundSoundService extends Service {
MediaPlayer mPlayer = null;
private final static int MAX_VOLUME = 100;
Context context;
AudioManager.OnAudioFocusChangeListener afChangeListener;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int musicflag = (int) intent.getExtras().get("songindex");
if (musicflag == 1) {
playMusic(R.raw.s1_pondambience);
} else {
playMusic(R.raw.s2_integrative_music);
}
return Service.START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
}
public void onTaskRemoved(Intent rootIntent) {
stopSelf();
}
/*
* playmusic custom method for manage two different background sounds for application
* */
public void playMusic(int musicFile) {
if (mPlayer != null) {
if (mPlayer.isPlaying()) {
try {
mPlayer.stop();
mPlayer.release();
mPlayer = MediaPlayer.create(this, musicFile);
AudioManager am = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
int result = am.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
mPlayer.setLooping(true);
final float volume = (float) (1 - (Math.log(MAX_VOLUME - 85) / Math.log(MAX_VOLUME)));
mPlayer.setVolume(volume, volume);
mPlayer.start();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
mPlayer = MediaPlayer.create(this, musicFile);
AudioManager am = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
int result = am.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
mPlayer.setLooping(true);
final float volume = (float) (1 - (Math.log(MAX_VOLUME - 85) / Math.log(MAX_VOLUME)));
mPlayer.setVolume(volume, volume);
mPlayer.prepare();
mPlayer.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
try {
mPlayer = MediaPlayer.create(this, musicFile);
AudioManager am = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
int result = am.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
mPlayer.setLooping(true);
final float volume = (float) (1 - (Math.log(MAX_VOLUME - 85) / Math.log(MAX_VOLUME)));
mPlayer.setVolume(volume, volume);
mPlayer.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* MediaPlayer methods
* */
public void pauseMusic() {
if (mPlayer.isPlaying()) {
mPlayer.pause();
length = mPlayer.getCurrentPosition();
}
}
public void resumeMusic() {
if (mPlayer.isPlaying() == false) {
mPlayer.seekTo(length);
mPlayer.start();
}
}
public void stopMusic() {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
public boolean onError(MediaPlayer mp, int what, int extra) {
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
return false;
}
}
and start and stop service whenever you want.
2. To Start Service use below method
private void startBackMusic() {
Intent musicintent = new Intent(MenuActivity.this, BackgroundSoundService.class);
musicintent.putExtra(EXTRA_SONGINDEX, 1);
startService(musicintent);
}
3. To Stop Service use below method
private void stopBackMusic() {
Intent musicintent = new Intent(MenuActivity.this, BackgroundSoundService.class);
stopService(musicintent);
}
Videos
Google Play
play.google.com › store › apps › details
Playback: background play - Apps on Google Play
Here are some of the key features that make Playback stand out: * Stream music and watching videos with ease, even when your screen is off or on the lock screen. * Multitask using a floating picture-in-picture mode while listening to your music video in the background, just like a music player or in a floating picture-in-picture mode * Access 100’s music genres like Electronic, Soul, Hip-Hop, Reggae, Rhythm & blues, Disco, Jazz and much more * Save your music or videos to favourites to watch later at your convenience.
Top answer 1 of 7
10
You could put the music player in a service. This would make it independent from the Activities and you would still be able to control the playback through intents.
Here are some code example about it: https://stackoverflow.com/a/8209975/2804473 The code below is written by Synxmax here at StackOverflow, and covered in the link above:
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.idil);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}
2 of 7
2
@Override
public void onCreate (){
super.onCreate();
Player = MediaPlayer.create(this, R.raw.jingle);
mPlayer.setOnErrorListener(this);
if(mPlayer!= null)
{
mPlayer.setLooping(true);
mPlayer.setVolume(100,100);
}
mPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int
extra){
onError(mPlayer, what, extra);
return true;
}
});
}
Softonic
en.softonic.com › downloads › background-music-for-android
Download Background Music For Android - Best Software & Apps
Download Background Music For Android. Free and safe download. Download the latest version of the top software, games, programs and apps in 2025.
Gadget Bridge
gadgetbridge.com › home › gadget bridge ace › top 6 ways to play audio or video in the background on...
Top 6 Ways to Play Audio or Video in the Background on Android (2024)
February 8, 2024 - Certain Android smartphones have a ‘background stream’ feature which is natively built into the sidebar. You can use this feature to play YouTube videos in the background. The only catch is that you won’t be able to see the video in PiP mode. However, it is ideal for listening to music on the YouTube app.
Google Play
play.google.com › store › apps › details
Music Player - MP3 Player - Apps on Google Play
Enjoy every beat, every lyric, and every note with Muzio: Music player - MP3 player, your ultimate music and audio companion. Join 200 Million+ music lovers and experience music like never before.
Themebin
themebin.com › how-to-keep-music-playing-in-the-background-on-an-android-device
How To Keep Music Playing In The Background On An ...
We cannot provide a description for this page right now
Android Developers
developer.android.com › camera & media dev center › background playback with a mediasessionservice
Background playback with a MediaSessionService | Android media | Android Developers
To enable background playback, you should contain the Player and MediaSession inside a separate Service. This allows the device to continue serving media even while your app is not in the foreground.
NoteBurner
noteburner.com › youtube-music-tips › play-youtube-music-in-background-without-premium.html
Solved! Play YouTube Music in Background without Premium | NoteBurner
After that, import YouTube Music to the native music player on your device, and start enjoying. YouTube Music is a free app for iOS and Android that lets you play music in the background only while you're a paid subscriber - YouTube Music Premium or YouTube Premium member.
LG USA
lg.com › us › mobile-phones › VS950 › Userguide › entertainment_To_play_music_in_the_background_while_accessing_other_applications.html
To play music in the background while accessing other applications
From the Home screen, tap Apps > Music Player . Tap a song in your library to listen to it. Tap the Menu Key > Settings and checkmark the Show notification option so that the music controller is displayed on the Notifications panel. Tap Home Key . Tap Apps and tap the application you want to access.
Reddit
reddit.com › r/music › is there a app where i can listen to music in the backround for free?
r/Music on Reddit: Is there a app where I can listen to music in the backround for free?
February 27, 2023 -
Pretty much every app I try either requires you to pay to listen, pay to chose specific songs, or pay to play music in the background. I don't mind ads I just want to listen to music without needing to be on the app.
Fossbytes
fossbytes.com › home › more › list › 11 best music player apps for android in 2022
11 Best Music Player Apps For Android In 2022 - Fossbytes
May 13, 2022 - For people who download music from YouTube, this is probably the best free music player app for Android. In the Discover section, you will find a Music tab where you can play audio of YouTube videos. On top of that, you can keep on playing the music in the background which makes it a perfect ...
Softonic
en.softonic.com › downloads › background-player
Download Background Player - Best Software & Apps
... Floating Popup Player for Android is the perfect application for watching movies or videos while doing other things. The application is made with the… ... Discover Music is an Android app developed by Imagesound Musicstyling, available for free. This app allows you to interact with the ...