Back to project page FuzzyPlayer.
The source code is released under:
GNU General Public License
If you think the Android project FuzzyPlayer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.susu.music; /* w w w . j a v a 2 s. c om*/ import java.io.File; import java.io.IOException; import java.io.FilenameFilter; import java.util.List; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.content.Context; import android.media.MediaPlayer; import android.widget.Toast; import android.widget.ArrayAdapter; import android.util.Log; import android.widget.ListView; import android.view.View; import android.view.Gravity; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; public class MusicTutorial extends Activity implements OnItemClickListener, OnClickListener { static class Mp3Filter implements FilenameFilter { public boolean accept(File dir, String name) { return name.endsWith(".mp3"); } } private static final String TAG = "MusicTutorial"; private static final String MEDIA_PATH = "/sdcard/Music/"; private List<String> m_songs = new ArrayList<String>(); private MediaPlayer m_player = new MediaPlayer(); private int m_currentPos = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv = (ListView)findViewById(R.id.song_list); lv.setOnItemClickListener(this); updateSongList(); findViewById(R.id.play_button).setOnClickListener(this); findViewById(R.id.next_button).setOnClickListener(this); } private void updateSongList() { File home = new File(MEDIA_PATH); File[] files = home.listFiles(new Mp3Filter()); if (files.length > 0) { m_songs.clear(); for(File f : files) { m_songs.add(f.getName()); Log.d(TAG, "------- FILE FOUND: " + f.getName()); } // toastMessage( "I've found " + m_songs.size() + " file..."); ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_item,m_songs); ListView lv = (ListView)findViewById(R.id.song_list); lv.setAdapter(songList); songList.notifyDataSetChanged(); } else { toastMessage( "No MP3 files found!"); } } @Override public void onItemClick(AdapterView<?> l, View v, int pos, long id) { m_currentPos = pos; playSong( MEDIA_PATH + m_songs.get(pos)); } @Override public void onClick(View v) { try { switch(v.getId()) { case R.id.play_button: playPause(); break; case R.id.next_button: nextSong(); break; } } catch(IllegalStateException e) { toastMessage("ERROR: player is not paused/playing!"); } } private void nextSong() { m_currentPos = ((1 + m_currentPos) % m_songs.size()); playSong( MEDIA_PATH + m_songs.get(m_currentPos)); } private void playPause() { if (m_player.isPlaying()) { m_player.pause(); ((Button)findViewById(R.id.play_button)).setText("Play"); } else { m_player.start(); ((Button)findViewById(R.id.play_button)).setText("Pause"); } } private void playSong(String path) { try { m_player.reset(); m_player.setDataSource(path); m_player.prepare(); m_player.start(); ((Button)findViewById(R.id.play_button)).setText("Pause"); } catch(IOException e) { Log.v(TAG, e.getMessage()); } } private void toastMessage(CharSequence text) { Context ctx = getApplicationContext(); int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(ctx,text,duration); toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0); toast.show(); } }