4

I put in background of my android application a song. I don't know how much time the application is open. And I want to put this song to repeat. My code is:

    MediaPlayer mySong;

    mySong = MediaPlayer.create(X_0Activity.this, R.raw.tj);
    mySong.start();
Rapptz
  • 20,807
  • 5
  • 72
  • 86
Andreea
  • 151
  • 1
  • 3
  • 12
  • you try repeat song your media player in this link http://stackoverflow.com/questions/11951420/how-do-i-loop-my-media-player-files – AnilPatel Apr 06 '13 at 13:04

3 Answers3

11
Uri mediaUri = createUri(context, R.raw.media); // Audiofile in raw folder
Mediaplayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(context, mediaUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepare();

mPlayer.setLooping(true);  // for repeat song 

mPlayer.start();
AnilPatel
  • 2,356
  • 1
  • 24
  • 40
1

mySong.setLooping(true) // repeat Song

mySong.start(),

And now you are ready with repeat mode on.

0

Use SoundPool, you can easily loop it any time you want! Here is a very good example: Play sound with SoundPool

In the

spool.play(soundID, volume, volume, 1, 0, 1f);

the number 0 represents the number you want to repeat the song. For infinite loop the suitable value is -1.

Community
  • 1
  • 1
Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • I followed the example above, but music doesn't play and I see some errors in LogCat: AudioFlinger could not create track: status -12 and Error creating AudioTrack. I want to mention that I used a wav file and I test my application on emulator – Andreea Oct 26 '12 at 23:28
  • The same thing :( If that help you, in LogCat I see now: SOUND [11.0][0] and the other 2 errors – Andreea Oct 26 '12 at 23:37
  • Anyway if you use WAV file, it needs to be under 1MB as well. I always use 22kHz .ogg files, these caused far the least problem so far. – Jani Bela Oct 26 '12 at 23:39
  • Ok, I will try to make it smaller,and I will inform you. Thanks – Andreea Oct 26 '12 at 23:41
  • I tried with a smaller wav file. Music doesn't play and now I get in LogCat: sample 1 not READY – Andreea Oct 27 '12 at 00:01
  • Try to implement onloadlistener, I am getting short of the ideas. But I still suggest you to use under-1MB ogg files, soundPool works the best with this type. – Jani Bela Oct 27 '12 at 00:07
  • I tried with a smaller wav file. Music doesn't play and now I get in LogCat: Unable to load sample: (null) with red and sample 1 not READY with orange – Andreea Oct 27 '12 at 00:08
  • I am pretty sure that your file is still big enough so the soundpool decompression cannot be finished and you already want to play it. If possbile (for api 8 and higher), you should play it only when it gets loaded. – Jani Bela Oct 27 '12 at 00:11
  • I will try tomorrow with an other smaller file – Andreea Oct 27 '12 at 00:14
  • I used wav file at 22050 Hz, and still not work.The ogg file can not have that frequency – Andreea Oct 28 '12 at 18:26
  • I dont know what program you use, but mp3,ogg,wav they all can have 22050kHz sample rate to even 8kHz. Audacity is free and very simple to use, you can make it with that. – Jani Bela Oct 29 '12 at 11:00