0

I am designing an App for android that requires various short music files to be played on after each other.

I have set up an "onClick" listener to run a loop that plays each of these sound files over and over, they may be played over 100 times.

BUT

I need the user to be able to interact with the app while it plays, which it cannot at this moment in time, the loop runs and basically the device is frozen until the music files have completed playing.

Any pointers to a helpful solution?

Loop is basically:

mPlayButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public synchronized void onClick(View v) {

            for(char i: method.toCharArray()){
                mPlayer.play(getActivity(),Integer.parseInt(Character.toString(i)));
                try {
                    wait(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }
    });
Allan Macmillan
  • 1,481
  • 3
  • 18
  • 30
  • The Service idea posted below is reasonable, but it sounds to me like you have a bigger problem. The Service will by default run on the same thread, so it's likely no help until you clean things up. Can you post the while loop? – Dave Oct 08 '13 at 11:10

3 Answers3

2

You can do it using a service to play the music in the background.

Here is a great tutorial on how to do this: LINK.

Something like this:

Start this when you want to play the music:

Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc); 

And create a new service file in your project:

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() {

}
}
Community
  • 1
  • 1
Dyna
  • 2,304
  • 1
  • 14
  • 25
1

I can only guess, because you did not attach code to your question, but it sounds like you implemented a loop which runs on the UI thread, the main thread of your application. The application seems to be frozen because one thread can only execute one line of code at a time and can't run code parallel. What you have to do is to create a different thread on which the loop for playing the music is executed. There are different ways to achieve this. I guess you should try to implement a Service. A Service gives you the possibility to run code on a different thread and also you can easily communicate with the service, e.g. for starting or stopping the playing of the music. For further information about services have a look at this: http://developer.android.com/reference/android/app/Service.html You can also google for android service tutorials, there are a lot in the web. Good luck!

mismor
  • 605
  • 4
  • 13
  • Looking at the page you sent there, it says that they are used for background tasks where the user will not interact with them, I would like the user to be able to interact whilst it is running. Is a service the right thing then – Allan Macmillan Oct 09 '13 at 09:28
1

First, sleeping isn't the way to control the timing here because MediaPlayer has an onCompletionListener you can set. It also has a setNextMediaPlayer method if you are careful about resource usage (you can't just create a bunch of MediaPlayers and prepare them all).

Second, you need a separate Thread or an AsyncTask (which will spawn a new Thread). A Service will run in the same Thread as the UI unless you specify otherwise, and it will mostly just add an unnecessary layer of complexity.

Last, you may want to look into using SoundPool.

Dave
  • 4,282
  • 2
  • 19
  • 24