Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:14:12+00:00 2026-06-15T17:14:12+00:00

Hi i’m trying to read the data in a wav file so that i

  • 0

Hi i’m trying to read the data in a wav file so that i can plot it as a waveform. Following is the code that i have tried:

    try {
        RandomAccessFile file = new RandomAccessFile(myFile,"r");
        while(file.read()>-1){
         byte b1 = (byte) file.read();
         byte b2 = (byte) file.read();
         Log.d(TAG,"DATA:"+ (double) (b2 << 8 | b1 & 0xFF) / 32767.0);
        }
        file.close();

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

My output is a value that ranges between -1 and +1 initially but later it gives only DATA:0 it seems like an infinite loop and there is a force close. According to me the loop should terminate when the end of file is reached. Can anyone pls suggest some terminating condition for the loop. Please Help…. Thanks is advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:14:14+00:00Added an answer on June 15, 2026 at 5:14 pm

    This code snippet has the following issues:

    1) Using file.read()>-1 in the while loop you still read bytes, so this code reads 3 bytes on every iteration.

    2) WAV files may be (and very often are) RIFF containers with WAV blocks. Also, it is not guaranteed that values are encoded in integers (I’ve seen float encoding in WAV). Byte order seems to be ok, but containers’ metainformation will be printed also.

    I’m not an expert in Java, but running your expression with bytes (-1, -1) gives a value very close to 0.

    To check for file termination, check if any of bytes are -1: while ((b1 != -1) && (b2 != -1))

    Also, the conversion from int to byte seems to be wrong: byte is signed, so you’ll get values from -128 to 127, which is not what you want (0..255).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.