How to play sounds (rightly) in games?

Hello

What is the correct way to play a sound effect in a game (or any application using a wire loop). I use a method that works very well on 99% of the phones, but it seems to cause a small number of phones for my client to freeze after a few sounds in game (I guess it's sort of leak memory). I also noticed that it creates an error in my event log (StreamingException: not logged in net.rim.device.internal.streamingnatives), even if the game continues to function normally.

Obviously something is wrong, but I can't understand what it is. Here is the code I use:

SoundObject.java:

public class SoundObject {
    Player player;
    VolumeControl vc;

    public SoundObject(String res) {
        Class cl = null;
        try {
            cl = Class.forName("my class path goes here");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        InputStream input = null;
        try {
            input = cl.getResourceAsStream(res);
            player = Manager.createPlayer(input,"audio/mpeg");
            player.realize();
            player.prefetch();
            vc = (VolumeControl) player.getControl("VolumeControl");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(input != null){
                    input.close();
                    input = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setVolume(int val){
        try{
            if(this.vc != null){
                vc.setLevel(val);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void Play(){
        if(Settings.getSoundVolume() == 0) return;
        try {
            if(player != null){
                player.stop();
            }
            player.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

To load the sound I do the following:

public static SoundObject sound_boing = new SoundObject("/boing.mp3");
sound_boing.setVolume(50);

And in the thread of my game, I play the sound like this:

sound_boing.Play();

Can someone tell me what I'm doingwrong? Copy the following code causes a memory leak? Y at - it a particular format of the audio file I should avoid? This could be the cause of the StreamingException: Session not open error?

Thank you!

Hi Dekerta!

If I read your code correctly you are creating an InputStream to a sound file, and then close the stream before playing what seems to be the problem.  Calling prefetch below will read the InputStream and pre-load the player with data - if the file is small enough (which I am assuimg that yours is) then it can indeed read the entire file in the stream.

The essential point that you don't know, is that this loading is made asynchronously.  As you close the stream your finally block you introduce a race condition - if it finally is executed before the player is first read then you will get the Exception you see (the stream is not open because you closed it).  If the player completes the preloading before closing the flow then the sound will play, which seems to be the case for 99% of the time .

Rather than close the stream immediately, why not keep it open until the player becomes closed in this way it is always available for the player and is no longer available when you can be sure that the player no longer has need you can save it to a field in your SoundObject and close in a PlayerListener implementation when you get the event PlayerListener.CLOSED

As alternative approach to play 'unique' sounds, check on the sample inthe pos , it is optimized for work for the CDMA and GSM-based devices universally, all keeping as minimal as possible use of the resources and the most effective cleaning possible.  The only other approach I've used that works well is to mix your own sounds, but I won't get it here since it is a topic all by itself (although the DEV04 session of the Conference DevCon2010 in recent years tells you how... just saying ' ).

Tags: BlackBerry Developers

Similar Questions

  • How to play sound headphone only

    Hey guys and girls

    recently, I bought a new headset, and it seems that windows won't play sounds only in the helmet as long as the speakers are turned on.

    in other words, music plays in the two headphones and speakers and not just in the headphones i unless:
    Unplug the speakers
    Turn them off
    disable them with a ridiculous amount of clicks. funny, is that I always forget how I did the last time >.

    So what I want to do, is make headphones have priority on the speakers without turn it off or unplugged.

    I also can't use the connectors on the front panel for a reason even if they appear in the ' Manager high definition realtek audio.

    Hello

    Method 1:

    I suggest to go to sound settings and turn off the speaker device for the only sound from the earpiece. To do so follow the steps below.

    1. click on Start

    2. go to Control Panel then hardware and audio.

    3. click on sound, it will open a window with all audio devices it contained therein.

    4 turn off the speaker by right-clicking on the " speaker " icon and select disable .

    Method 2:

    To make the helmet of your default audio device, follow the procedure below.

    1. Click Start and click on Control Panel.
    2. click on sound, then a new window will open.
    3. in the new window click on the "Playback" tab and right-click in the window and click disabled devices onShow.
    4. now check if headphone is listed there and right-click on it and choose activate.
    5. highlight this headset and click "set as default".

    It will be useful.

  • How to play sounds on Simulator?

    As title, I have speakers on my computer.

    I want to test my application with the playback of sounds.

    How to debug these things like playing a sound.

    I don't like to build and sign and load to the device each time.

    Thank you

    Visit this link: http://today.java.net/pub/a/today/2005/09/27/j2me4.html

  • How to play sound loop

    Hello

    How can I play looping sound? Is it just well SoundChannel class responsible for this or I have to write my own function?

    With the help of the sound object, there is a property, you can move that asks the number of loops of

    Play (StartPosition:Number, Loops:int, SoundTransform)

    or you can create your own method who listens SoundComplete and then starts playing again.  which in my opinion is the best thing to do because when you play a sound with a loop and choose to have is to start at a different position of 0 so the loop will play from where you set the position.

  • How to play sound in AS3

    In AS2, you can listen to sound through s.loadSound("mysound.mp3",true) where the second loadSound parameter indicates that you want the sound.
    How do you do this in AS3?
    Thanks for your help.
    Ken

    When you run your load(), the audio file begins to download. who will continue unless you run a close() method.

    your sound will start playing when you run, you're your play() method or after the bufferTime is met, the first of the two prevailing.

    the real question is how do you keep streaming in as3. and who is easy to aid sound complete event to trigger the play() method. If the play() method is executed while loading is complete AND bufferTime is lower than the charging time, your sound will flow.

  • How to play windows 95 Windows games 8

    I see that there is only a virtual PC provided by Microsoft for Windows 7, but I don't see anything for Windows 8 Professional. Y at - it an and if if some would provide a link.

    Thank you in anticipation of your help.

    Hello Sparrow,

    One that comes with Windows 8 Pro is called Hyper V. If you can use it, of course, will depend on if your system (especially the 64-bit processor) compatible SLAT (Second level Address Translation). If it isn't then you will not be able to install Hyper V.

    To access Hyper V proceed as follows:

    1. open Control Panel

    2. click on programs and features icon

    3. in the window programs and features, look on the left side of the window and click on Windows features turn on or off the link.

    4. in the windows turn on or off features window, look down the list of available programs and put a check mark (tick) by clicking your mouse in the box to the left of the Hyper-v option.

    5. Finally, click the OK button

    6. hyper V will now install. You may need to restart your PC after installation.

    7. when your PC restarts, look for Hyper V icon on the start page.

    You would like to do a Google search to configure Hyper-V because it is a little more complex than the use as Virtual PC.

    Also if you have any other virtual machine installed, for example, VMware Workstation software, you can see that the conflicts of Hyper V. To correct you need to uninstall any other virtual machine software.

    This forum post is my own opinion and does not necessarily reflect the opinion or the opinion of Microsoft, its employees or other MVPS.

    John Barnett MVP: Windows XP Expert associated with: Windows Expert - consumer: www.winuser.co.uk | vistasupport.mvps.org | xphelpandsupport.mvps.org | www.silversurfer-Guide.com

  • How to play a sound on a button to the bottom of the State?

    Hello

    That is my question. How to play sound on CLICK or down state of the button? I don't know how to slap a sound on the timeline, but not how to put specifically for the button and that's down from the State.

    Thanks for the help in advance.

    go to the scenario of the button and attach a sound to its low chassis.   You probably want the sync event property.

  • Play sounds in the order using MediaPlayer

    Hello. How to play sounds in order?

    For now, here are my code.

    I have a MediaPlayer component as an attached object

    http://developer.BlackBerry.com/native/reference/Cascades/bb__multimedia__mediaplayer.html

    attachedObjects :[
        MediaPlayer{
           id: sound
           sourceUrl: "asset:///sounds/sound1.mp3"
        }
    ]
    

    Then I have a button. Here is what my code for it

    Button{
       id: play
       text: "Play Sequence"
    
       onClicked :{
    
          sound.sourceUrl = "asset:///sounds/sound1.mp3"
          sound.play();
          sound.sourceUrl = "asset:///sounds/sound2.mp3"
          sound.play();
          sound.sourceUrl = "asset:///sounds/sound3.mp3"
          sound.play();
    
       }
    }
    

    What goes wrong.

    The code runs after 1 second about, cut the next sound. I'm doing his next game, after the end of his previous.

    The sounds are not finished because play() is not a call blocking. Once you call the play() function it will start playing the sound and return immediately to the following line of code in the QML. If the next line is a call to the play() still it will have precedence over previous sound, i.e. If you think logically about what you need in an application, you would not have the sound of a doorbell 2 seconds later, play just because you play already a sound, you would stop, as it does.

    My suggestion to use a selection intended to use the MediaPlayer playlist function, that you implemented will obviously suffer from the same problem for the above reason. A playlist is a separate file that contains a list of media you want to play and can be in different formats.

    My other suggestion of using onPlaybackCompleted does not seem to have been followed by you, but is probably the easiest to implement.

    MediaPlayer {
        id: mediaPlayer
    
        onPlaybackCompleted: {        count = count + 1;
            var s = count;
            mediaPlayer.setSourceUrl("asset:///sounds/sound" + s + ".mp3");
            mediaPlayer.play();
        }
    
    }
    

    Where count is a comprehensive, defined property to 1 in your QML file and you have some relaxation (onCreationCompleted or clicking a button) to start the first play().

  • How to play audio through the headphones and computer speakers, at the same time.

    Hey I just need to know how to play sounds through the headphones and speakers. Help

    Depends on your audio device.

    If its edge, while normally when you plug the headset switch will cut off the speakers.

    If you have a USB or Firewire pro device you can listen to your headphones as the signal played through your speakers.

    See you soon,.
    Jerry

  • Windows Movie Maker plays sound but no picture for the Sims 2 movies after installing the codec vp6vfw.dll - how to fix Movie Maker?

    Sims 2 was not properly installing its codec vp6vfw.dll film so I copy and pasted from Sims 1 CD in my windows > System 32.  Now when I play the Sims 2 game I can make movies.  They appear in Windows Media Player very well.  But they do not visually show in Windows Movie Maker.  I get sound.  How can I fix Windows Movie Maker to display my Sims 2 movies?

    I found the answer.  Windows Movie Maker is no longer supported.  The new version is Windows Live Movie Maker, and it has all the appropriate codecs.  Download that and you're good!

    WMM is trash but it can not be uninstalled from the PC.  Just ignore it and use WLMM.
  • How to make sound to play through my headphones and not my TV when you use an HDMI cable to extend my desktop?

    Often, I connect my laptop to my TV to extend my desktop and find work more quickly. Today, I wanted to listen to music then I worked, but did not want to disturb others in the House with the sound so I plugged my headphones on my laptop but the sound plays through television. How can I get it to play through my headphones only?

    I do not use HDMI but audio HDMI runs your graphics instead of the audio chip

    the laptop, so I think that you can turn off the HDMI audio in your graphics cards

    The control panel settings.

    -If not, you should be able to disable the HDMI audio via Device Manager.

    The HDMI audio will probably be known as your graphics (Nvidia, AMD or Intel).

    [Type Device Manager in the search bar on the Start Menu and select - sound, video and game controllers.] R / click on the appropriate audio device and select disable. ]

    Or - connect your headphones to the TV should turn off the sound from the speakers.

    You can purchase an extension for headphones, if necessary.

  • Music and sounds of the game won't play; all other sounds work

    I have an iPod 5th generation touch. I checked my volume and the volume of ringtone and leaves. All the sounds are turned on. I checked for if sure speaker working, (it does because the music plays and keypad lock and sounds of device clicks), and I checked in game controllers and all sense I could think of to make sure sounds is enabled. I check everywhere he looks good, but the sounds of the game will not play AGAIN. I'm starting to think that maybe it's a problem of material with my iPod touch 5g because I also have a 6g-key and everything works on it just. Does anyone have any ideas if I could solve this problem?

    Try:

    -Reset the device iOS. Nothing will be lost

    Device iOS Reset: Hold down the On / Off button and the Home button at the same time for to

    ten seconds, until the Apple logo appears.

    -Reset all settings

    Go to settings > general > Reset and tap reset all settings.

    All your preferences and settings are reset. Information (such as your contacts and calendars) and media (such as songs and videos) are not affected.

    -Restore from backup. See:

    Restore your device from an iCloud or iTunes backup - Apple Support

    https://support.Apple.com/en-us/HT204184

    -Restore factory settings/new iOS device.

    If still problem, make an appointment at an Apple store Genius Bar because it appears that you have a hardware problem.

    Shop Apple Store - Genius Bar

    https://www.Apple.com/retail/Geniusbar/

  • How can I get iPhone play sound of favorite when text DND?

    Hello

    How can I get iPhone play sound of favorite when text DND?

    It plays the ringtone for a call when DND of a favorite but not identified a favorite texts me

    Thanks in advance

    You do not have. Notifications for all the messages are deleted when DND is turned on.

  • How to play games windows 95 Windows 7

    Original title: How do I play games windows 95 under windows 7. I think I have the 64 bit version. I've tried troubleshooting for older programs tool, but nothing does not open. Can I install a second operating system?

    How to play games windows 95 under windows 7. I think I have the 64 bit version. I've tried troubleshooting for older programs tool, but nothing does not open. Can I install a second operating system?. The game I want to play is the 25th anniversary of star trek

    Hi alecfss,

    Well, I don't think that it is possible to play games Windows 95 to Windows 7.

    1. When you mention "install a second operating system", are you referring to the installation of a program, something of the stature of the virtual XP 95 games?

    In this case, download and install Virtual XP , from this link and run Windows 95 games that is better equipped to manage the games for windows 95/98.

    2 Alternatively, are you referring to the installation of an operating system to dual boot?

    See also the article below to see which edition of Windows 7, since you are not sure which edition you are using.

    Find which edition of Windows 7 you are using
    http://Windows.Microsoft.com/en-us/Windows7/find-which-edition-of-Windows-7-you-are-using

    Aziz Nadeem - Microsoft Support

    [If this post was helpful, please click the button "Vote as helpful" (green triangle). If it can help solve your problem, click on the button 'Propose as answer' or 'mark as answer '. [By proposing / marking a post as answer or useful you help others find the answer more quickly.]

  • Please can someone help me with simple instructions on how to play and run and old game back?

    I can't work for the life of me how to play the Pandora Directive, an old game in windows 7.

    I tried the compatibility program, but he has a problem with Vesa?

    Please can someone help?

    Thank you

    Try to install the 3rd party program dosbox. http://www.dosbox.com/ it will emulate a complete enviroiment of ms - dos for the game. If you're playing the support.http://www.gog.com/en/support/ here contact gog.com edition

Maybe you are looking for