如何在Titanium中播放时返回5秒,点击按钮会发生这种情况

时间:2011-06-14 05:29:12

标签: ios audio titanium playback duration

我有以下代码。

var file = recording.stop(); 
sound = Titanium.Media.createSound({sound:file});

sound.play();

sound.getTime(); // This will return the current time position of the audio.

var goBack = Titanium.UI.createButton({title:'Go Back',height:40,width:200,top:230});
goBack.addEventListener('click',function(){
      if(sound.playing){
        Ti.API.info(sound.getTime()); // If this prints 25 secs.
       }
});
win.add(goBack);

假设当前getTime()返回25并且我需要在按钮点击时播放20秒内的音频。

我该怎么做?

我可以使用这样的东西

setTime = sound.getTime() - 5

但是我们如何将浮点值传递给sound.setTime?

还有没有办法添加搜索栏来指示声音对象的播放状态?

1 个答案:

答案 0 :(得分:1)

您可以像这样设置时间值:

sound.time = 20.0; // assuming that you want to start the sound from 20th second.

您可以添加如下进度条:

     var pb = Titanium.UI.createProgressBar({
            min:0,
            value:0,
            width:200
      });

     //when you play the sound set
     pb.max = sound.duration;

    // when your sound is complete

    sound.addEventListener('complete', function()
    {
        pb.value = 0;
    });

    // and also add this code to your file

    //INTERVAL TO UPDATE PB


    var i = setInterval(function()
    {
        if (sound.isPlaying())
        {
            Ti.API.info('time ' + sound.time);
            pb.value = sound.time;

        }
    },500);


    //  CLOSE EVENT - CANCEL INTERVAL
    win.addEventListener('close', function()
    {
        clearInterval(i);
    });