Use audio PreviewWidget to play music in the Scope, previewwidgetscope

Source: Internet
Author: User

Use audio PreviewWidget to play music in the Scope, previewwidgetscope

In our Scope PreviewWidget, we can use audio PreviewWidget to play our music. This is very important for some music scopes. In today's practice, we use this to listen to our music in our Scope.


First, we will use the Scope we have prepared. We can download the previously prepared Scope:


Git clone https://gitcafe.com/ubuntu/scopetemplates.git


In our query. cpp, add the following items:

Query. cpp

Void Query: pushResult (SC: SearchReplyProxy const & reply, const string renderer, int I) {stringstream ss; ss <I; string str = ss. str (); auto cat = reply-> register_category ("id" + str, "Template" + str, "", SC: CategoryRenderer (renderer); SC :: categorisedResult r (cat); r. set_uri (URI. toStdString (); r. set_art (images _ [0]. toStdString (); r ["subtitle"] = "Subtitle" + str; r. set_title ("Title "+ Str); r [" summary "] =" Summary: "+ str; r [" fulldesc "] =" fulldesc: "+ str; r ["mascot"] = icons _ [0]. toStdString (); r ["emblem"] = icons _ [1]. toStdString (); r ["background"] = background _. toStdString (); r ["overlay-color"] = "# FF0000"; QString likes = QString ("% 1% 2 "). arg (qstr (u8 "\ u261d"), "100"); QString views = QString ("% 1% 2 "). arg (qstr (u8 "\ u261f"), "99"); std: string both = qstr ("% 1% 2 "). arg (Likes, views ). toStdString (); SC: VariantBuilder builder; builder. add_tuple ({"value", Variant (both)}); builder. add_tuple ({"value", Variant ("")}); r ["attributes"] = builder. end (); r ["musicSource"] = "http://qqmp3.djwma.com/mp3/the magic sound is an excellent private goods. This saw-pull ear mask is broken down"; if (! Reply-> push (r) return ;}


The above "musicSource" is a newly added item. We must note that "musicSource" is not an item in our standard template. How can we use this item in our Preview?

Preview. cpp
void Preview::run(sc::PreviewReplyProxy const& reply) {    // Support three different column layouts    sc::ColumnLayout layout1col(1), layout2col(2), layout3col(3);    // We define 3 different layouts, that will be used depending on the    // device. The shell (view) will decide which layout fits best.    // If, for instance, we are executing in a tablet probably the view will use    // 2 or more columns.    // Column layout definitions are optional.    // However, we recommend that scopes define layouts for the best visual appearance.    // Single column layout    layout1col.add_column( { "image", "header", "summary", "tracks" });    // Two column layout    layout2col.add_column( { "image" });    layout2col.add_column( { "header", "summary", "tracks" });    // Three cokumn layout    layout3col.add_column( { "image" });    layout3col.add_column( { "header", "summary", "tracks" });    layout3col.add_column( { });    // Register the layouts we just created    reply->register_layout( { layout1col, layout2col, layout3col });    // Define the header section    sc::PreviewWidget header("header", "header");    // It has title and a subtitle properties    header.add_attribute_mapping("title", "title");    header.add_attribute_mapping("subtitle", "subtitle");    // Define the image section    sc::PreviewWidget image("image", "image");    // It has a single source property, mapped to the result's art property    image.add_attribute_mapping("source", "art");    // Define the summary section    sc::PreviewWidget description("summary", "text");    // It has a text property, mapped to the result's description property    description.add_attribute_mapping("text", "description");   Result result = PreviewQueryBase::result();    PreviewWidget listen("tracks", "audio");    {        VariantBuilder builder;        builder.add_tuple({            {"title", Variant("This is the song title")},            {"source", Variant(result["musicSource"].get_string().c_str())}        });        listen.add_attribute_value("tracks", builder.end());    }    if ( result["musicSource"].get_string().length() != 0 ) {        qDebug() << "it is not null";        // Push each of the sections        reply->push( { image, header, description, listen });    } else {        // Push each of the sections        reply->push( { image, header, description });    }
}
Here, we can see: 
   Result result = PreviewQueryBase::result();    PreviewWidget listen("tracks", "audio");    {        VariantBuilder builder;        builder.add_tuple({            {"title", Variant("This is the song title")},            {"source", Variant(result["musicSource"].get_string().c_str())}        });        listen.add_attribute_value("tracks", builder.end());    }    if ( result["musicSource"].get_string().length() != 0 ) {        qDebug() << "it is not null";        // Push each of the sections        reply->push( { image, header, description, listen });    } else {        // Push each of the sections        reply->push( { image, header, description });    }

We can use the "result ()" method to obtain the result. We also created a PreviewWidget called listen. We use it to create the items we need.


Run our Scope:




We can click the button above to play our music for audition!


Source code for the entire project in: git clone https://gitcafe.com/ubuntu/scopetemplates_audio.git


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.