利用audio PreviewWidget在Scope中來播放音樂,previewwidgetscope
在我們的Scope PreviewWidget,我們可以利用audio PreviewWidget來播放我們的音樂。這對一些音樂的Scope來說,非常中要。在今天的練習中,我們來利用這個它來在我們的Scope中來試聽我們的音樂。
首先我們來利用我們已經做好的Scope。我們可以下載我們先前做好的Scope:
git clone https://gitcafe.com/ubuntu/scopetemplates.git
在我們的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;}
上面的“musicSource”是我們新添加的項。我們必須指出的是,“musicSource”不是我們標準的模版中的項,那麼我們怎麼在我們的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 }); }}
在這裡,我們可以看到:
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:
我們可以點擊上面的按鈕來播放我們的音樂來試聽!
整個項目的源碼在:git clone https://gitcafe.com/ubuntu/scopetemplates_audio.git