播放声音OnClick Android

时间:2017-02-13 17:01:55

标签: android android-studio audio onclick playsound

当我点击android studio中的按钮时,我只想播放一个mp3文件。

我的问题是,使用我使用的两种方法播放声音,但出于某种原因,我听到它像一个极度扭曲的超慢动作声音。如果我通常打开文件就可以了。

!!!!!方法1:

我宣布:

SoundPool mySound;
int soungId;

我初始化:

mySound  = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
soungId = mySound.load(this, R.raw.asd,1);

然后使用动作监听器:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mySound.play(soungId,1,1,1,0,1);
    }
});

!!!!!方法2:

我宣布:

MediaPlayer mp = null;

我使用听众:

button.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view) {
        managerOfSound();
    }
});

我的经理SSound mathod:

public void managerOfSound() {
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.www);
mp.reset();
mp.start();
}

2 个答案:

答案 0 :(得分:0)

可能是因为MaxStreams最大数量(最大并发流数)的Soundpool实例太低

SoundPool mySound;
mySound  = new SoundPool(10, AudioManager.STREAM_MUSIC,0);

答案 1 :(得分:-2)

试试这个,

MediaPlayer mp = new MediaPlayer();

// Set data source -
setDataSource("/sdcard/path_to_song");

// Play audio
mp.start();

// Pause audio
mp.pause();

// Reset mediaplayer
mp.reset();

// Get song length duration - in milliseconds
mp.getDuration();

// Get current duration - in milliseconds
mp.getCurrentDuration();

// Move song to particular second - used for Forward or Backward
mp.seekTo(positon); // position in milliseconds

// Check if song is playing or not
mp.isPlaying(); // returns true or false

http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/