Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background music Android

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);
like image 405
Zookey Avatar asked Oct 28 '11 11:10

Zookey


People also ask

How do I play music in the background on my Android?

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.

Why is my phone playing music by itself?

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.

How do I play background music in Android?

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

What is Android background music service?

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.

What is the best app to add background music to videos?

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.

What is the best music mixing app for Android?

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


1 Answers

AsyncTasks are good just for very short clips, but if it is a music file you will face problems like:

  • You will not be able to stop/pause the music in middle. For me that is a real problem.

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.

like image 165
aks Avatar answered Oct 19 '22 23:10

aks