Sound repeat problem

All your problems with Audio, Video and Images.

Sound repeat problem

Postby Lowster » Fri Jul 09, 2010 11:13 am

Hello! Let say I have a sound in my app. This sound is five seconds long. I want to repeat this sound every 30 seconds, so it is like this:

My sound plays (5 seconds), wait for 25 seconds, then play again, and it goes like this as long my user is in my main menu.

Any ideas how to solve this?

Current code I have is like this (I know it just looping directly after the sound has played)
Code: Select all
   mp = MediaPlayer.create(getBaseContext(), R.raw.mainsound);
   mp.start();
   mp.setLooping(true);


So how should I change my code to achieve the "every 30 seconds" thing?
Lowster
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 09, 2010 11:11 am

Top

Re: Sound repeat problem

Postby anarche » Fri Jul 09, 2010 11:38 am

Use a sound file with 25sec of silence at the end?
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Sound repeat problem

Postby Lowster » Fri Jul 09, 2010 11:42 am

Ah nice! :) I had even thought about that.

But (for the sake of this thread) so can any who can solve this post the solution but I accept anarche's solution.
Lowster
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 09, 2010 11:11 am

Re: Sound repeat problem

Postby anarche » Fri Jul 09, 2010 11:43 am

Put it in a different thread and call Thread.sleep(25000); ??
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Sound repeat problem

Postby Lowster » Fri Jul 09, 2010 1:23 pm

I have now coded it like this
Code: Select all
    private void RepeatSoundFunction(){
        t = new Timer();
        tt = new TimerTask() {

            @Override
            public void run() {
                mp.seekTo(0);
                mp.start();
            }
        };

        mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                t.schedule(tt, 25*1000);
               
            }
        });
    }


The wrong I get now is that after it has been repeated once, I get a force close.
Lowster
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 09, 2010 11:11 am

Re: Sound repeat problem

Postby anarche » Mon Jul 12, 2010 5:04 am

I like that solution.

Can you post the logcat?
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Re: Sound repeat problem

Postby Lowster » Mon Jul 19, 2010 8:23 pm

Sorry for the big delay, busy all the time! :)

Here's the logcat: http://pastebin.org/387648
Lowster
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 09, 2010 11:11 am

Re: Sound repeat problem

Postby Angus T. Jones » Fri Jul 23, 2010 2:52 pm

You need to do it in a different way, include your SoundRepeatFunction in the onCompletion; like this:

In onCreate:
Code: Select all
       
mp = MediaPlayer.create(getBaseContext(), R.raw.yoursound);
mp.setOnCompletionListener(new OnCompletionListener() {
      @Override
      public void onCompletion(MediaPlayer mp) {
         RepeatSoundFunction();  //Here do you call for you method
      }     
       });


In RepeatSoundFunction:
Code: Select all
   
public final void RepeatSoundFunction(){    
        t = new Timer();
               tt = new TimerTask() {

              @Override
              public void run() {
                  mp.seekTo(0);
                  mp.start();
                  t.purge();
              }
             
          };
          t.schedule(tt, 25*1000);
    }
User avatar
Angus T. Jones
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jul 23, 2010 2:47 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 14 guests