- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 Android 开发的新手,想知道如何在广播接收器中播放 SoundPool 声音?
我在某处读到,使用 SoundPool 是播放声音的方式,但我不知道如何正确设置它。
我的 Eclipse res\raw 文件夹中有一些声音文件,例如 wave 和 mp3 文件。我想播放一个名为 half.wav 的文件
你能展示我需要放入我的广播接收器中的示例代码吗?
这是对代码的第一次尝试,但我确实收到一条错误消息,指出 soundID = soundPool.load(this, R.raw.half, 1);
“类型 SoundPool 中的方法 Load(Context, Int, Int) 不适用...”
这是类的代码:
package ChimeMe.BigBen;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import android.os.Bundle;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
public class AlarmReceiver extends BroadcastReceiver {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
@Override
public void onReceive(Context context, Intent intent) {
try {
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.half, 1);
Toast.makeText(context, "This is the alarm.", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
提前致谢。
真的,艾玛德
最佳答案
如果您只想播放一种声音,我认为使用 MediaPlayer 会更快更容易...
这是我的一个应用程序的代码,当这个 Broadcastreceiver 运行时,它每 30 分钟发出一次哔声
public class Gameloop extends BroadcastReceiver {
MediaPlayer mp = null;// Here
private static final String TAG = "VPET";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Loop running");
if (Pet.isAlive == true) {
mp = MediaPlayer.create(context, R.raw.beep);//Onreceive gives you context
mp.start();// and this to play it
} else {
}
}
}
}
关于Android 编程 - 如何从广播接收器播放 SoundPool 声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7164760/
我正在创建一个游戏,它有一系列声音可以在不同的 Activity 中播放。 在 onCreate 中加载声音会极大地阻碍 FPS,我想知道是否有更有效的加载声音的方法,或者创建一个可以由 Activi
我的应用程序中有一个调用异步任务的按钮。 此异步任务应在每个ProgressUpdate上播放音频文件。 到目前为止,我已经弄清楚了很多,但是我坚持这一点: 我打电话 this.str
我在使用 SoundPool 和 .OGG 文件播放循环声音时遇到问题。我设置了这个 HashMap 来查找与名称关联的声音并播放/停止它 public void playLoopSound(
当我尝试在应用程序中启动声音时,它无法播放。我已经看到该方法确实被调用,但是 soundPool 只是不想播放声音。该应用程序按预期启动,但是,它只是不想播放排队的声音。难道是我做错了什么?我尝试播放
我对java有点陌生,所以如果我犯了一个非常愚蠢的错误,请耐心等待。我正在尝试使用 SoundPool 在计数器应用程序上单击按钮时播放声音,但是单击任一按钮时应用程序崩溃。编译过程中没有出现错误。这
我正在使用SoundPool为了在我的应用程序中使用计时器播放一些声音。有谁知道是否有一个内置队列,它将对文件进行排队并仅在前面的文件完成后才播放它们? 我必须编写自己的 SoundPool 队列实现
我正在尝试播放原始文件夹中的 .mp3 声音,但不知何故无法播放。该代码确实执行了 play 方法,但它不产生声音。这是我的代码: public SoundPlayer(Context context
我有 8 种不同的 Soundpool 噪音,都有自己的搜索栏来控制音量。我有日志显示搜索栏中的数字正在按应有的方式变化,但音量在 90% 的时间里都没有变化(奇怪的是它偶尔会起作用)。 这是设置音量
我有一个 soundpool 对象和几个声音,但是一旦创建我就无法改变声音播放,例如循环次数、音量、停止等。 声明代码: public SoundPool sounds; public HashMap
第一次按下按钮停止声音。但是在那之后按下它没有任何效果。这是我的 Java 代码: public class App extends MultiTouchActivity { SoundPool sp
我正在尝试使用 SoundPool 在我的 Activity 中播放声音,但我听不到声音。 private void beginGame(){ setContentView(R.lay
我有一个按钮,当按下它时,SoundPool 会发出短促的声音效果。该文件是 16k mp3。 声音效果适用于数百次按钮点击,但在某些时候我收到此错误(并且不再为点击播放声音): E/AudioFli
我在onCreate() 方法中写了这段代码,但是没有播放! SoundPool mSound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
我有 2 个 SoundPool 实例,如果我运行我的应用程序,soundPool2 运行与 soundPool1 相同的声音,我不知道为什么这播放相同的声音。谁能帮我找出问题所在? public c
我已经阅读了其他帖子,但我仍然不明白如何停止正在播放的声音,我希望当我按下一个按钮时播放声音,当我按下另一个按钮时我希望声音播放别玩了,如果有人能帮助我那就太好了。 最佳答案 您需要创建一个 Stre
我正在制作一款有很多声音的游戏,我使用 Soundpool 来播放它们。在几乎每一个声音中,我都创建了一个新线程,不会在主 UI 线程中加载太多。像这样: public void playSatell
所以我有 soundpool OnLoadCompleteListener,它有这个代码 mStreamId = soundPool.play(sampleId, 1, 1, 1, 0, 1f);我有
Android 的 soundpool.play [文档][1] 说“播放速率允许应用程序改变声音的播放速率(音调)。值 1.0 表示播放以原始频率播放。值为 2.0 表示播放速度加倍,值为 0.5
我正在制作一款应用,您可以在其中选择 1 到 4 种不同的声音并同时播放它们。一切正常,直到我想停止声音,功能停止同时仅适用于一种声音,这是代码(仅适用于 soundpool) sound = new
我是 Android 的新手,所以要温柔点! 我有以下代码,(我认为)应该只是播放“developersshort”。 public constructor(Context context){
我是一名优秀的程序员,十分优秀!