【问题标题】:How to add a progress bar when playing a remote mp3如何在播放远程 mp3 时添加进度条
【发布时间】:2012-03-17 09:57:36
【问题描述】:

我正在播放一个远程 mp3 音频文件,我只想添加一个进度条和一个标签来显示剩余时间。

我该怎么做。 我正在使用以下代码播放远程mp3文件。

我的代码链接在这里!!!

http://pastie.org/3613911

【问题讨论】:

    标签: ios audio titanium


    【解决方案1】:

    已编辑

    如果你想显示进度条,那么你需要你的 mp3 文件的总时长。但是使用当前的 Titanium 1.8 SDK,您无法从远程文件 (.mp3) 的 音频播放器 获得 total 持续时间。但是,您仍然可以从后端获得总持续时间。例如,在您的响应中设置总持续时间标签,并在 progress 的最大尺寸中使用它。

    //assing total duration in milliseconds and divide by 1000 from your server
    
    var totalDurationFromBackend = 898 // its in your case.Also divided by 1000 
    
    var audioPlayer = Ti.Media.createAudioPlayer({ 
    
        url: 'http://naturallyimprove.com/mp3/Drone.mp3',
        allowBackground: true
    });
    
    var pb=Titanium.UI.createProgressBar
    ({
       top:10,
       width:250,
       height:'auto',
       min:0,
       max:totalDurationFromBackend,
       value:0,
       color:'#fff',
       style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
    });
    win.add(pb);
    pb.show();
    
    audioPlayer.addEventListener('progress',function(e) 
    {
       Ti.API.info('Time Played: ' + Math.round(e.progress) + ' milliseconds');
    
       // set the progress bar's progress value with current time played.. (milliseconds) 
       pb.value =  Math.round(e.progress/1000) ; 
    });
    
    startStopButton.addEventListener('click',function() {
    
    // When paused, playing returns false.
    // If both are false, playback is stopped.
    if (audioPlayer.playing || audioPlayer.paused)
    {
        audioPlayer.stop();
        pauseResumeButton.enabled = false;
        if (Ti.Platform.name === 'android')
        { 
            audioPlayer.release();
        }   
    }
    else
    {
        audioPlayer.start();
        pauseResumeButton.enabled = true;
    }
     });
    

    另一种选择是:使用Titanium.Media.VideoPlayer 将网址传递给它,您将从远程文件的视频播放器(也是.mp3)获得持续时间并在音频播放器中使用它...

    【讨论】:

    • var audioPlayer = Ti.Media.createAudioPlayer({ url: 'http://naturallyimprove.com/mp3/Drone.mp3', allowBackground: true }); 你能举个例子使用上面创建的音频!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多