3

在我的应用程序中,我想录制一个 2 秒长的声音并将其回放给用户。到目前为止,我发现的所有示例都要求用于播放的音频数据要么来自文件,要么来自 URL。

MediaPlayer 和 SoundPool 类都只接受文件、文件描述符、资源 ID 或 URL 作为输入,而不仅仅是字节数组。

由于只能将文件写入 SD 卡而不是内部存储(是这样吗?),该应用程序需要安装 SD 卡并且可写入。但是如果没有 SD 卡,我的应用程序也应该可以工作。

有没有办法做到这一点?谢谢

4

2 回答 2

3

是的,应用程序可以写入内部存储(安装位置)。每个已安装的应用程序都提供了一个 Data 文件夹来写入其内部文件。还提供了一个缓存存储。

这些方法Context可以为您获取这些目录:

  • getCacheDir():返回文件系统上应用程序特定缓存目录的绝对路径(注意:如果存储空间不足,系统可能会删除缓存

  • getDir(String name, int mode): 检索,如果需要,创建一个新目录,应用程序可以在其中放置自己的自定义数据文件。

还有一种外部存储方法:getExternalCacheDir()但不可靠,因为外部存储可能并不总是存在。

另外,如果只需要在App的内部数据目录中写入文件,有一个非常简单的方法:

openFileOutput (String name, int mode)

于 2012-12-07T17:01:04.150 回答
0

You can use 'getApplicationContext().getFilesDir().getAbsolutePath()' to get the path of the directory where the app is installed. One limitation is, 'getFilesDir()' gives you the path to the directory on the file system where files created with openFileOutput(String, int) are stored.

于 2012-12-10T13:01:18.593 回答