【发布时间】:2011-07-15 08:36:23
【问题描述】:
希望我能很好地表达这个问题。我正在 Titanium Desktop 中开发一个简单的音频播放器。我现在关注的主要代码如下:
function pickMusicFolder (){
var win = Titanium.UI.getCurrentWindow();
win.openFolderChooserDialog(function(folderResponse) {
var file = Titanium.Filesystem.getFile(folderResponse[0]);
var listing = file.getDirectoryListing();
for (var i = 0; i < listing.length; i++) {
if (listing[i].isDirectory()) {
// if the listing is a directory, skip over it
$('#main').append('DIRECTORY: ' + listing[i].nativePath() +
'<br />');
// continue;
}
else {
// otherwise, print the filename of the file to the #main content window
var songOnList = listing[i].nativePath();
var songURL = songOnList.replace(/\\/g,"/");
$('#main ul').append('<li><a href="javascript:playSong(\'' + songURL + '\')">' + songURL + '</a></li>');
}
}
});
};
function playSong(songURL){
var currentSong = Titanium.Media.createSound(songURL);
currentSong.play();
this.stopPlayback = stopPlayback;
function stopPlayback(currentSong){
currentSong.stop();
}
}
然后是相关的HTML:
<input type="image" src="img/player_stop.png" name="stopPlayback" onClick="playSong.stopPlayback(songURL)" />
<input type="image" src="img/folder_add.png" name="pickMusicFolder" onClick="pickMusicFolder()" />
现在,pickMusicFolder 和 playSong 本身都可以正常工作。但是,stopPlayback 不起作用,我很难掌握如何处理播放和停止音频的不同功能,因为生成可点击链接的代码在 pickMusicFolder 函数中完全划分,而用于停止的代码播放仅附加到一个单独的界面按钮。
我只需要访问多个函数之间的 songURL 变量,以便能够在播放(或不播放)一首单独的歌曲时对其执行操作。我正在避免使用全局变量,因为我发现这有点逃避现实。
有人有什么想法吗?非常感谢任何提示! (哦,请忽略我丑陋的代码;在发布之前尝试了一堆 hack-y 解决方案。)
【问题讨论】:
标签: javascript jquery titanium appcelerator