-1

I have application that will search for song on external storage and play it by clicking a button. I use String path = getFullFilePath(getApplicationContext(), "FILENAME"); and it works fine if i type something like song.mp3...I tried to use EditText.getText().toString(); except "FILENAME" and it doesnt work. This is my code in OnCreate:

EditText et = (EditText) findViewById(R.id.et1);
            String string = et.getText().toString();
        sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        String path = getFullFilePath(getApplicationContext(), string);
        mSoundId = sp.load(path, 1);

And LogCat says `11-07 14:26:39.909: W/SoundPool(12627): sample 1 not READY

BUT if I input et.setText("SongName"); it works fine.. I want to type something in edittext and then use it as filename! Thanks `

slezadav
  • 6,104
  • 7
  • 40
  • 61
Tom Jackson
  • 230
  • 3
  • 17
  • In OnCreate is there any event happening like Button Click or something. Then on button click you get text from edittext. – Raghunandan Nov 07 '12 at 13:37
  • Only this is in my button click public void button1(View view) { if (mStreamId != 0) { sp.stop(mStreamId); } mStreamId = sp.play(mSoundId, 1, 1, 1, 0, 1f); } – Tom Jackson Nov 07 '12 at 13:40

2 Answers2

1

Do this in OnCreate:

mEditText = (EditText) findViewById(R.id.et1); 
mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    public void onLoadComplete (SoundPool soundPool, int sampleId, int status) {
        mStreamId = soundPool.play(sampleId, 1, 1, 1, 0, 1f);
    }
}

Then in your onClick do:

public void yourOnClickMethod(View view) { 
    if (mStreamId != 0) { 
        mSoundPool.stop(mStreamId); 
    } 

    String path = getFullFilePath(getApplicationContext(), EditText.getText().toString());
    mSoundId = mSoundPool.load(path, 1); 
}
PKeno
  • 2,694
  • 7
  • 20
  • 37
  • 11-07 15:07:01.659: W/SoundPool(14064): sample 1 not READY – Tom Jackson Nov 07 '12 at 14:07
  • check out [this question](http://stackoverflow.com/questions/2116910/warn-soundpool-sample-2-not-ready) and [SoundPool.OnLoadCompleteListener](http://developer.android.com/reference/android/media/SoundPool.OnLoadCompleteListener.html). It takes a while for the sample to be ready. – PKeno Nov 07 '12 at 15:06
0

OnCreate will only be called one time for each lifetime of the Activity. So OnCreate is used to initialize UI. So it does not get the change on EditText. Have a button. On Click get the data from edittext. That should solve your problem.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I did like this if (mStreamId != 0) { sp.stop(mStreamId); } String path = getFullFilePath(getApplicationContext(), mEditText.getText().toString()); mSoundId = sp.load(path, 1); mStreamId = sp.play(mSoundId, 1, 1, 1, 0, 1f); } And i am getting same error. – Tom Jackson Nov 07 '12 at 14:07
  • http://stackoverflow.com/questions/5202510/soundpool-sample-not-ready. http://stackoverflow.com/questions/7786314/android-2-2-soundpool-sample-0-is-not-ready. Have a look a these links. – Raghunandan Nov 07 '12 at 14:20
  • if i type in edittext filename that doesnt exist on my external storage then i get something like this 11-07 15:24:35.219: E/SoundPool(14487): error loading /mnt/sdcard/dub.mp3 But if i put dub2.mp3 that exists i get 11-07 15:24:35.219: E/SoundPool(14487): error loading /mnt/sdcard/dub.mp3 and everytime i click the button again it gives sample 2 not ready..next time sample 3 not ready... – Tom Jackson Nov 07 '12 at 14:27