2010-05-02 108 views
5

我試圖播放一個聲音文件時,單擊按鈕,但不斷收到錯誤。如何在Android中單擊按鈕時播放聲音?

的錯誤是:

"The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new View.OnClickListener(){}, int)" 

這裏是我的代碼:

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Button zero = (Button)this.findViewById(R.id.btnZero); 
    zero.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      mp = MediaPlayer.create(this, R.raw.mamacita_zero); 
     } 
    }); 
} 

任何幫助或建議,將不勝感激。 Thnx!

回答

23

有幾件事情怎麼回事(免責聲明,這只是我如何使用使用它,有可能是一個更好的辦法):

  • 你似乎做更多每次點擊工作比你需要。您正在爲活動的視圖中的每個點擊創建並添加新的onClickListener,而不是按鈕。你只需要設置監聽器一次,而不是總體視圖;我傾向於在Activity的構造函數中這樣做。

  • 關於您的錯誤,當我通過它的上下文是覆蓋活動時,MediaPlayer可以正常工作。當你通過this時,它傳遞你正在創建的onClickListener,拋棄MediaPlayer。

  • 最後,要實際播放聲音,您必須致電start()

所以對於活動中的構造函數,你可以創建MediaPlayer一次,找到按鈕,並附加onClickListener,將播放從MediaPlayer的您剛剛創建的聲音。它看起來像這樣:

public class MyActivity extends Activity { 

    public MyActivity(Bundle onSavedStateInstance) { 
     // eliding some bookkeepping 

     MediaPlayer mp = MediaPlayer.create(this, R.raw.mamacita_zero); 

     Button zero = (Button)this.findViewById(R.id.btnZero); 
     zero.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mp.start(); 
      } 
     }); 
    } 
} 

希望有所幫助!

+0

感謝您開始工作的想法.. – Noman 2012-12-17 07:41:51

3
MediaPlayer mp = MediaPlayer.create(getBaseContext(), 
R.raw.yoursoundfile); 
mp.start(); 

yoursoundfile必須將該文件是在res /原始文件夾

13

我與媒體播放器播放左右,而它很容易陷入困境。我遵循了Volodymyr的建議,並且SoundPool更容易管理。

MediaPlayer不喜歡在當時播放多個聲音,例如當您的按鈕上有許多快速選項卡時。

private void playSound(Uri uri) { 
    try { 
     mMediaPlayer.reset(); 
     mMediaPlayer.setDataSource(this, uri); 
     mMediaPlayer.prepare(); 
     mMediaPlayer.start(); 
    } catch (Exception e) { 
     // don't care 
    } 

}

在構造函數中我做:我用下面的方法管理

mMediaPlayer = new MediaPlayer(); 
mSoundLess = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.less); 
mSoundMore = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.more); 

在點擊我會再打電話playSound(mSoundLess):

相反,我創建了一個的Soundpool幫手:

package com.mycompany.myapp.util; 

import java.util.HashSet; 
import java.util.Set; 

import android.content.Context; 
import android.media.AudioManager; 
import android.media.SoundPool; 

public class SoundPoolHelper extends SoundPool { 
    private Set<Integer> mLoaded; 
    private Context mContext; 

    public SoundPoolHelper(int maxStreams, Context context) { 
     this(maxStreams, AudioManager.STREAM_MUSIC, 0, context); 
    } 

    public SoundPoolHelper(int maxStreams, int streamType, int srcQuality, Context context) { 
     super(maxStreams, streamType, srcQuality); 
     mContext = context; 
     mLoaded = new HashSet<Integer>(); 
     setOnLoadCompleteListener(new OnLoadCompleteListener() { 
      @Override 
      public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
       mLoaded.add(sampleId); 
      } 
     }); 
    } 

    public void play(int soundID) { 
     AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 
     float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     float volume = actualVolume/maxVolume; 
     // Is the sound loaded already? 
     if (mLoaded.contains(soundID)) { 
      play(soundID, volume, volume, 1, 0, 1f); 
     } 
    } 
} 

現在我初始化這樣的:

mSoundPoolHelper = new SoundPoolHelper(1, this); 
mSoundLessId = mSoundPoolHelper.load(this, R.raw.less, 1); 
mSoundMoreId = mSoundPoolHelper.load(this, R.raw.more, 1); 

和播放聲音是這樣的:

private void playSound(int soundId) { 
    mSoundPoolHelper.play(soundId); 
} 

不要忘了在onDestroy()打電話mSoundPoolHelper.release();,例如。如果您使用MediaPlayer,則需要類似的東西。

0

如果你真的要編程方式調用點擊,因爲該視圖沒有自己的聲音,我會解決它這樣,其最簡單的解決方案和oneliner

view.playSoundEffect(SoundEffectConstants.CLICK); 

非常簡單的作品,如果你想使佈局發揮你需要把

android:soundEffectsEnabled="true" 

在您的XML。

相關問題