尝试播放声音时出错

时间:2013-08-11 21:57:36

标签: android

我尝试构建一个按钮,点击播放声音。

以下是我的代码。当我点击按钮时,我得到错误(-19,0)。

这是什么意思?

public void onClick(View v) {

    MediaPlayer click = MediaPlayer.create(Timer.this, R.raw.click);
        click.start();

        }
    });

3 个答案:

答案 0 :(得分:1)

使用MediaPlayer播放声音并不是一个好主意。你应该考虑使用SoundPool 看看SoundPool's documentation

答案 1 :(得分:1)

最好将Soundpool用于像点击一样的小声音。看这个视频: http://thenewboston.org/watch.php?cat=6&number=79

答案 2 :(得分:1)

做得好......最好在资源文件夹中创建一个名为raw的新文件夹,然后在那里复制声音片段。然后在onClick下添加以下内容,如果你想点击一个按钮播放声音:

public void onClick(View v) {
ourSong = MediaPlayer.create(Incoming.this, R.raw.abcd); 
ourSong.start(); //where abcd is ur sound file and Incoming is ur java class 

你需要定义MediaPlayer ourSong;像这样:

public class Incoming extends Activity{
MediaPlayer ourSong;