Monday, May 13, 2013

A little bit of reusable code Sound Button

Want a button for Ubuntu Components that plays a sound when pressed, and stops when released? Here's SoundButton . Perfect for your typical Sound Board type app.


import QtQuick 2.0
import Ubuntu.Components 0.1
import QtMultimedia 5.0

    Button
    {
        id: soundButton
        property string soundUrl: ""
        onPressedChanged:
        {
            if(pressed)
            {
                audio.play()
            }
            else
            {
                audio.stop()
            }
        }

    Audio
    {
        id: audio
        source:soundButton.soundUrl;
    }
}


27 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey Rick,

    I would do it slightly more QML'ish:

    import QtQuick 2.0
    import Ubuntu.Components 0.1
    import QtMultimedia 5.0

    Button {
    id: soundButton
    property string soundUrl: ""

    Audio {
    id: audio
    source:soundButton.soundUrl;
    playbackState: soundButton.pressed ? Audio.PlayingState : Audio.StoppedState
    }
    }

    try to use as little javascript as possible to speed up your QML application.

    ReplyDelete
    Replies
    1. Thanks Micahel, that is more QMLish. I still have not shaken off my old habits of responding to signals instead of binding to properties. On the other hand, I wonder if my original code might be a bit easier to read?

      Delete
    2. Being the QML newbie that I am, I don't find it hard to read once it's formatted as yours (indent et.al.)

      Delete
  3. Nice code simplification Micahel!
    Although, be careful not to make people think the app will be any faster that way. Reducing the size of the JavaScript will impact performance positively if this code is called very frequently, for example as part of an animation.
    In the case above, it will not make any difference.

    ReplyDelete
  4. Yep, agreed. It doesn't make any real difference here. Its still a good idea to get used to this declarative way of doing things. Its easier to modify the code to add additional states without ending up in endless if/else trees.

    ReplyDelete
  5. Very nice and great article it's also helpful information of all us so thanks a lot for sharing us
    clipping path service

    ReplyDelete

  6. I really appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it
    clipping path service provider
    clipping path service
    Background Removal

    ReplyDelete
  7. Latex froth isn't in fact a kind know this of adjustable foam, know and it doesn't comply with a sleeper a similar way adaptable foam does, however it is rich and agreeable. The key advantage of latex froth is that it's how about this hypoallergenic and antimicrobial, grab this so it's extraordinary for individuals with dust sensitivities. Be that as it may, you shouldn't get a latex sleeping cushion in case you're got this oversensitive to latex. Searching for the best flexible got this foam sleeping cushion for you? We have this you secured. Supplanting your old, destroyed bedding with another adjustable foam sleeping pad is read this a speedy method to at long last get know this the profound, tranquil rest you need.

    ReplyDelete
  8. Reusing codes in programming is a very smart strategy that not only saves you time but also avoids mistakes in writing code. Dissertation Proposal Writing Services

    ReplyDelete
  9. Are you facing difficulties in locating Dissertation Help Online? Do you need online marketing Dissertation Help to submit your assignment on the scheduled time? We at Gotoassignmenthelp.com are committed to providing high-grade services to all students in Australia. You may be struggling to decipher and understand the geographic units of analysis or narrow down the focus of research in a much more reasonable level. Writing a marketing Science Assignment Help is difficult especially if you amateur so our Coursework Help is always ready to help even at the last moment. If you are struggling to complete your assignment due to an emergency or any other hurdles. Call us for one-stop solution inwriting services

    ReplyDelete
  10. I love your content and the lessons you share with your readers. Every time I read a post, best forum I feel like I am able to take advantage of this post,

    ReplyDelete
  11. If you are looking for Free Spins or Free Bets you are in the right place. Here you may find a great collection of Best Online Casino and Bookmaker Offers. Also, the newest Games and Sporting Sites Reviews are waiting for you. Join us to see more.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. In the Color Pink bonus the Pink Panther paints a wall. All the pink numbers are totaled to give the player’s cash prize. Wheel of Pink Bonus is another exciting game. The wheel has two layers. The outer one gives the multiplier.

    야한소설
    오피헌터
    타이마사지
    안마
    카지노사이트존

    ReplyDelete
  14. Hi there

    Nice post and blog, keep sharing the best content, hope to read more interesting articles like this one,

    take care and regards

    Your follower

    Salvatore from Macuco Safari Foz do Iguaçu

    ReplyDelete