我正在播放一个远程mp3音频文件,我只想添加一个进度条和一个标签,以显示还有多少时间。
我怎么能这么做。我使用下面的代码来播放远程mp3文件。
我代码的链接在这里!
发布于 2012-03-17 14:18:23
编辑的
如果要显示进度条,则需要mp3文件的总持续时间。但是,使用当前的钛1.8SDK,您无法从远程文件()的音频播放器 (.mp3)中获得的总持续时间。但是,您仍然可以从后端获得总持续时间。例如,在您的响应中设置总持续时间标记,并在进度的最大大小中使用它。
//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
将url传递给它,您将从远程文件的视频播放器(.mp3)获得持续时间,并在音频播放器中使用它.
https://stackoverflow.com/questions/9749002
复制相似问题