1、方法一

public void onCreate(Bundle savedInstanceState) {     
super.onCreate(savedInstanceState); setContentView(R.layout.main); MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile); mPlayer.start(); }public void onDestroy() { mPlayer.stop(); super.onDestroy();}

2、方法二:

A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.

/** * How many sounds can be played at once. */private static final int MAX_SOUND_POOL_STREAMS = 4;/** * Modify this as part of your own priority scheme. Higher numbers mean higher * priority. If you don't care, it's okay to use the same priority for every * sound. */private static final int NORMAL_PRIORITY = 10;private int mySoundId;@Overridepublic void setupContent() {    this.soundPool = new SoundPool(MAX_SOUND_POOL_STREAMS,            AudioManager.STREAM_MUSIC, 100);    this.mySoundId = this.soundPool.load(this.getApplicationContext(),            R.raw.mySound, 1);}@Overrideprivate void playMySound() {    this.soundPool.play(this.mySoundId, 1, 1, NORMAL_PRIORITY, 0, 1);}

更多相关文章

  1. Android在onCreate()中获得控件尺寸 (转)
  2. Android中全屏无标题设置
  3. Android之TabHost的几种使用方法
  4. Android(安卓)获得当前进程PackageName
  5. 防止Android点击按钮过快造成多次事件实现原理
  6. Android保持屏幕常亮的方法总结
  7. android 查找联系人方法(支持首拼,全拼,英文)
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. javascript:引入方式/变量与常量声明/函数
  2. 记录一次生产上暴力解决HBase RIT问题
  3. linux scp命令参数及用法详解--linux远程
  4. MongoDB模糊查询时带有括号的情况
  5. MySql基础查询-分组函数
  6. 即将发布的 Apache Spark 2.4 都有哪些新
  7. MacBook pro的touch bar怎么显示歌词?
  8. [Phoenix系列]Phoenix入门到精通续集
  9. Python编程必备5大工具,你用过几个?
  10. HBase应用实践专场-HBase问题排查思路