Monday, December 7, 2015

ListView for Displaying File

From the last documentation about  ListView I have explored on how to make a list of string. I though that clicking on a list view will certainly play a sound file (for the time being *mp3 files). But first thing first is about clicking a directory name that will show what kinds of files reside inside it.

The program requires several objects  that can accommodate  above action. The classes are :
  1. FileArray.
  2. File
The steps of operation on how to operate on files are listed below : 
  1. Environment.getExternalStorageDirectory.listFiles() returns directories and files inside a particular directory. The returns (plural : because it returns many files object) are stored inside file array.  
  2. An object of ArrayList will hold array of file (representation of file or directory)
  3. An object of ArrayAdapter that acts as adapter between object of ArrayList and object of ListView
To make things short I show layout file :
/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    Copyright 2015, Joko Adianto 

   */
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Next is MainActivity.java file (this is not a complete program, It cannot play music when i click a file , not a directory, it gives an error. This is not a complete program. It will be complete on the next article when it can play music) :

/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    Copyright 2015, Joko Adianto 

   */
package com.example.joko.listviewfiles;

import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    ArrayAdapter<String> arrayAdapter;
    File fileArray[];
    MediaPlayer mediaPlayer; //Declare Reference Variable

    @Override    protected void onDestroy() {
        mediaPlayer.stop();
        super.onDestroy();
    }

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

        fileArray = Environment.getExternalStorageDirectory().listFiles();
        listView = (ListView) findViewById(R.id.listView);
        mediaPlayer = new MediaPlayer(); //mediaPlayer refers a new object of MediaPlayer;        final ArrayList<String> arrayList = new ArrayList<String>();
        j = fileArray.length;
        for(i=0; i< j; i++){
            arrayList.add(fileArray[i].getPath()+"");
        }

        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

        // Assign adapter to ListView        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int i, j;
                String str = (String) parent.getItemAtPosition(position);
                arrayList.clear();
                File file = new File(str);
                arrayList.clear();
                arrayList.add(str);
                listView.setAdapter(arrayAdapter);

                fileArray = file.listFiles();
                j = fileArray.length;
                for (i = 0; i < j; i++) {
                    arrayList.add(fileArray[i].getPath() + "");
                    }
                    listView.setAdapter(arrayAdapter);
                }

        });
    }
}
 
Next I am going to play *mp3 files.

Friday, December 4, 2015

List on Screen : Using ListView


A List is needed for displaying playable list. The basic thinking on how to use a ListView is explained on the next paragraph. When building this example several controls are used :
  1. ListView. I place ListView in both layout xml files and in java android programs.
  2. In java android program, I use several classes :
    1. ArrayAdapter
    2. ArrayList
    3. List
    4. ListView 
On using array adapter I found out that both android.R.layout.simple_list_item_1 and android.R.id.text1 have been already defined in the library.

We need to put ListView control in layout. The snippet of xml source code is :
    /*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   
    Copyright 2015, Joko Adianto 

   */

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
/>


Afterward we need to hook java code on the ListView control named listView :
   
   /*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
    Copyright 2015, Joko Adianto
     */

public class MainActivity extends Activity {
    ListView listView;
    List<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        //creating empty array list of string
        ArrayList<String> arrayList = new ArrayList<String>();
        //Populate it with several string of character
        arrayList.add("AAAAA");
        arrayList.add("BAAAA");
        arrayList.add("CAAAA");
        arrayList.add("DAAAA");
        arrayList.add("EAAAA");
       //We need to have an array adapter for displaying an array list
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

        // Assign adapter to ListView
        listView.setAdapter(arrayAdapter);

         //adding another string
        arrayList.add("fAAAA");
        arrayList.add("gAAAA");
    }   

When the program running, the screen below display the list. It is a simple list view example program. It does not have any event listeners attach to it.

Picture 1. The Screen Shoot of ListView Example

On the next article about file access I will document on how to display files.

Thursday, December 3, 2015

Realm of AudioFX

I read the documentation about AudioFX library. Basically it consists of subclasses :

  1. BassBoost. Bass boost is an audio effect to boost or amplify low frequencies of the sound
  2. EnvironmentReverb. Controlling/simulating echoes The EnvironmentalReverb class allows an application to control each reverb engine property in a global reverb environment and is more suitable for games
  3. Equalizer. alter the frequency response of a particular music source or of the main output mix
  4. PresetReverbs.  The PresetReverb class allows an application to configure the global reverb using a reverb preset. This is primarily used for adding some reverb in a music playback context
  5. Virtualizer. spatialize audio channels. 
I plan to use these concrete classes in my project(s). But at this stage I haven't decided yet which classes for which component(s) of my project(s). My project will consist of :

  1. Trackers
  2. Check boxes
  3. Some colourful effect
  4. ability to convert to various formats


Tuesday, December 1, 2015

Hello World

This blog site contains my journey in building AudioFX in android devices. I have outlined the first project is an mp3 player with equalizer. Next would be file converter, that can convert files with certain formats to another formats.

This blog site is a journal on my experience in using android library regarding Audio Effects in android

Any source code will be released under GPL3, except if I fell that I make exception . Therefore you may use any software for any purpose, but without any waranty. Be aware about any copyright notice.

Cheers
Joko Adianto