0

How to load next song when previous is still playing ? I created a online player , which load songs from VK , but it create very big pauses in the end while loading next song , so is it possible to load next song when this is still playing? and is there any way to perform loading faster?

And one more , when phone isn't locked it load music faster , why?!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ilbets
  • 710
  • 1
  • 9
  • 35

2 Answers2

2

To play next song you need to override setOnCompletionListener for your MediaPlayer

myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {           
   public void onCompletion(MediaPlayer player) {          

   player.stop();
   player.reset();
   player.setDataSource(/*  go to your next target*/);
   player.prepare();
   player.start();
  }
});

but if you want to prepare next song and still didn't finish previous, I think you need two MediaPLayer instances.

As I know Android has about 32 channels for audio stream so no worries, just be sure that you stop on finish each mediaplayer

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
1

You could read the next dataSource into a byte array using this method:

java.net.URL read stream to byte[].

Then when you're ready to read play the song, use a localhost socket.

new Thread(new Runnable() {
    public void run() 
    {
         ServerSocket serverSocket = new ServerSocket(1111); //some port
         Socket s = serverSocket.accept();
         OutputStream out = s.getOutputStream();
         out.write(myByteArray); //either this is static somewhere, or you find a way to get the byte array into the instance
         out.close()
    }
}).start();

mp.setDataSource("127.0.0.1:1111");

You'll still have to prepare the song, but the data transfer will be much faster than reading off an internet URL. So we do that part while the previous song is playing.

Also, note that I didn't handle any exceptions here, you'll have to.

Community
  • 1
  • 1
Cruncher
  • 7,641
  • 1
  • 31
  • 65