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 6955039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:41:06+00:00 2026-05-27T14:41:06+00:00

i’m programming for Android 2.1.Could you help me with the following problem? I have

  • 0

i’m programming for Android 2.1.Could you help me with the following problem?

I have three files, and the general purpose is to play a sound with audiotrack buffer by buffer. I’m getting pretty desperate here because I tried about everything, and there still is no sound coming out of my speakers (while android’s integrated mediaplayer has no problem playing sounds via the emulator).

Source code:

An audioplayer class, which implements the audio track. It will receive a buffer, in which the sound is contained.

    public AudioPlayer(int sampleRate, int channelConfiguration, int audioFormat) throws ProjectException {
        minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfiguration, audioFormat);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfiguration,
                                    audioFormat, minBufferSize, AudioTrack.MODE_STREAM); 

        if(audioTrack == null)
            throw new ProjectException("Erreur lors de l'instantiation de AudioTrack");

        audioTrack.setStereoVolume((float)1.0, (float)1.0);
    }

    @Override
    public void addToQueue(short[] buffer) {
         audioTrack.write(buffer, 0, buffer.length*Short.SIZE);
         if(!isPlaying ) {
             audioTrack.play();
             isPlaying = true;
         }
    }

A model class, which I use to fill the buffer. Normally, it would load sound from a file, but here it just uses a simulator (440Hz), for debugging.

Buffer sizes are chosen very loosely; normally first buffer size should be 6615 and then 4410. That’s, again, only for debug.

public void onTimeChange() {
    if(begin) {

                    //First fill about 300ms

        begin = false;
        short[][] buffer = new short[channels][numFramesBegin];
                    //numFramesBegin is for example 10000
                    //For debugging only buffer[0] is useful
        fillSimulatedBuffer(buffer, framesRead);
        framesRead += numFramesBegin;
        audioPlayer.addToQueue(buffer[0]);

    }

    else {
        try {
            short[][] buffer = new short[channels][numFrames];

                            //Afterwards fill like 200ms

            fillSimulatedBuffer(buffer, framesRead);
            framesRead += numFrames;
            audioPlayer.addToQueue(buffer[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

private short simulator(int time, short amplitude) {
            //a pure A (frequency=440)
            //this is probably wrong due to sampling rate, but 44 and 4400 won't work either
    return (short)(amplitude*((short)(Math.sin((double)(simulatorFrequency*time)))));
}

private void fillSimulatedBuffer(short[][] buffer, int offset) {
         for(int i = 0; i < buffer[0].length; i++)
        buffer[0][i] = simulator(offset + i, amplitude);
}

A timeTask class that calls model.ontimechange() every 200 ms.

public class ReadMusic extends TimerTask {
private final Model model;

public ReadMusic(Model model) {
    this.model = model;
}


@Override
public void run() {
    System.out.println("Task run");
    model.onTimeChange();
}

}

What debugging showed me:

  • timeTask works fine, it does its job;
  • Buffer values seem coherent, and buffer size is bigger than minBufSize;
  • Audiotrack’s playing state is “playing”
  • no exceptions are caught in model functions.

Any ideas would be greatly appreciated!

  • 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-05-27T14:41:07+00:00Added an answer on May 27, 2026 at 2:41 pm

    OK I found the problem.

    There is an error in the current AudioTrack documentation regarding AudioTrack and short buffer input: the specified buffer size should be the size of the buffer itself (buffer.length) and not the size in bytes.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
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

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.