Assets的使用和SoundPool播放音频

关于Assets

assets目录下存放的资源代表应用无法直接访问的原生资源(R.xxx.xxx),应用程序需要通过AssetManager以二进制流形式进行读取资源。assets可以被看作随应用打包的微型文件系统,支持任意层次的文件目录结构。

  • 导入assets
    file->New->Folder->Assets Folder 然后直接点finish
    在Assets 创建子目录 sample_sounds.

处理assets

  • 创建常量用于存储声音资源文件目录名
    private static final String SOUND_FOLDER=”sample_sounds”
  • 访问assets。
    private AssetManager mAsstes;
    在构造方法中初始化
    mAsstes=context.getAssets();

查看assets资源

String[] soundsNames;
try{
//返回指定路径下的所有文件及目录名。
    soundNames=mAssets.list(SOUND_FOLDER);
}catch(IOException ioe){
}

使用assets

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是通过SoundPool播放音频文件实现弹钢琴效果的实验步骤: 1. 在Android Studio中创建一个新的项目。 2. 将需要用到的钢琴音频文件放在项目的assets目录下。 3. 在主Activity中定义一个SoundPool对象和一个HashMap对象,用于存储音频文件对应的ID值。 4. 在onCreate()方法中初始化SoundPool对象,设置最大音频流数为10,并且为每个音频文件加载对应的ID值,并存储到HashMap对象中。 5. 在布局文件中添加一个钢琴键盘的视图,用于用户触摸弹奏钢琴。 6. 在MainActivity中定义一个方法playSound(int soundId),用于播放指定ID的音频文件。 7. 在钢琴键盘视图的触摸事件监听中,获取当前触摸的位置,并根据位置播放对应的音频文件。 以下是实现的代码示例: ``` public class MainActivity extends AppCompatActivity implements View.OnTouchListener { private SoundPool soundPool; private HashMap<Integer, Integer> soundMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化SoundPool对象 soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundMap = new HashMap<>(); AssetManager assetManager = getAssets(); try { // 加载钢琴音频文件 String[] soundFiles = assetManager.list("piano"); for (String soundFile : soundFiles) { String path = "piano/" + soundFile; int soundId = soundPool.load(assetManager.openFd(path), 1); soundMap.put(Integer.parseInt(soundFile.substring(0, soundFile.indexOf("."))), soundId); } } catch (IOException e) { e.printStackTrace(); } // 设置钢琴键盘的触摸事件监听 findViewById(R.id.key1).setOnTouchListener(this); findViewById(R.id.key2).setOnTouchListener(this); findViewById(R.id.key3).setOnTouchListener(this); findViewById(R.id.key4).setOnTouchListener(this); findViewById(R.id.key5).setOnTouchListener(this); findViewById(R.id.key6).setOnTouchListener(this); findViewById(R.id.key7).setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 根据触摸的位置播放对应的音频文件 switch (v.getId()) { case R.id.key1: playSound(1); break; case R.id.key2: playSound(2); break; case R.id.key3: playSound(3); break; case R.id.key4: playSound(4); break; case R.id.key5: playSound(5); break; case R.id.key6: playSound(6); break; case R.id.key7: playSound(7); break; } break; } return true; } // 播放指定ID的音频文件 private void playSound(int soundId) { if (soundMap.containsKey(soundId)) { soundPool.play(soundMap.get(soundId), 1, 1, 0, 0, 1); } } } ``` 在上面的代码中,我们使用SoundPool类来播放钢琴音频文件,使用HashMap对象存储音频文件对应的ID值,使用触摸事件监听实现用户弹奏钢琴的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值