Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am making dictionary app having sound sample, when an item in the list is click, a new activity will open containing details view and a button. Can someone help me how can I assign the specific sound to the button. The sound files are placed in raw folder. for example, I click the item 'araw',the details will show and the button must play the sound araw...Pls help me...

heres the codes:

ListView lv;
SearchView sv;


String[] tagalog= new String[] {"alaala (png.)","araw (png.)","baliw (png.)","basura (png.)",
        "kaibigan (png.)","kakatuwa (pu.)", "kasunduan (png.)","dambuhala (png.)",
        "dulo (png.)","gawin (pd.)","guni-guni (png.)","hagdan (png.)","hintay (pd.)",
        "idlip (png.)","maganda (pu.)","masarap (pu.)", "matalino (pu.)"};



ArrayAdapter<String> adapter;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    lv = (ListView) findViewById(R.id.listView1);
    sv = (SearchView) findViewById(R.id.searchView1);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tagalog);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tagword =tagalog[position];


            String[] definition = getResources().getStringArray(R.array.definition);
            final String definitionlabel = definition[position];

            String[] cuyuno = getResources().getStringArray(R.array.cuyuno);
            final String cuyunodefinition = cuyuno[position];

            String[] english = getResources().getStringArray(R.array.english);
            final String englishdefinition = english[position];


            Intent intent = new Intent(getApplicationContext(), DefinitionActivity.class);
            intent.putExtra("tagword", tagword);
            intent.putExtra("definitionlabel", definitionlabel);
            intent.putExtra("cuyunodefinition",cuyunodefinition);
            intent.putExtra("englishdefinition", englishdefinition);




            startActivity(intent);

        }
    });


    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {

            adapter.getFilter().filter(text);
            return false;
        }



    });

}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

}

DefinitionActivity.java

public class DefinitionActivity extends AppCompatActivity {

String tagalogword;
String worddefinition;
String cuyunoword;
String englishword;

int[] sounds = new int[]{R.raw.alaala,
        R.raw.araw,
        R.raw.baliw,
        R.raw.basura,
        R.raw.kaibigan,
        R.raw.kakatuwa,
        R.raw.kasunduan,
};



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


    TextView wordtv = (TextView) findViewById(R.id.wordtv);
    TextView definitiontv = (TextView) findViewById(R.id.definitiontv);
    TextView cuyunotv = (TextView) findViewById(R.id.cuyunotv);
    TextView englishtv = (TextView) findViewById(R.id.englishtv);
    Button play = (Button) findViewById(R.id.playbty);



    final Bundle extras = getIntent().getExtras();

    if (extras != null) {

        tagalogword = extras.getString("tagword");
        wordtv.setText(tagalogword);

        worddefinition = extras.getString("definitionlabel");
        definitiontv.setText(worddefinition);

        cuyunoword = extras.getString("cuyunodefinition");
        cuyunotv.setText(cuyunoword);

        englishword = extras.getString("englishdefinition");
        englishtv.setText(englishword);


    }

    play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // I do not know how to play the audio when button click

        }
    });

}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

Like Pedif said, you should use SoundPool for playing short sounds.

Here is Code from my Tetris like game, the Activity creates the Instance and get a callback when sounds were loaded.

Example as Singleton:

public class Sounds {

    private Context ctx;
    private static Sounds mSounds = null;

    private static SoundPool mSPool = null;
    private int sound_gameover;
    private int sound_teil_drehen;
    private int sound_1_zeile;
    private int sound_2_zeile;
    private int sound_3_zeile;
    private int sound_4_zeile;

    private int soundsToLoad = 6;
     /**
     *   Volumecontrol
     */
    private AudioManager audioManager;
    private float actualVolume;
    private float maxVolume;
    private float volume;

    private IOnSoundReady callback = null;

    public static Sounds getInstance(Context ctx, IOnSoundReady mCallback){
        if(mSPool == null){
            mSounds =  new Sounds(ctx, mCallback);
        }
        return mSounds;
    }

    private Sounds(Context ctx, IOnSoundReady mIOSoundReady) {
        this.ctx = ctx;
        this.callback = mIOSoundReady;
        initVolume();

        AsyncTask<Void, Void, Void> mTask = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                initSoundPool();
                return null;
            }
        };
        mTask.execute();
    }

    public void unprepare() { 
        if (mSPool == null) return;
        mSPool.release();
        mSPool = null;
    } 

    private void initSoundPool(){

        mSPool = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);

        mSPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                Log.w(TAG, "loaded soundid: " + sampleId);
                if(--soundsToLoad == 0 && callback != null){
                    callback.onSoundReady();
                }
            }
        });

        sound_gameover = mSPool.load(ctx, R.raw.game_over, 1);
        sound_teil_drehen = mSPool.load(ctx, R.raw.rotate, 1);
        sound_1_zeile = mSPool.load(ctx, R.raw.line_1,1);
        sound_2_zeile = mSPool.load(ctx, R.raw.line_2, 1);
        sound_3_zeile = mSPool.load(ctx, R.raw.line_3, 1);
        sound_4_zeile = mSPool.load(ctx, R.raw.line_4, 1);

    }

    /**
     * calculate volume
     */
    private void initVolume(){
        audioManager = (AudioManager) ctx.getSystemService(SpielActivity.AUDIO_SERVICE);
        actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        volume = actualVolume / maxVolume;
    }            

    /**
     * plays a sound
     * @param soundid 
     */
    public void playSound(int soundid){
        mSPool.play(soundid, volume, volume,1, 0, 1f);
    }
}

Interface used when sounds are loaded, implemented by activity:

public interface IOnSoundReady {
    void onSoundReady();
}

Usage in your activity:

Sounds mySounds = Sounds.getInstance(this, this);

Play sound:

public static void playSound(int soundid) {
    mySounds.playSound(soundid);
}

Hope I could help a bit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...