android - 振动效果很好,但声音在 android 上没有
问题描述
我想同时应用振动和声音。振动工作正常,但声音不起作用。我得到的声音是 mp3。
我将如何解决这个问题?
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Get Ready Bus is Near",Toast.LENGTH_LONG).show();
// Initializing instance of Vibrator.
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
// Initializing instance of MediaPlayer.
MediaPlayer mediaPlayer = new MediaPlayer();
// Starting Vibration
v.vibrate(2000);
try {
// Setting the source of audio file.
//String path = "android.resource://"+"com.example.myproject"+"/raw/"+audioFile;
mediaPlayer.setDataSource(context,Uri.parse("android.resource://"+"com.example.fahad.finalyearprojectlayout"+"/raw/"+"sound.mp3")); // Fill the information accordingly.
mediaPlayer.prepare();
// playing audio.
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
解决方案
这是一种不同的、更短的方法来创建mediaPlayer
:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
所以现在,这就是你的代码应该是什么样子。您不需要try
orcatch
块:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
v.vibrate(2000);
mediaPlayer
在开始振动之前尝试启动。如果这不起作用,请确保您已在设备中打开声音。如果仍有问题,请在下面发布新问题。但是,如果您为您的应用程序启用了声音和振动,我不明白为什么它不应该工作。
推荐阅读
- asp.net-core - 我们可以在没有 Kestrel 的情况下在 linux 上托管 ASP.NET Core 应用程序吗?
- c# - 使用 c# 捕获文件异常
- autodesk-forge - 有没有办法知道哪个revit链接文件属于forge查看器中的元素?
- c++ - 如何停止 OpenMP 2.0 的并行区域
- eslint - Rollup 和 eslint:如何使用带有 rollup 的 eslint 修复此错误“TypeError:eslint 不是函数”?
- python - 导出到 csv 并读取 multiIndex 数据框 pandas
- c# - Asp.Net MVC 应用程序的问题 - 无法加载“System.Diagnotics.Tracing”
- c# - 无法使用我的身份服务器从本机应用程序调用 API
- python - Python正则表达式匹配字符串的特定位置
- c++ - 将字符添加到 std::cin