Does anyone know how to get the duration of a sound file programatically?
I'd like to switch between different background music but I also want to display how much time remains on the currently playing song.
Thanks :)
A fast, easy to use, free, and community supported 2D game engine
Does anyone know how to get the duration of a sound file programatically?
I'd like to switch between different background music but I also want to display how much time remains on the currently playing song.
Thanks :)
If you use Denshion, probably something like
[[[[CDAudioManager] sharedManager] backgroundMusic] duration];
and
[[[[CDAudioManager] sharedManager] backgroundMusic] currentTime];
Yes, ob1 is correct. Or using property notation:
[CDAudioManager sharedManager].backgroundMusic.duration
[CDAudioManager sharedManager].backgroundMusic.currentTime
note that currentTime can be set as well and is used to seek to a certain position within an audio file.
Works great! Thank you :)
Can anyone provide a more fleshed-out example of this in action?
[CDAudioManager sharedManager].backgroundMusic.currentTime
I'm having no luck...I keep getting this error when I try to get or set it (probably a syntax issue knowing my newbeeness): "request for member 'currentTime' in something not a structure or union"
A simple example that CCLOG's the current MP3 running time each frame would be incredibly helpful.
TIA,
André
Thanks CJ...unfortunately that didn't solve the problem. I've already imported CDAudioManager.h (which itself imports AVFoundation/AVFoundation.h) and have successfully begun streaming an MP3 with:
[[CDAudioManager sharedManager] playBackgroundMusic:@"myMusic.mp3" loop:YES];
Still can't figure out a way to use the currentTime property to any extent.
André
@Steve - Awesome, thanks...got it working with the following code:
[[CDAudioManager sharedManager] playBackgroundMusic:@"myTrack.mp3" loop:YES];
[[CDAudioManager sharedManager].backgroundMusic.audioSourcePlayer setCurrentTime: 1.5]; // <-appears to be time in seconds
CCLOG(@"%f",[CDAudioManager sharedManager].backgroundMusic.audioSourcePlayer.currentTime);
I had looked through the source code (trying not to be lazy here), but I'm still wrapping my head around Objective-C syntax. I'm sure I'll be asking more seemingly obvious questions here in the near future, and I appreciate your (and others') patience and your "teach a man to fish" approach to answering our Q's. ;)
André
Okay, so I've just done some testing on this and it appears currentTime is reflecting an inaccurate value once setCurrentTime is set.
In init function:
[[CDAudioManager sharedManager] playBackgroundMusic:@"myTrack.mp3" loop:YES];
[self schedule:@selector(nextFrame:)];
Elsewhere:
- (void) nextFrame:(ccTime)dt {
float mxIndex = [CDAudioManager sharedManager].backgroundMusic.audioSourcePlayer.currentTime;
CCLOG(@"%g", mxIndex);
if (mxIndex >= 5){
[[CDAudioManager sharedManager].backgroundMusic.audioSourcePlayer setCurrentTime: 0];
}
}
With the setCurrentTime and mxIndex max times above, initially after five seconds the track will start over from the beginning, but the currentTime will reflect -5. Each successive restart of the music (which doesn't happen when 5 additional seconds of music have played but rather when the now incorrect currentTime value reads +5) will cause the currentTime value to jump back an additional 5 seconds (to -10, then to -15, then to -20) while the track itself actually starts over from the beginning each time.
Am I doing something wrong here (probably) or is this a bug?
André
What happens if you do rewind instead of setCurrentTime:0 ?
I can confirm the bug occurs on a 3GS with 3.1.3. However, if I pause the background music before setting the currentTime everything seems to work okay. e.g something like
am.backgroundMusic.audioSourcePlayer.pause;
am.backgroundMusic.audioSourcePlayer.currentTime = 0.0f;
am.backgroundMusic.audioSourcePlayer.play;Thanks Steve...your suggestion of pausing before using setCurrentTime seems to be solving the problem (except that I'm unable to resume playback after pausing and resetting...but I'm probably doing something wrong).
I'm working on a music game so achieving tight integration with playback is paramount. Your help (and from others) is greatly appreciated.
André
You must log in to post.