Sound FX touches to Note To Self

As it has become customary over the last few chapters, we will use our new-found knowledge to add an enhancement to our Note To Self app. This chapter will be the last time we do this, however, before we move on to new coding pastures.

Using your operating system's file browser, go to the appsrcmain folder of the project and add a new folder called assets.

There is a sound file called beep.ogg ready-made for you in the Chapter 17/Note To Self/assets folder of the download bundle. Of course, you can make your own sound FX using Bfxr if you prefer.

Place the file into the assets directory that you just created.

Now, we can add some Java code to play the beep sound each time a note from the list is selected.

First, let's add a couple of new member variables to the MainActivity class:

public class MainActivity extends AppCompatActivity {

  Animation mAnimFlash;
  Animation mFadeIn;

  int mIdBeep = -1;
  SoundPool mSp;

  private NoteAdapter mNoteAdapter;
  private boolean mSound;
  private int mAnimOption;
  private SharedPreferences mPrefs;



  @Override
  protected void onCreate(Bundle savedInstanceState) {
 …
 …

Now, in onCreate, as we have done in the Sound Demo app, after the call to setContentView, add code to initialize SoundPool in a version-specific manner, as highlighted in this next code:

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

  // Instantiate our sound pool
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_MEDIA)
    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
    .build();

    mSp = new SoundPool.Builder()
    .setMaxStreams(5)
    .setAudioAttributes(audioAttributes)
    .build();
    } else {
      mSp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    }


    try{
      // Create objects of the 2 required classes
      AssetManager assetManager = this.getAssets();
      AssetFileDescriptor descriptor;

      // Load our fx in memory ready for use
      descriptor = assetManager.openFd("beep.ogg");
      mIdBeep = mSp.load(descriptor, 0);


     }catch(IOException e){
      // Print an error message to the console
      Log.e("error", "failed to load sound files");
    }
  mNoteAdapter = new NoteAdapter();

  ListView listNote = (ListView) findViewById(R.id.listView);

  listNote.setAdapter(mNoteAdapter);
 …
 …

Now, add this next code that plays a sound in the onItemClick method. I have added some context and highlighted the new code to make it plain as to where it goes:

…
public void onItemClick(AdapterView<?> adapter, View view, int whichItem, long id) {

  if(mSound) {
    mSp.play(mIdBeep, 1, 1, 0, 0, 1);
  }

  Note tempNote = mNoteAdapter.getItem(whichItem);
…

That's it. You can now run the Note To Self app and receive a satisfying click each time a note from the list is clicked on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.246.203