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.jsinit: 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; } }, ...
相关文章推荐
- seekbar 控制音乐播放进度~之musicplay与seekbar关系
- 【Android】如何用听筒播放音乐,并且按键控制音量
- js控制播放音乐 setTimeout与setInterval的区别
- 闹钟AlarmAndMusic 和支持播放音乐效果的
- 闹钟AlarmAndMusic 和支持播放音乐效果《IT蓝豹》
- Android音乐播放器---实现Notification控制音乐播放
- 在当前应用的Activity 中如何对正在播放的music 进行控制
- Android实现音乐播放 Music 详解
- 微信小程序之音乐控制播放(audio的API)
- 在当前应用的Activity 中如何对正在播放的music 进行控制
- 用checkbox控制音乐的暂停与播放,同时音乐要循环播放(可以利用它当个报警提醒喔)
- Android音乐播放器---实现Notification控制音乐播放
- C#、VB.NET 使用System.Media.SoundPlayer播放音乐
- android 音乐、铃声播放控制相关问题
- 有趣的vi-vim页面内的音乐播放控制
- 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制
- [RK3288][Android6.0] 按键(MediaButton)控制音乐播放功能
- 用button简单控制音乐的播放与暂停
- js(jquery)控制 midea player 播放器(控制页面音乐播放)