在我们的Scope PreviewWidget,我们可以利用audio PreviewWidget来播放我们的音乐。这对一些音乐的Scope来说,非常中要。在今天的练习中,我们来利用这个它来在我们的Scope中来试听我们的音乐。
首先我们来利用我们已经做好的Scope。我们可以下载我们先前做好的Scope:
https://github.com/liu-xiao-guo/scopetemplates
在我们的query.cpp中,我们来添加如下的项:
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/魔音神据极品私货这锯子拉的耳膜都要碎了.mp3";
if (!reply->push(r))
return;
}
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 });
}
}
在这里,我们可以看到:
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 });
}
我们可以通过“result()”的方法来得到result。我们同时创建了一个叫做listen的PreviewWidget。我们利用他来创建我们所需要的项。
运行我们的Scope:
我们可以点击上面的按钮来播放我们的音乐来试听!
整个项目的源码在: https://github.com/liu-xiao-guo/scopetemplates_audio.git