AAudioStream is an Android C API that provides a way to play and record audio with low latency. It does not provide direct support for streaming from or to a file, but you can read data from a file and write it to an AAudioStream for playback, or read data from an AAudioStream and write it to a file for recording.
Here's a basic outline on how you could use AAudioStream to play audio data from a file:
-
Open the audio file and read its contents. You will have to handle this according to the audio format you're dealing with (e.g., WAV, OGG, MP3). For uncompressed formats like WAV, you could read the raw PCM data directly; for compressed formats, you will need to use a decoder to convert the file data to PCM data first.
-
Configure and create an AAudioStream for playback.
-
In a loop, read chunks of audio data from the file into a temporary buffer, then write that buffer to the AAudioStream.
Example pseudo-code to create an AAudioStream for playback:
#include <aaudio/AAudio.h>
#include <iostream>
#include <fstream>
void playAudioFromFile(const std::string& filepath) {
std::ifstream file(filepath, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Could not open file!" << std::endl;
return;
}
AAudioStreamBuilder* builder = nullptr;
AAudioStream* stream = nullptr;
// Customize these options according to your audio file's specifications
aaudio_format_t format = AAUDIO_FORMAT_PCM_I16;
int32_t sampleRate = 44100;
int32_t channelCount = 2;
aaudio_result_t result = AAudio_createStreamBuilder(&builder);
if (result != AAUDIO_OK) {
std::cerr << "Failed to create stream builder!" << std::endl;
return;
}
AAudioStreamBuilder_setFormat(builder, format);
AAudioStreamBuilder_setSampleRate(builder, sampleRate);
AAudioStreamBuilder_setChannelCount(builder, channelCount);
result = AAudioStreamBuilder_openStream(builder, &stream);
if (result != AAUDIO_OK) {
std::cerr << "Failed to open stream!" << std::endl;
AAudioStreamBuilder_delete(builder);
return;
}
AAudioStream_requestStart(stream);
// Replace with the appropriate buffer size based on your needs
constexpr int bufferSizeInFrames = 1024;
int16_t buffer[bufferSizeInFrames * channelCount];
// Reading and playing loop
while (!file.eof()) {
file.read(reinterpret_cast<char*>(buffer), sizeof(buffer));
// Check how many frames were read to handle the case where less than
// bufferSizeInFrames of audio is left in the file
auto framesRead = file.gcount() / (sizeof(int16_t) * channelCount);
if (framesRead > 0) {
// This example assumes blocking write but you can use non-blocking approach
AAudioStream_write(stream, buffer, framesRead, AAUDIO_TIMEOUT_INFINITE);
}
}
AAudioStream_requestStop(stream);
AAudioStream_close(stream);
AAudioStreamBuilder_delete(builder);
}
This is a rudimentary example and lacks proper error checking, EOF handling, and so on, but it illustrates the overall approach. Note that actual implementations often involve more complex handling to ensure seamless and glitch-free playback. You would also need to link against the libraries providing the AAudio API (libaaudio.so) and ensure that your application has the necessary permissions to access the file system if the audio file resides outside of your application's private storage.
For decoding compressed audio formats, you might use libraries like libVorbis, libopus, or the Android Media API to decode files to PCM data that can then be played using AAudioStream.