在 Javascript 中的函数之间传递变量 (Titanium Studio Desktop)

发布于 2024-11-19 23:09:04 字数 1866 浏览 1 评论 0原文

希望我能很好地阐明这个问题。我正在 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 解决方案。)

Hopefully I can articulate this problem well. I'm working on a simple audio player in Titanium Desktop. The primary code I'm focused on right now is the following:

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();
    }
}

And then related 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()" />

Now, both pickMusicFolder and playSong itself work properly. However, stopPlayback isn't working, I'm having a really hard time grasping how to deal with different functions for playing and stopping audio, since the code that's generating the clickable links is completely compartmentalized within the pickMusicFolder function, while the code for stopping playback is only attached to the one separate interface button.

I simply need access to the songURL variable between multiple functions in order to be able to perform operations on one solitary song while it's playing (or not). I'm avoiding resorting to global variables, as I find that to be sort of a cop out.

Anyone have any ideas? Any tips are much appreciated! (Oh, and please ignore my ugly code; was trying a bunch of hack-y solutions before posting.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2024-11-26 23:09:04

很快的解决方案是将 currentSong 存储为函数 playSong 的属性:

function playSong(songURL){
  var currentSong = Titanium.Media.createSound(songURL);
  playSong.currentSong = currentSong; // store curernt playing song as property of function
  currentSong.play();
}

playSong.stopPlayback = function() {
  if (playSong.currentSong) {
    playSong.currentSong.stop();
    delete playSong.currentSong;
  }
};

当歌曲停止时,只需删除此属性,这意味着现在没有歌曲正在播放

Very quickly solution is to store currentSong as property of function playSong:

function playSong(songURL){
  var currentSong = Titanium.Media.createSound(songURL);
  playSong.currentSong = currentSong; // store curernt playing song as property of function
  currentSong.play();
}

playSong.stopPlayback = function() {
  if (playSong.currentSong) {
    playSong.currentSong.stop();
    delete playSong.currentSong;
  }
};

when song will be stopped just remove this property that would mean no song are playing now

吃→可爱长大的 2024-11-26 23:09:04

你应该使用闭包。请参阅下面的

var stopPlayback = function() { /* Placeholder, defined later */ };

var playSong = function(songURL){
  var currentSong = Titanium.Media.createSound(songURL);
  currentSong.play();

  // Redefine stopSong in the parent scope
  stopPlayback = function(){
    // currentSong is available to this function via closure
    currentSong.stop();
  }
}

HTML 编辑

<input type="image" src="img/player_stop.png" name="stopPlayback" onClick="stopPlayback()" />

作为旁注,您不应该使用 HTML 属性来附加 JavaScript 事件。 这是一个关于事件附件的精彩系列,但您真正应该阅读的是这一部分。此外,每个 JS 库都提供了附加事件的方法,让您的生活更轻松。

You should use a closure. See below

var stopPlayback = function() { /* Placeholder, defined later */ };

var playSong = function(songURL){
  var currentSong = Titanium.Media.createSound(songURL);
  currentSong.play();

  // Redefine stopSong in the parent scope
  stopPlayback = function(){
    // currentSong is available to this function via closure
    currentSong.stop();
  }
}

HTML Edit

<input type="image" src="img/player_stop.png" name="stopPlayback" onClick="stopPlayback()" />

As a side note, you should not be using HTML attributes to attach JavaScript events. This is a great series on event attachment, but what you really should read is this part. Also, every JS library provides methods for attaching events to make your life easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文