# mp3 accurate duration



## perrce (Feb 8, 2007)

I'm trying to figure out how to get accurate information from the server about the duration of mp3 files. From reading the HME Developer's Guide, it appears that I need to use the Factory.addHeaders() method to add a X-Tivo-Accurate-Duration HTTP header. It points to the sample music app for an example implementation. Problem is, the sample app doesn't actually implement that feature (as far as I can tell).

I can probably figure out how to include the header, once I find the duration, but I have no idea how to find the duration of the mp3 in the first place.

Does anyone have some sample code that finds the duration of an mp3 and includes the appropriate header?


----------



## perrce (Feb 8, 2007)

For the benefit of those who come later and have the same problem, I found a solution for finding the length of an mp3 using the JLayer1.0 mp3 library (http://www.javazoom.net).

Here's the code that I added to the addHeaders() method of my factory:


```
protected void addHeaders(IHttpRequest http, String uri)throws IOException {
			if (uri.toLowerCase().endsWith(".mp3")) {
				long duration = -1;
				
				
				// Open mp3 file
				File file = new File(this.getPreferences().getMusicRoot(), uri);
				
				// Open input streams so that the mp3 header can be examined
				FileInputStream fis;
				try {
					fis = new FileInputStream(file);
				}
				catch(FileNotFoundException e) {
					return;
				}
				
				try {
					BufferedInputStream bis = new BufferedInputStream(fis);

					try {
						Bitstream stream = new Bitstream(bis);
						try {
							Header header;
							try {
								header = stream.readFrame();
							}
							catch(BitstreamException e) {
								return;
							}
							// get the length of the mp3 in milliseconds
							duration = (long)header.total_ms((int)file.length());
							
						}
						finally {
							stream.closeFrame();
						}

					}
					finally{
						try {
							bis.close();
						}
						catch(IOException e) {
						}
					}
				}
				finally {
					try {
						fis.close();
					}
					catch(IOException e) {
					}
				}
				
				if (duration != -1) {
					
					http.addHeader(IHmeConstants.TIVO_DURATION, String.valueOf(duration));
				}
			}
			super.addHeaders(http, uri);
		}
	}
```
I still have a problem though. Even though I'm sending the X-Tivo-Accurate-Duration header, the duration I'm sending isn't being reported in RSRC_STATUS_PLAYING event as the developer guide says it should. Instead, it looks like it's using a default value of 180000 milliseconds. (Maybe I'm just being naive in expecting the documentation to accurately reflect the code!  )

Is there something else I need to do in addition to sending the X-Tivo-Accurate-Duration header for the correct length to be reported?


----------

