This discussion is archived
10 Replies Latest reply: May 27, 2005 4:41 AM by 843851 RSS

Sound HEEEEELP ! :)

843851 Newbie
Currently Being Moderated
Hi,
I've started to study Xlets only few days ago, from a really good starting point :)
http://www.mhp-interactive.org/

I have simple questions ... does anybody have an example explaning how can I play an audio file ? ... I'd like to use it when an object on the scene gets focus.

I'd like also to know which sound formats are supported and if XleTView can play sound

I tried HSound but with no results.

Where could I found a good documentation for Classes of JavaTV and Havi and DVB packages ?
(plz .. write links :P )

many many thanks :D
  • 1. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    The following code should work, but I haven't tested it. What MHP implementation are you using, and what behavior do you see from your code?
    HSound myHSound = new HSound;
    try {
      myHSound.load("audio/myaodiofile.mpg");
    } catch (IOException io) {
      ioe.printStackTrace();
    }
    myHSound.play();
    MHP receivers will only support MPEG-1 audio, so you need to make sure that your audio clips are in the right format.

    Since you've already been exploring my website, http://www.interactivetvweb.org/resources/links.shtml#specs includes links to all of the approproiate javadoc.

    Steve.
  • 2. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    Hi Hi..
    all HSounds should be in mpeg2.

    HSound sound1;
    sound1.load("a1.mp2");
    sound.play();

    this definitely work.. i've tried it.. :)
  • 3. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    i met a problem when i try to play a sound on the screen, i saw your solutions, however, i still got an error when i compile the source codes in the C propmt, it is said that

    "java.net.MalformedURLException:no protocol :001.mp2. is there anyone who can help me to slove that problem? "

    thanks~~~

    william
  • 4. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    Can you post your code? That will make it easier to find the problem.

    What implementation are you using?

    Steve.
  • 5. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.Container;
    import java.awt.Panel;
    import java.awt.List;
    import java.awt.ItemSelectable;
    import java.awt.BorderLayout;
    import xjavax.tv.xlet.Xlet;
    import xjavax.tv.xlet.XletContext;
    import xjavax.tv.xlet.XletStateChangeException;
    
    //import java.util.*;
    //import java.awt.image.*;
    //import java.net.*;
    //import java.awt.event.*;
    //import xjavax.tv.xlet.*;
    //import org.dvb.ui.*;
    //import org.davic.media.*;
    //import xjavax.tv.service.selection.*;
    
    import org.havi.ui.HSound;
    import java.io.IOException;                                            //need for Sound IOExcpetion
    
    import org.havi.ui.HDefaultTextLayoutManager;
    import org.havi.ui.HScene;
    import org.havi.ui.HSceneFactory;
    import org.havi.ui.HScreen;
    import org.havi.ui.HStaticText;
    
    
    public class testframework implements Xlet, KeyListener 
    {
    
         private XletContext context;
         private HScene scene;
         private Panel MainFramePanel;
         private Panel SubFramePanel1;
         private HStaticText label1;
         private HStaticText TestTemplateLabel;
         private List TestTemplateList;
         private HStaticText TestTemplateLabel1;
         private HStaticText[] labels ;
         private int intColor;
         private int pointerdown = 0;
         private int pointerup = 0;
         private int keycontroler = 0;
         private int p1option_pointer = 0;
         private final String listc[] = {"Draw circle","Draw rectangular","Play music"};
         private HSound myHSound;
         //private AudioClip sound;
         
         public testframework() 
         {
         }
    
         public void initXlet(XletContext xletContext) throws XletStateChangeException 
         {
              System.out.println("begin MHP Test Framework");
              context = xletContext;
         }
    
         public void startXlet() throws XletStateChangeException 
         {
              System.out.println("begin MHP Test Framework");
              HSceneFactory hsceneFactory = HSceneFactory.getInstance();
              scene = hsceneFactory.getFullScreenScene(HScreen.getDefaultHScreen().getDefaultHGraphicsDevice());
            
              scene.setSize(800, 600);
              scene.setLayout(null);
              scene.addKeyListener(this);
              scene.setLocation(50,50);
              //----------------------------------------------------------------------------------------------------------------------------
              // this codes is to set a main framework of the MTF.
              MainFramePanel = new Panel();
              MainFramePanel.setSize(450, 350);
              MainFramePanel.setBackground(Color.gray);
    
              scene.add(MainFramePanel);
              
              labels = new HStaticText[5];
    
              label1 = new HStaticText("MHP Application Test framework", 20, 20, 400, 40, new Font("Tiresias", Font.BOLD, 20), Color.red, Color.gray, new HDefaultTextLayoutManager());
              MainFramePanel.add(label1);
    
              labels[1] = new HStaticText("Graphics testing", 70,70,350,40, new Font("Tiresias", Font.BOLD, 20), Color.yellow, Color.gray, new HDefaultTextLayoutManager());
              
              MainFramePanel.add(labels[1]);
    
              labels[2] = new HStaticText("Key events testing", 70,120,350,40, new Font("Tiresias", Font.BOLD, 20), Color.yellow, Color.gray, new HDefaultTextLayoutManager());
              MainFramePanel.add(labels[2]);
    
              labels[3] = new HStaticText("Objecting moving testing", 70,170,350,40, new Font("Tiresias", Font.BOLD, 20), Color.yellow, Color.gray, new HDefaultTextLayoutManager());
              MainFramePanel.add(labels[3]);
    
              labels[4]= new HStaticText("Sound testing", 70,220,350,40, new Font("Tiresias", Font.BOLD, 20), Color.yellow, Color.gray, new HDefaultTextLayoutManager());
              MainFramePanel.add(labels[4]);
    
              scene.setVisible(true);
              scene.requestFocus();
              // this codes is to set a main framework of the MTF.
              //------------------------------------------------------------------------------------------------------------------------------
              //------------------------------------------------------------------------------------------------------------------------------
              //this codes define the subframepanel1 of MTF.
    
              SubFramePanel1 = new Panel();
              SubFramePanel1.setSize(450, 350);
              SubFramePanel1.setBackground(Color.gray);
              TestTemplateLabel = new HStaticText("Sound Testing", 20, 20, 400, 40, new Font("Tiresias", Font.BOLD, 20), Color.red, Color.gray, new HDefaultTextLayoutManager());
              SubFramePanel1.add(TestTemplateLabel);
              
    
              TestTemplateLabel1 = new HStaticText("Play music...", 0, 70, 400, 40, new Font("Tiresias", Font.BOLD, 20), Color.yellow, Color.gray, new HDefaultTextLayoutManager());
              SubFramePanel1.add(TestTemplateLabel1);
              ////TestTemplateList = new List(5);
              //TestTemplateList.setVisibleRowCount(5);
              ////for (int i =0; i< listc.length;i++){
              ////TestTemplateList.add(listc);
              //add(TestTemplateList);
              ////}

              //TestTemplateList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
              ////TestTemplateList.setVisible(true);
              //scene.add(TestTemplateList);
              ////SubFramePanel1.add(TestTemplateList);

              //this codes define the subframepanel1 of MTF.
              //------------------------------------------------------------------------------------------------------------------------------
         }

         public void pauseXlet()
         {
         }

         public void destroyXlet(boolean flag) throws XletStateChangeException
         {
         }

         public void keyTyped(KeyEvent e)
         {
         }

         public void keyReleased(KeyEvent e)
         {
         }

         public void keyPressed(KeyEvent e)
         {

              
              int keyCode = 0;
              int id = e.getID();
              if (id == KeyEvent.KEY_TYPED)
              {
                   char c = e.getKeyChar();
                   
              }
              else
              {
                   keyCode = e.getKeyCode();
                   
                   if (keycontroler ==0)
                   {
                        if (keyCode==40)
                        {
                             if(pointerdown>3 || pointerdown<0)
                             {
                                  pointerdown = 0;
                                  pointerup = pointerdown;
                             }

                             if(pointerdown<=4 && pointerdown>=0)
                             {

                                  for(int i=1;i<=4;i++)
                                  {
                                       labels[i].setForeground(Color.yellow);
                                       labels[i].repaint();
                                  }
                                  pointerup = pointerdown;
                                  pointerdown++;
                                  labels[pointerdown].setForeground(Color.blue);
                                  labels[pointerdown].repaint();
                                  System.out.println(pointerdown);
                                  System.out.println(pointerup);
                             
                             }
                        
                        }
                        if (keyCode==38)
                        {
                             for(int i=1;i<=4;i++)
                             {
                                  labels[i].setForeground(Color.yellow);
                                  labels[i].repaint();
                             }
                             if(pointerup<0 ||pointerup>3)
                             {
                                  pointerup = 3;
                                  pointerdown = pointerup;
                             }

                             switch(pointerup)
                             {
                        
                                  case 0:
                                       labels[4].setForeground(Color.blue);
                                       labels[4].repaint();
                                       break;
                                  case 1:
                                       labels[1].setForeground(Color.blue);
                                       labels[1].repaint();
                                       break;
                                  case 2:
                                       labels[2].setForeground(Color.blue);
                                       labels[2].repaint();
                                       break;
                                  case 3:
                                       labels[3].setForeground(Color.blue);
                                       labels[3].repaint();

                             }
                        
                             pointerup--;
                             pointerdown = pointerup;
                             System.out.println(pointerdown);
                             System.out.println(pointerup);
                        
                        
                        }
                        if(keyCode==10)
                        {
                             if ((pointerdown == -1 && pointerup == -1)||(pointerdown ==4 && pointerup ==3))
                             {
                                  scene.remove(MainFramePanel);
                                  TestTemplateLabel1.setForeground(Color.yellow);
                                  TestTemplateLabel1.repaint();
                                  scene.add(SubFramePanel1);
                                  

                                  keycontroler = 4;
                             }

                        }
                   }
                   else
                        if(keycontroler == 4)
                   {

                        if(keyCode == 40)
                        {
                             TestTemplateLabel1.setForeground(Color.blue);
                             TestTemplateLabel1.repaint();
                             p1option_pointer = 1;
                        }
                   
                        if(keyCode == 38)
                        {
                             TestTemplateLabel1.setForeground(Color.blue);
                             TestTemplateLabel1.repaint();
                             p1option_pointer = 1;
                        }
                        if(keyCode == 10)
                        {
                             if(p1option_pointer == 1){
                             
                                  //scene.removeAll();
                                  playMusic();
                             }
                        }
                   }
                   if(keyCode==27)
                   {
                        
                        if (keycontroler !=0 )
                        {
                             scene.removeAll();
                             scene.add(MainFramePanel);
                             keycontroler = 0;
                             //pointerup = 0;
                             //pointerdown = 0;
                        }
                        else
                             if(keycontroler == 0)
                        {

                             if (scene != null)
                             {
                                  scene.setVisible(false);
                                  scene.removeAll();
                                  scene = null;
                             }
                             context.notifyDestroyed();
                        }
                                  
                   }

              }
         }
         public void playMusic()
         {
              HSound myHSound = new HSound();
              try
              {
                   myHSound.load("testsound.mp2");
              }
              catch (IOException ioe)
              {
                   System.out.println("Error: myHSound Could not load sound. Input/output error:" + ioe.getMessage());
              //ioe.printStackTrace();
              }
              myHSound.play();
         //PlaySoundXlet playmusic = new PlaySoundXlet();


         }
    }



    the codes are posted above, the key problem is that the sound can not be loaded. when you press ok ,it will generate an error information said there is no protocol in the I/O interface. i don't know why.

    Actually the sound related method is in the playmusic method defined at the bottom.

    thanks.

    william
  • 6. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    class Sounds{
    
    
    public void sound()
    {
         HSound bgHSound = new HSound();
          try {
              bgHSound.dispose();
              bgHSound.load("file:\\D:/xletview-0.3.6/7/bounce.au");
                                }catch (IOException io) {
    System.out.println("Error happened while playing sound. Most probably IOException Error. Check to make sure the sound file is there, it is of the right format, and absolute URL has been used!");
               bgHSound.stop();
                 }
               bgHSound.play();
     //either it plays in loop or plays nothing, by nothing I mean silent
    }
    
    
    
    }
    This should play a sound... Atleast it does for me in MHPXlet... However, in the real settop boxes, you will have to have a mp2 format sound.
    My 2 cent solution! :D Good luck with your project
  • 7. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    hey
    does your code work for drawing other objects.. have you tested with the settop boxes.. and did it work with key presses
  • 8. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    sorry mate, i know it is painful to look at that source code.

    i have fixed the sound problem now, the source codes below should work.

    public void playMusic()
         {
              myHSound = new HSound();
              try
              {
                   url = new URL("file://" + new File(fileName).getAbsolutePath());
                   myHSound.load(url);
                   System.out.println(url);
              }

              catch (IOException ioe)
              {
                   System.out.println("Error: myHSound Could not load sound. Input/output error:" + ioe.getMessage());
              }
              myHSound.play();
         
         }
  • 9. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    I was looking to make a stop in a sound at a particular moment, but when I give stop, the application expects the sound to be finalized after stop(). Somebody can help follows the code below:

    import javax.media.Manager;
    import javax.media.MediaException;
    import javax.media.MediaLocator;
    import javax.media.Player;

    public class PlayMusic {
    private URL url;
    String SOUND_URL = "";
    MediaLocator locator = new MediaLocator(SOUND_URL);
    Player player;
    String musicaSelecionada;
    DataSource playingDataSource;

    public void start(String arq) {


    SOUND_URL = "file:/root/NetBeansProjects/LeituraTudoXml/build/classes/" + arq;
    try {
    locator = new MediaLocator(SOUND_URL);
    playingDataSource = Manager.createDataSource(locator);
    player = Manager.createPlayer(playingDataSource);
    player.addControllerListener(this);
    player.start();
    try {
    Thread.sleep(350);
    } catch (Exception qw) {
    qw.printStackTrace();
    }
    player.setMediaTime(new Time(0));
    player.stop();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (MediaException e) {
    e.printStackTrace();
    }
    }
    }
  • 10. Re: Sound HEEEEELP ! :)
    843851 Newbie
    Currently Being Moderated
    Hi There,
    I am trying to run the wav file on BD, firstly i converted the wav file to bdmv file and now i need to download and play bdmv file. I tried it playing HSound an dPlayer but, no use its not giving any error/exception and its not playing.
    Here is the code that I used, can some body help on this.

    HSound Code
    HSound myHSound = new HSound();
    
    
    try {
    
    
    URL s1 = new URL(Url);
    
    
    myHSound.load(s1);
    
    
    } catch (IOException io) {
    
    
    io.printStackTrace();
    
    
    Log.+error+("", io);
    
    
    }
    
    
    myHSound.play();
    Player Code
     
    
    
    URL s1 = new URL(Url);
    
    
    Player player = Manager.+createPlayer+(s1);
    
    
    player.prefetch();
    
    
    player.start();