×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Feb 2007
    Posts
    4

    j2me audio streaming with two players

    We are trying to create streaming mp3 player with j2me. Because inputstream isn't really a stream we decided to chop the song into 20kb pieces and play a piece at a time. Actual problem is that when the first piece ends and the second piece should start to play, there is a small gap between those(less than second). We have tried to manage the problem by getting the other piece simultaneously and setting the second player in prefetched state. Then it listens that when the other player is no more in started state and puts its own player into started state. Here is to code:

    public void play(String url) {
    try {
    String murl = new String("http://xxx.xxx.xxx.xxx/" + url + "." + teejotai());

    HttpConnection conn = (HttpConnection) Connector.open(murl,
    Connector.READ_WRITE);

    form.append(murl);

    InputStream is = conn.openInputStream();
    player = Manager.createPlayer(is, "audio/mpeg");
    player.addPlayerListener(this);
    player.realize();
    player.prefetch();
    form.append("\n soitin1 " + i + " " + soitin + "\n");
    player.start();
    }
    catch(Throwable t) {
    reset();
    }
    }
    void play2(String url) {
    try {
    String murl2 = new String("http://xxx.xxx.xxx/" + url + "." + teejotai());
    HttpConnection conn2 = (HttpConnection) Connector.open(murl2,
    Connector.READ_WRITE);
    InputStream is2 = conn2.openInputStream();
    player2 = Manager.createPlayer(is2, "audio/mpeg");
    player2.addPlayerListener(this);
    player2.realize();
    player2.prefetch();
    while (player.getState() == Player.STARTED){
    }
    form.append("\n soitin2 " + i + " " + soitin + "\n");
    player2.start();
    play(getURL());
    }
    catch(Throwable t) {
    reset();
    }
    }


    We are just beginning to learn programming so the code is not very sophisticated, so to say. The function teejotai() just increments variable and returns it. So how to fill the gaps and also if there are better ways to do some things.
    One more noob question: how to start it all over again?

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    11

    Re: j2me audio streaming with two players

    hmm... I see two major issues that are wrong here:

    1) Busy wait loop
    Code:
    while (player.getState() == Player.STARTED){
    }
    You should avoid doing a busy wait loop for state change. Instead implement the PlayerListener interface.

    2) What is your thread model? Is this done in seperate threads or in one.

    Hope it helps to focus your efforts.
    For my knowledge there will always be a certain gap unless you try to do audio mix for the first second by overlapping audio from the previous file.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    Re: j2me audio streaming with two players

    hi i am trying to do the same kind of things but with video.
    What do you exactly do with this:
    String murl = new String("http://xxx.xxx.xxx.xxx/" + url + "." + teejotai()); ??
    do you get chunks of data via HTTP with an URL???
    For my part i get byte[] from the network.

    Did you try without prefetching your players?
    have a nice day
    sebastien

  4. #4
    Registered User
    Join Date
    Feb 2007
    Posts
    4

    Lightbulb Re: j2me audio streaming with two players

    Quote Originally Posted by arikr7
    hmm... I see two major issues that are wrong here:

    1) Busy wait loop
    Code:
    while (player.getState() == Player.STARTED){
    }
    You should avoid doing a busy wait loop for state change. Instead implement the PlayerListener interface.

    2) What is your thread model? Is this done in seperate threads or in one.

    Hope it helps to focus your efforts.
    For my knowledge there will always be a certain gap unless you try to do audio mix for the first second by overlapping audio from the previous file.
    1) I see your point with busy waiting loop. It is better to wait END_OF_MEDIA than keep watching the state all the time.

    2) All this is done in a single thread.

    What do you mean by audio mix? We have noticed that if the players overlap then there is no action. It is all for nothing if there is no way to go around the gap because the music is in 20kb chunks what means about 2 seconds so it would be quite annoying if there was small gap every time the chunk changes...

  5. #5
    Registered User
    Join Date
    Feb 2007
    Posts
    4

    Re: j2me audio streaming with two players

    Quote Originally Posted by sepi1982
    hi i am trying to do the same kind of things but with video.
    What do you exactly do with this:
    String murl = new String("http://xxx.xxx.xxx.xxx/" + url + "." + teejotai()); ??
    do you get chunks of data via HTTP with an URL???
    For my part i get byte[] from the network.
    Yes we use it as a locator of a file, so the files are in actual pieces with number behind. For example song.mp3.1 and so on.

    Did you try without prefetching your players?
    have a nice day
    sebastien
    I have understood that in the prefetched state the player establishes the network connections and allocates the data for the player. Or is it somehow possible to jump from realized to start?

  6. #6
    Registered User
    Join Date
    Feb 2007
    Posts
    5

    Re: j2me audio streaming with two players

    hi! how are you?.

    In my case i have exactly the same problem Viheaho has. The situation is that i get the mp3 from the internet and then play it with a player instance. All the mp3 doesn't fit in the memory phone, so i download it in parts.When i finish to get the first part, i start to play it while downloading the second part, and so and so. Then the first part of music reach the end, and the second part (already downloaded) start to play, and ...well...in that moment there was a gap, not generated by the download, caused by the "realized" and "prefetch" states of the player before he play the music. There is no way to skip this states is you wanna play a media using a Player class instance.

    The question is...
    I try your way using two player...and, doesn't work allright...but some parts (by art of magic) where reproduced one after the other without the gap... Do you know another way to do this... I guess this problems it's not new and somehow there is common solution....i hope =D.

    Well thanks for all and luck for everyone=D
    Cya!

  7. #7
    Registered User
    Join Date
    Feb 2007
    Posts
    4

    Re: j2me audio streaming with two players

    Did you see any pattern in where there was no gap? I'm still curious about mixing two players to allmost overlap with eachother. That might be the answer for atleast reducing the gap. By the way Vitucho5005, how did start the loop over again? Have you tried amr as format? It should work with regular stream modified, still not succeeded with it but hopeful. We are using Nokia N80 as our primary testing device. What is the platform you are trying it with?

  8. #8
    Registered User
    Join Date
    Feb 2007
    Posts
    5

    Re: j2me audio streaming with two players

    hi viheaho! looks like we are in the same problem jejeje.
    I am currently working with a nokia 6682. So far, i still have the problem of the gap.

    By "how did start the loop over again?" you refer to how i make to coordine the two players to start one when the other ends...no?..

    well.. basically i have for one side, a buffer of memory where a threat put data from the web (the .mp3 file) and other thread read that buffer. This buffer is "synchronized" so the while one is reading the other can write and viceversa.
    For the other side a thread that pick up the data of the buffer, puts this data on another object that plays the sound, let's call it 2_Player_Reproductor. Here a have two ideas...well i tell you the more structured one. This objetct 2_player_Reproductor implements the interface PlayerListener.

    First you store for a first time the data (in 1 of the two players), then you play it. Then you entered a while there is no the end of the mp3 (you can set a while (true) and start with that...there is no need to worry about that condition). Inside the while you read from the buffer one more block of data, and while you doesn't have place to put this data, this means one player (A) is playing the song and the other (B) has already data and it prefeching or realizing it. You put this thread to wait.
    When the message END_OF_MEDIA is send to your player listener (2_player_reproductor), inside this callback style function you put to play the other player (B), and "releases" the player that was playing before(A), then i activates the thread that was waiting, then he store the data on the player (A).

    I guess it's not very visible, and a matter of fact is quite complicate cuz i guess there are many way to do it... you must control what player (A or B) is avaiable using booleans or another mecanism.

    Inside the class that Reads from the buffer, we have the run method that's looks like this..

    Code:
    
    public synchronized void run ()
    {
            buffer.read(data); // read data
            reproductor.store (data); // put data inside one of the 2 players, not play it!!!
            reproductor.play ();      // then i use this method for start playing just once.
            
            while (true && !bCancel)        
            {                              
                this.buffer.read(data); // read more data blocks
                while (!reproductor.haveStore()) // if a have another player where i can put the music...put it...if not, we wait.
                {                
                    try {this.wait ();} catch (Exception e) {}                
                }                                                    
                reproductor.store (data); // we put that data in one of the two players..        
            }
    }
    
    public synchronized void NOTIFY ()
    {
        this.notify ();
    }
    
    Inside the class Reproductor :

    Code:
       public void playerUpdate (Player player, String eventDescription, Object object) 
        {                
       
            if (eventDescription ==  END_OF_MEDIA) 
            {  
                this.bPlaying = false;
                            
                if (player == player1)
                {
                    this.bPlayer1Avaiable = true;
                    this.nextPlayer = this.player2;          
                }
                else
                {
                    this.bPlayer2Avaiable = true;
                    this.nextPlayer = this.player1;
                }
                try {this.nextPlayer.start();} catch (MediaException ex) {} // Then start the other player...
                BufferReader.NOTIFY(); // Remenber you must gain the lock of the object.., you can't use bufferReader.notify ().... instaed you can use synchornized (BufferReader) { BufferReader.notify; }
            }
        }   
    It's was really hard to me doing all this....most at all for thinking..i hope it can be understable...and i hope we can reach a common solution.

    P.D : The Store method (i forgot jejeje) this method put the data in one of the two players avaible. And realize and prefect, then it modifies the avaiable state of that player. Nothing more. It doesn't play music.
    The play method just do a player.start (); where player is the fists player where you put data with the store method.
    Last edited by Vitucho5005; 2007-02-27 at 14:08.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    11

    Re: j2me audio streaming with two players

    Quote Originally Posted by viheaho
    What do you mean by audio mix? We have noticed that if the players overlap then there is no action. It is all for nothing if there is no way to go around the gap because the music is in 20kb chunks what means about 2 seconds so it would be quite annoying if there was small gap every time the chunk changes...
    I mean that some devices (I guess most of the newer ones) support audio mix. Meaning, you can start more than one player at a time. This enables you for example to hear one sound overlapping the other. Thus, you might exploit that to start the second player not when the first one finishes but a little bit earlier (by querying the first player timeline).

    Let me know if it helped.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Re: j2me audio streaming with two players

    Quote Originally Posted by Vitucho5005 View Post
    hi viheaho! looks like we are in the same problem jejeje.
    I am currently working with a nokia 6682. So far, i still have the problem of the gap.

    By "how did start the loop over again?" you refer to how i make to coordine the two players to start one when the other ends...no?..

    well.. basically i have for one side, a buffer of memory where a threat put data from the web (the .mp3 file) and other thread read that buffer. This buffer is "synchronized" so the while one is reading the other can write and viceversa.
    For the other side a thread that pick up the data of the buffer, puts this data on another object that plays the sound, let's call it 2_Player_Reproductor. Here a have two ideas...well i tell you the more structured one. This objetct 2_player_Reproductor implements the interface PlayerListener.

    First you store for a first time the data (in 1 of the two players), then you play it. Then you entered a while there is no the end of the mp3 (you can set a while (true) and start with that...there is no need to worry about that condition). Inside the while you read from the buffer one more block of data, and while you doesn't have place to put this data, this means one player (A) is playing the song and the other (B) has already data and it prefeching or realizing it. You put this thread to wait.
    When the message END_OF_MEDIA is send to your player listener (2_player_reproductor), inside this callback style function you put to play the other player (B), and "releases" the player that was playing before(A), then i activates the thread that was waiting, then he store the data on the player (A).

    I guess it's not very visible, and a matter of fact is quite complicate cuz i guess there are many way to do it... you must control what player (A or B) is avaiable using booleans or another mecanism.

    Inside the class that Reads from the buffer, we have the run method that's looks like this..

    Code:
    
    public synchronized void run ()
    {
            buffer.read(data); // read data
            reproductor.store (data); // put data inside one of the 2 players, not play it!!!
            reproductor.play ();      // then i use this method for start playing just once.
            
            while (true && !bCancel)        
            {                              
                this.buffer.read(data); // read more data blocks
                while (!reproductor.haveStore()) // if a have another player where i can put the music...put it...if not, we wait.
                {                
                    try {this.wait ();} catch (Exception e) {}                
                }                                                    
                reproductor.store (data); // we put that data in one of the two players..        
            }
    }
    
    public synchronized void NOTIFY ()
    {
        this.notify ();
    }
    
    Inside the class Reproductor :

    Code:
       public void playerUpdate (Player player, String eventDescription, Object object) 
        {                
       
            if (eventDescription ==  END_OF_MEDIA) 
            {  
                this.bPlaying = false;
                            
                if (player == player1)
                {
                    this.bPlayer1Avaiable = true;
                    this.nextPlayer = this.player2;          
                }
                else
                {
                    this.bPlayer2Avaiable = true;
                    this.nextPlayer = this.player1;
                }
                try {this.nextPlayer.start();} catch (MediaException ex) {} // Then start the other player...
                BufferReader.NOTIFY(); // Remenber you must gain the lock of the object.., you can't use bufferReader.notify ().... instaed you can use synchornized (BufferReader) { BufferReader.notify; }
            }
        }   
    It's was really hard to me doing all this....most at all for thinking..i hope it can be understable...and i hope we can reach a common solution.

    P.D : The Store method (i forgot jejeje) this method put the data in one of the two players avaible. And realize and prefect, then it modifies the avaiable state of that player. Nothing more. It doesn't play music.
    The play method just do a player.start (); where player is the fists player where you put data with the store method.


    I am concerned about if that is "real-time streaming" or just progressive download ?I am looking for a way to do real http streamming on j2me , but with little result.
    Would anyone tell me more on this topic , pls?
    Thanks in advance.

  11. #11
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: j2me audio streaming with two players

    it's a 3 year old topic in which u replied...
    And yes downing a piece, playing it while downloading the next chunk is techincally progressive download...
    real http streaming is simply not supported, the only supported straming protocol for the MMAPI is rtsp... the implementation and result are (imho) pretty bad...

Similar Threads

  1. recording audio and streaming it over a network using j2me
    By babai78 in forum Mobile Java Tools & SDKs
    Replies: 4
    Last Post: 2007-10-17, 08:17
  2. How to repeat audio playback in audio streaming?
    By asheeshv in forum Symbian C++
    Replies: 6
    Last Post: 2007-07-11, 08:33
  3. Half Duplex Audio Streaming
    By subhrajyotisaha in forum Symbian Media (Closed)
    Replies: 0
    Last Post: 2006-05-25, 04:39
  4. audio streaming using j2me
    By niveas in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-03-24, 20:16
  5. How to stop audio streaming on an incoming call?
    By lalitsharma in forum Symbian C++
    Replies: 0
    Last Post: 2005-06-02, 06:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •