您的位置:首页 > 其它

music播放音乐和system中播放控制栏的关联

2014-03-18 16:12 417 查看


主要用到了IAC 通讯机制


 http://tech.mozilla.com.tw/posts/3560/inter-app-communication-app-%E7%9A%84%E6%BA%9D%E9%80%9A%E6%A9%8B%E6%A8%91

在music界面第一页是Tile形式,点击某一首进行播放的click事件在,注意:不能在nightly中同时打两个异步的断点,如下面的1,2,3需要各自单独打断点:music.js

init: function tv_init() {
this.dataSource = [];
this.index = 0;

this.view.addEventListener('click', this);
this.view.addEventListener('input', this);
this.view.addEventListener('touchend', this);
this.searchInput.addEventListener('focus', this);
},
handleEvent: function tv_handleEvent(evt) {
function tv_resetSearch(self) {
evt.preventDefault();
self.searchInput.value = '';
SearchView.clearSearch();
}
var target = evt.target;
if (!target)
return;

switch (evt.type) {
case 'touchend':                 //1,先响应此case
// Check for tap on parent form element with event origin as clear buton
// This is workaround for a bug in input_areas BB. See Bug 920770
if (target.id === 'views-tiles-search') {
var id = evt.originalTarget.id;
if (id && id !== 'views-tiles-search-input' &&
id !== 'views-tiles-search-close') {
tv_resetSearch(this);
return;
}
}

if (target.id === 'views-tiles-search-clear') {
tv_resetSearch(this);
return;
}

break;

case 'click':                      //2,后响应click
if (target.id === 'views-tiles-search-close') {
if (ModeManager.currentMode === MODE_SEARCH_FROM_TILES) {
ModeManager.pop();
}
this.hideSearch();
evt.preventDefault();
} else if (target.dataset.index) {
var handler;
var index = target.dataset.index;

var data = this.dataSource[index];
handler = tv_playAlbum.bind(this, data, index);  //这里有个handler

target.addEventListener('transitionend', handler);
}

break;

case 'focus':
if (target.id === 'views-tiles-search-input') {
if (ModeManager.currentMode !== MODE_SEARCH_FROM_TILES) {
ModeManager.push(MODE_SEARCH_FROM_TILES);
SearchView.search(target.value);
}
}

break;

case 'input':
if (target.id === 'views-tiles-search-input') {
SearchView.search(target.value);
}

break;

default:
return;
}

function tv_playAlbum(data, index) {         //3,对应上面的handler处理,此函数中播放音乐
var key = 'metadata.album';
var range = IDBKeyRange.only(data.metadata.album);
var direction = 'next';

ModeManager.push(MODE_PLAYER, function() {
PlayerView.clean();

// When an user tap an album on the tilesView
// we have to get all the song data first
// because the shuffle option might be ON
// and we have create shuffled list and play in shuffle order
playerHandle = musicdb.enumerateAll(key, range, direction,
function tv_enumerateAll(dataArray) {
PlayerView.setSourceType(TYPE_LIST);
PlayerView.dataSource = dataArray;

if (PlayerView.shuffleOption) {
PlayerView.setShuffle(true);
PlayerView.play(PlayerView.shuffledList[0]);
} else {
PlayerView.play(0);
}
}
);
});

target.removeEventListener('transitionend', handler);
}
}
};

music中communications.js中有:

this.mrc = new MediaRemoteControls();

music:MediaRemoteControls?在中shared目录下的remote_controls.js中,该类中会有_postMessage的动作以和system交互。

MediaRemoteControls.prototype.notifyAppInfo = function(info) {
// Send the app info via IAC.
this._postMessage('appinfo', info);   //system中处理的一种
};
...
// Now, send it via IAC.
this._postMessage('nowplaying', metadata);   //system中处理的一种
...
// Now, send it via IAC.
this._postMessage('status', status);    //system中处理的一种
...
MediaRemoteControls.prototype._postMessage = function(name, value) {
var message = {type: name, data: value};
if (!this._ports) {
if (this._queuedMessages)
this._queuedMessages.push(message);
} else {
this._ports.forEach(function(port) {
port.postMessage(message);
});
}
};

在system的manifest.webapps中有:

"connections": {
"mediacomms": {
"description": "Communication with media apps for now playing info",
"rules": {}
},
"ftucomms": {
"description": "Communicate between communications/ftu and System",
"rules": {}
},
"bluetoothTransfercomms": {
"description": "Communication with bluetooth apps for sending files info",
"rules": {}
}

system中用到shared目录下的iac_handler.js中有,在这里会给mediacomms加上前缀iac-

var IACHandler = {
_eventPrefix: 'iac-',
_ports: {},

init: function onInit() {
var self = this;

window.navigator.mozSetMessageHandler('connection',
function onConnected(request) {
...
var evtName = self._eventPrefix + keyword;
var iacEvt = document.createEvent('CustomEvent');
iacEvt.initCustomEvent(evtName,
/* canBubble: */ true, /* cancelable */ false, message);
window.dispatchEvent(iacEvt);
...

system下lockscreen.js及notification.js中的播放控制器均是media_playback.js中MediaPlaybackWidget的对象,这里会进一步处理经过IACHandler处理过的message

window.addEventListener('iac-mediacomms', this.handleMessage.bind(this));
...
MediaPlaybackWidget.prototype = {
handleMessage: function mpw_handleMessage(event) {
var message = event.detail;
switch (message.type) {
case 'appinfo':
this.updateAppInfo(message.data);
break;
case 'nowplaying':
this.updateNowPlaying(message.data);
break;
case 'status':
this.updatePlaybackStatus(message.data);
break;
}
},

...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: