在 android 中播放 mp3 文件时遇到问题
我在播放音频(mp3)文件时遇到问题,该音乐文件就像驻留在原始文件夹中的点击声音,问题是如果随机间隔有多次点击,则会引发 nullPointer 异常。单击完成后的任何地方和任何时间都会发生这种情况,是否与内存问题或 MediaPlayer 相关问题有关,请提出任何建议。 我正在调用它的简单媒体播放器对象,但它是一个游戏,所以在触摸时它会播放文件,所以在游戏中我有很多东西要拖动,所以我当时想要一个点击声音,有时它工作正常,但当超过一定限制时它抛出空指针异常。这是代码:
MediaPlayer mp= MediaPlayer.create(context,R.raw.soun1);
mp.start();
就是这样:
I am getting problems in playing audio(mp3) files this music files are like click sounds its residing at the raw folder, the problem is if there are many clicks at random intervals it throws an exception of nullPointer. It occurs anywhere when the click is done and anytime, is it related to the memory issue or MediaPlayer related problem, pls any suggestion will be appreceated.
Its simple media player object that i m calling, but its a games so on touch it plays the files, so in game i have many things to drag so i want a click sound at that time, sometime it works fine but when exceeds certain limit it throws null pointer exceptions. this is the code:
MediaPlayer mp= MediaPlayer.create(context,R.raw.soun1);
mp.start();
thats it:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个::
清单文件中的权限:::
just try this ::
permission in manifest file:::
要播放媒体播放器...我们需要两个类..
让我们假设 mainactivity.java 是我们的第一个文件。
这里我们定义了两个按钮 -
start_button
&stop_button
maservice.java 是我们的另一个 java 文件。这里我们定义了媒体播放器,并且应该有3个方法:onCreate()、onStart()、onDestroy()。
代码如下:
我们还必须在清单文件中定义这些java文件
To play media player...we need two classes..
let us suppose
mainactivity.java
is our first file..here we define two buttons -
start_button
&stop_button
maservice.java is our another java file. Here we define media player and also there should be 3 methods: onCreate(), onStart(), onDestroy().
Here is the code:
We also have to define these java files in manifest file
我得到了答案,它是 SoundPool,特别是在关注游戏时创建的像连续使用声音文件的应用程序,所以这里我们应该使用除 MediaPlayer 之外的 SoundPool。
I got my answer, its SoundPool, especially created when the concern of game like application where the sound files are used continuously, so here we should use SoundPool except of MediaPlayer.
问题出在 MP3 编码上。我尝试使用相同的代码,很少工作,很少不工作。因此,如果下次出现相同的错误,请尝试使用其他的。
The issue is with the MP3 encoding. I tried with the same code, few work and few don't. So please try with a different one if it shows up the same error next time.