Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

How to play a sound within a view

I need to play a sound file from inside a view. I can use "sound.play();" to play the sound in the window, but I need it to play from the view. I hope someone will be able to help with this. My code is below…

    var imageView = Titanium.UI.createImageView({
        image:myImage,
        top:20,
        width:300,
        height:300
    });

sound = Titanium.Media.createSound({url:mySound});


var view = Titanium.UI.createView({
    top:20,
    width:300,
    height:300
    });


view.add(imageView);
view.play(sound);
win.add(view);
— asked November 17th 2010 by David Dushok
  • iphone
  • sound
  • view
0 Comments

7 Answers

  • I think you should be able to fire an event easily from within the view, and then his event will call a custom handler. I did this for my latest app, works fine, I can fire the event from anywhere within the app (even from within an onClick event in the view HTML).

    // make sure that sound effects do not stop the users itunes music
    Titanium.Media.defaultAudioSessionMode = Titanium.Media.AUDIO_SESSION_MODE_AMBIENT;
    
    // method for loading sounds
    var soundArray = [];
    function loadSound(soundName) {
        soundArray[soundName] = Titanium.Media.createSound({
            url:'sounds/'+soundName+'.mp3', 
            preload:true 
        });
    }
    
    // now load the sounds
    loadSound('chime');
    loadSound('bell');
    
    // event listener for playing any sound
    Ti.App.addEventListener('playSound', function(data) {
        if (soundArray[data.sound] != null) {
            soundArray[data.sound].play();
        }
    });
    

    And then from anywhere else:

    Ti.App.fireEvent("playSound", { sound: "chime"} );
    
    — answered November 19th 2010 by Todd Trann
    permalink
    1 Comment
    • Not advisable to fire events with Ti.App… too many of these will slow your app.

      — commented May 22nd 2013 by Joseph Sachs
  • you can just do this, the documentation states this is just for basic sounds and recommends Titanium.Media.createAudioPlayer for streaming or longer media files.

    sound = Titanium.Media.createSound({url:mySound});
    sound.play();
    sound.release();
    
    — answered November 17th 2010 by Aaron Saunders
    permalink
    0 Comments
  • sound.play(); works fine as stated above. But what I need is for the sound to play from within the view. The reason being that the view contains an event listener that when clicked will refresh the image and the sound.

    Do you know how to play the sound from the view and not from the window?

    — answered November 17th 2010 by David Dushok
    permalink
    3 Comments
    • David

      So we have a bit of insight into what you are trying to achieve, would you explain which event listeners you are specifically interested in that can be provided by a view but not a window?

      — commented November 17th 2010 by Paul Dowsett
    • @Hal thx for the comment, there appears to be a lack of clarity in alot of the questions posted here

      — commented November 17th 2010 by Aaron Saunders
    • heh, Aaron, I have seen so many of your answers, and I have to say that I admire your patience sometimes. :)

      — commented November 17th 2010 by Paul Dowsett
  • from within your event listener you could fire a custom event that plays the sound

    — answered November 17th 2010 by ben nduati
    permalink
    0 Comments
  • Hal,

    perhaps you are right about using a window instead of a view. To better explain this I drew a picture.

    Picture

    If you know a better way of achieving the same thing, please let me know. I appreciate all your help.

    Regards,

    Dave

    — answered November 18th 2010 by David Dushok
    permalink
    0 Comments
  • Hi David

    Assuming that you wanna play the sound when the View pops up…., hope the code will help you…

    function playSound(url){
     var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,url);
     var sound = Titanium.Media.createSound({
            sound:file,
            volume:1.0
        });
      sound.addEventListener('complete',function(e){
            Ti.API.info('sound played');
        });
        sound.play();
    }
    
    var imageView = Titanium.UI.createImageView({
            image:myImage,
            top:20,
            width:300,
            height:300
        });
    playSound(soundURL);
    win.add(imageView);
    win.open();
    });
    

    NOTE
    First time playing the sound takes few seconds to play the sound…

    — answered November 19th 2010 by Satta Ravi
    permalink
    0 Comments
  • Double Post…

    — answered November 18th 2010 by David Dushok
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.