In our Scope PreviewWidget, we can use the Audio PreviewWidget to play our music. This is very important for some musical scopes. In today’s exercise, we’ll use this to try out our music in our Scope.
\
First let’s take advantage of the Scope we have already made. We can download the Scope we made earlier:
\
Github.com/liu-xiao-gu…
\
In our query.cpp, we 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(a);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(a); r["emblem"] = icons_[1].toStdString(a); r["background"] = background_.toStdString(a); 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(a); sc::VariantBuilder builder; builder.add_tuple({{"value".Variant(both)}
});
builder.add_tuple({{"value".Variant("")}}); r["attributes"] = builder.end(a); r["musicSource"] = "Http://qqmp3.djwma.com/mp3/ the magic sound of god according to gourmet bootleg that saw the eardrum is broken. Mp3." ";
if(! reply->push(r))
return;
}
Copy the code
\
“MusicSource” above is our new addition. We must point out that “musicSource” is not an item in our standard template, so how do we leverage this item in 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(a);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 });
}
Copy the code
}
Copy the code
Here, we can see:
Result result = PreviewQueryBase::result(a);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 });
}
Copy the code
We can get result by using the “result()” method. 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 and listen to it!
\
The source of the whole project is at: https://github.com/liu-xiao-guo/scopetemplates_audio.git
\
\