在java(android)中用低CPU和空间保存short[]

发布于 2024-12-17 13:59:22 字数 190 浏览 4 评论 0原文

在流式传输音乐时,我正在获取类型为short[]的pcm数据,并且我想将其保存到我的Android设备中的文件中,以便稍后可以再次播放(使用AudioTrack)。我不希望音乐的存储在内存和CPU上是高效的。

如何将短[]保存到文件,因为我在.write(short[])中看不到任何函数?

我怎样才能减少保存这个文件的空间\CPU?

while streaming music i'm getting pcm data as type short[] and i want to save it to file in my android device so i can play it again later (using AudioTrack). i wan't the store of the music to be efficent in memory and cpu.

how to save short[] to file cause i dont see any function in.write(short[])?

how can i decrease the space\cpu for saving this file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2024-12-24 13:59:22

用 DataOutputStream 包装 FileOutputStream:

DataOutputStream doStream = new DataOutputStream(new BufferedOutputStream(fileOutputStream));
doStream.writeInt(numberArray.length); //Save size
for (int i=0;i<numberArray.length;i++) {
    doStream.writeShort(numberArray[i]); //Save each number
}

以相同的方式读回它:

DataInputStream diStream = new DataInputStream(new BufferedInputStream(fileInputStream));
int size = diStream.readInt(); //Read size
short[] data = new short[size]; //Create new array with required length
for (int i=0;i<size;i++) {
    data[i] = diStream.readShort(); //Read each number
}

Wrap your FileOutputStream with DataOutputStream:

DataOutputStream doStream = new DataOutputStream(new BufferedOutputStream(fileOutputStream));
doStream.writeInt(numberArray.length); //Save size
for (int i=0;i<numberArray.length;i++) {
    doStream.writeShort(numberArray[i]); //Save each number
}

Same way for reading it back:

DataInputStream diStream = new DataInputStream(new BufferedInputStream(fileInputStream));
int size = diStream.readInt(); //Read size
short[] data = new short[size]; //Create new array with required length
for (int i=0;i<size;i++) {
    data[i] = diStream.readShort(); //Read each number
}
ゃ人海孤独症 2024-12-24 13:59:22

无需对 MP3 或类似内容进行任何编码,您始终可以这样做。

short[] sound = ...;
ByteBuffer byteMyShorts = ByteBuffer.allocate(sound.length * 2);
ShortBuffer shortBytes = byteMyShorts.asShortBuffer();
shortBytes.put(sound);
byteMyShorts.flip();

// byteMyShorts.array() now contains your short[] array as an
// array of bytes.

Without any encoding to MP3 or similar you can always do like this.

short[] sound = ...;
ByteBuffer byteMyShorts = ByteBuffer.allocate(sound.length * 2);
ShortBuffer shortBytes = byteMyShorts.asShortBuffer();
shortBytes.put(sound);
byteMyShorts.flip();

// byteMyShorts.array() now contains your short[] array as an
// array of bytes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文