经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android利用SoundPool实现音乐池
来源:jb51  时间:2021/11/15 17:22:02  对本文有异议

本文实例为大家分享了Android利用SoundPool实现音乐池的具体代码,供大家参考,具体内容如下

运行效果图如下:

布局文件(activity_sound_pool.xml)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_sound_pool"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. android:orientation="vertical"
  12. tools:context="com.example.g150825_android26.SoundPoolActivity">
  13.  
  14. <Button
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:text="音效鸡"
  18. android:onClick="playKFC"
  19. />
  20. <Button
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:text="音效TWO"
  24. android:onClick="playTWO"
  25. />
  26. <Button
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="音效Three"
  30. android:onClick="playThree"
  31. />
  32. <Button
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:text="音效Four"
  36. android:onClick="playFour"
  37. />
  38. <Button
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:text="音效狗"
  42. android:onClick="playDog"
  43. />
  44.  
  45. </LinearLayout>

Java代码

  1. package com.example.g150825_android26;
  2.  
  3. import android.app.AlarmManager;
  4. import android.media.AudioManager;
  5. import android.media.SoundPool;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9.  
  10. public class SoundPoolActivity extends AppCompatActivity {
  11.  
  12. private SoundPool soundPool;
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_sound_pool);
  18. soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,0);
  19.  
  20. soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
  21. @Override
  22. public void onLoadComplete(SoundPool soundPool, int i, int i1) {
  23. soundPool.play(i,1,1,1,-1,1);
  24. }
  25. });
  26. }
  27. public void playKFC(View view){
  28. soundPool.load(this,R.raw.rooster,1);
  29. }
  30. public void playTWO(View view){
  31. soundPool.load(this,R.raw.chimp,1);
  32.  
  33. }
  34. public void playThree(View view){
  35. soundPool.load(this,R.raw.crickets,1);
  36. }
  37. public void playFour(View view){
  38. soundPool.load(this,R.raw.roar,1);
  39. }
  40. public void playDog(View view){
  41. soundPool.load(this,R.raw.dogbark,1);
  42. }
  43.  
  44. @Override
  45. protected void onDestroy() {
  46. super.onDestroy();
  47. if (soundPool!=null){
  48. soundPool.release();
  49. soundPool=null;
  50. }
  51. }
  52. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号