2017-02-13 164 views
0

我只是想在Android Studio中點擊按鈕時播放mp3文件。播放聲音OnClick Android

我的問題是,我用2種方法播放聲音,但由於某種原因,我聽到它像一個非常扭曲的超級慢動作聲音。而如果我通常打開文件就沒問題。

!!!!!方法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(); 
    } 
}); 

我managerOfSound馬託:

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

回答

-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/

+0

我跟着那個和描述它仍然表現 – johnnyshrewd

0

這可能是因爲MaxStreams最大數量(併發流的最大數)的Soundpool實例太低

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

我只是想流1,我試過這個,不幸的是它沒有工作 – johnnyshrewd