PDA

View Full Version : ساخت دکمه صدا دار



darkhah1
یک شنبه 05 بهمن 1393, 11:00 صبح
سلام من دارم یه ساز می سازم و می خواستم چند دکمه بسازم که هر کدوم یه صدایی بده کسی میتونه راهنماییم کنه؟؟؟

مصطفی شکوری
یک شنبه 05 بهمن 1393, 12:32 عصر
از این کد استفاده کن:

finalMediaPlayer mp =newMediaPlayer();Button b =(Button) findViewById(R.id.button1);

b.setOnClickListener(newOnClickListener(){

@Override
publicvoid onClick(View v){

if(mp.isPlaying())
{
mp.stop();
mp.reset();
}
try{

AssetFileDescriptor afd;
afd = getAssets().openFd("AudioFile.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getSt artOffset(),afd.getLength());
mp.prepare();
mp.start();
}catch(IllegalStateException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}



}});

صدا ی مورد نظر(در این مثالAudioFile.mp3) رو مستقیما در پوشه ی Assets قرار بده.:لبخندساده:

darkhah1
یک شنبه 05 بهمن 1393, 13:20 عصر
این چجوری دیگه؟؟؟؟؟

big lost
یک شنبه 05 بهمن 1393, 15:02 عصر
این بهترین کدیه که بدردت میخوره :

NoteManager:

import android.app.Activity;import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.View;
import android.widget.Button;


public class NoteManager {


public NoteManager() {
//Nothing Needed, as of now.
}


public void defineKeyNote(final Activity A, int noteId,final int soundId) {
Button cKey = (Button) A.findViewById(noteId);
cKey.setSoundEffectsEnabled(false);
cKey.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View V)
{
playNote(A, V, soundId);
}
});
}

/** Play the file defined by resid **/
public void playNote(final Activity A, View view, int resid) {
// Play a Note
MediaPlayer clip = MediaPlayer.create(A.getApplicationContext(), resid);
clip.start();

OnCompletionListener listener = new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
};
clip.setOnCompletionListener(listener);
}

}


MainActivity:





import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
private AudioManager audioManager;
private NoteManager noteManager = new NoteManager();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createClickEvents();
}



private void createClickEvents() {

//Fix this up to go by ID.
noteManager.defineKeyNote(this, R.id.button1, R.raw.a1);
noteManager.defineKeyNote(this, R.id.button2, R.raw.a2);
noteManager.defineKeyNote(this, R.id.button3, R.raw.a3);
noteManager.defineKeyNote(this, R.id.button4, R.raw.a4);
noteManager.defineKeyNote(this, R.id.button5, R.raw.a5);
noteManager.defineKeyNote(this, R.id.button6, R.raw.a6);
noteManager.defineKeyNote(this, R.id.button7, R.raw.a7);
noteManager.defineKeyNote(this, R.id.button8, R.raw.a8);
noteManager.defineKeyNote(this, R.id.button9, R.raw.a9);
noteManager.defineKeyNote(this, R.id.button10, R.raw.a10);
noteManager.defineKeyNote(this, R.id.button11, R.raw.a11);
noteManager.defineKeyNote(this, R.id.button12, R.raw.a12);
noteManager.defineKeyNote(this, R.id.button13, R.raw.a13);
noteManager.defineKeyNote(this, R.id.button14, R.raw.b1);
noteManager.defineKeyNote(this, R.id.button15, R.raw.b2);
noteManager.defineKeyNote(this, R.id.button16, R.raw.b3);
noteManager.defineKeyNote(this, R.id.button17, R.raw.b4);
noteManager.defineKeyNote(this, R.id.button18, R.raw.b5);
noteManager.defineKeyNote(this, R.id.button19, R.raw.b6);
noteManager.defineKeyNote(this, R.id.button20, R.raw.b7);
noteManager.defineKeyNote(this, R.id.button21, R.raw.b8);
noteManager.defineKeyNote(this, R.id.button22, R.raw.b9);
noteManager.defineKeyNote(this, R.id.button23, R.raw.b10);
noteManager.defineKeyNote(this, R.id.button24, R.raw.b11);
noteManager.defineKeyNote(this, R.id.button25, R.raw.b12);
noteManager.defineKeyNote(this, R.id.button26, R.raw.b13);
noteManager.defineKeyNote(this, R.id.button27, R.raw.b14);
noteManager.defineKeyNote(this, R.id.button28, R.raw.c1);
noteManager.defineKeyNote(this, R.id.button29, R.raw.c2);
noteManager.defineKeyNote(this, R.id.button30, R.raw.c3);
noteManager.defineKeyNote(this, R.id.button31, R.raw.c4);
noteManager.defineKeyNote(this, R.id.button32, R.raw.c5);
noteManager.defineKeyNote(this, R.id.button33, R.raw.c6);
noteManager.defineKeyNote(this, R.id.button34, R.raw.c7);
noteManager.defineKeyNote(this, R.id.button35, R.raw.c8);
noteManager.defineKeyNote(this, R.id.button36, R.raw.c9);
noteManager.defineKeyNote(this, R.id.button37, R.raw.c10);
noteManager.defineKeyNote(this, R.id.button38, R.raw.c11);
noteManager.defineKeyNote(this, R.id.button39, R.raw.c12);
noteManager.defineKeyNote(this, R.id.button40, R.raw.c13);



}

}

------------------------------------------------------------------------------------------------------------------

Americanboy
دوشنبه 06 بهمن 1393, 20:47 عصر
ببخشید دوستان من یه مشکلی برام پیش اومده
برای هر دکمه یک صدا تعیین کردم و در رویداد کلیک اون مدیا پلیرم رو استارت کردم و ولوم هم دادم،حالا برنامم رو اجرا کردم اما وقتی روی دکمه کلیک میکنم صدا اجرا میشه اما صدای رویداد خود دستگاه موبایل هم ایجاد میشه به نظرتون چطوری میتونم صدای خود دستگاه موبایل رو از کار بندازم؟