SamRowley
SamRowley

Reputation: 3485

Return the length in seconds of a track to be played using AudioTrack

I am trying to impliment what most media players have, a way of displaying the total time for an audio track as well as the time which it is currently at (which is increased every second). I'm pretty sure I have to use threads but i'm not sure how. I have the audio track saved on the sd card and when it comes to be played all the data put into a short array.

Ideally I would also like to be able to show the progress using a progressBar, I don't think it would be a massive step to go from displaying it in TextViews to a progress bar. I have looked around on the internet and all the examples (vague as they are) I have found have used MediaPlayer but as i'm using audioTrack it makes it much more difficult I guess. There is no way in my app I can use MediaPlayer to do what I want to do with the audio.

Can anybody help me out or point me in the right direction?

Upvotes: 3

Views: 1888

Answers (2)

Priyanthi
Priyanthi

Reputation: 1785

float time = (float)file.length()/playbacksamplerate/2;

2 is for turning short to bytes sample rate should be the amount you have set for playback on your audio track object

Upvotes: 1

SamRowley
SamRowley

Reputation: 3485

Seeing as nobody had an answer for my I used a crude method to find the time in Seconds of an audio track. Knowing that I recorded the track myself with settings I gave I knew the bitrate, could get the filesize too.

So creating a File object and then setting that object as the file in storage to get the time in seconds use:

float time = (float)file.length()/22050/2;

Where 22050 is the sample rate, I think, of the recording and 2 I think is because it recorded it in stereo.

Anyway hope this helps anybody in the future and correct me if I am wrong at all please.

Upvotes: 3

Related Questions