Okey, this is my problem. I have one service class where Ive managed to create media player to play music in background all time. Here is code:
package com.test.brzoracunanje;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
player = MediaPlayer.create(this, R.raw.test_cbr);
player.setLooping(true); // Set looping
player.setVolume(100,100);
player.start();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
protected void onNewIntent() {
player.pause();
}
}
But now I have problem when I click on HOME
, or BACK
button. It still plays music. Does anyone knows how to solve that problem?
And here is code how i call this service on class where I want to play music;
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);
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.
In some phone and app configurations, music can automatically start playing when the phone senses an external audio device. To remedy this, try using a toothpick to clean out lint and debris from the jack. Avoid using something hard or sharp that might damage the phone.
This example demonstrates how do I play background music in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java
Service is going to do background operation without interacting with UI and it works even after activity destroy. This example demonstrates what is Android background music service. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
VideoSound ( iOS) This app is basically designed for adding background music to videos that will be uploaded on instagram, Vine or facebook. We have additional feature to add music to single photo or can add dozens of photos with slideshow and background music.
Lumify ( Android) This app is available in iOS and is best for beginners and it’s very easy to use. It lets you mix files and allows adding background music from its own library. We can also add voice as a replacement for music to create better effect if required. 3. MixBit
AsyncTasks are good just for very short clips, but if it is a music file you will face problems like:
Here is few line from Android developers page about AsyncTasks
AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.
So currently I am looking in to other options and update the answer once I find the solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With