Last active
October 2, 2015 18:04
-
-
Save Domacoski/5d067ecfaf4e091233bf to your computer and use it in GitHub Desktop.
Cria uma lista que exibe os mp3 disponíveis no Storage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tl.domacoski; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.annotation.SuppressLint; | |
import android.media.MediaPlayer; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
/** | |
* {@link Fragment} que exibe a lista de mp3 disponiveis no Storage. | |
* | |
*/ | |
public class Mp3Fragment extends Fragment { | |
private MediaPlayer mPlayer; | |
private ListView listView; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
final View rootView = inflater.inflate(R.layout.mp3_fragment, container, false); | |
this.listView = ListView.class.cast(rootView.findViewById(R.id.lista)); | |
final List<TabSom3> list = getPlayList(); | |
final Adapter adapter = new Adapter(this, list); | |
this.listView.setAdapter(adapter); | |
this.listView.refreshDrawableState(); | |
return rootView; | |
} | |
/** | |
* Retona a lista de objetos comforme arquivos existentes. | |
* @return {@link List} | |
*/ | |
public List<TabSom3> getPlayList(){ | |
final List<TabSom3> list = new ArrayList<TabSom3>(0); | |
final List<File> files = getMp3File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)); | |
for(final File mp3 :files){ | |
final TabSom3 som3 = new TabSom3(); | |
som3.ArqSom = mp3.getAbsolutePath(); | |
som3.NomeSom = mp3.getName(); | |
list.add(som3); | |
} | |
return list; | |
} | |
/** | |
* Retorna todos os arquivos que contem mp3 no nome. | |
* @param file | |
* @return {@link List} | |
*/ | |
public List<File> getMp3File(final File file){ | |
final List<File> lst = new ArrayList<File>(0); | |
for(final File child : file.listFiles()){ | |
if(child.isDirectory()){ | |
final List<File> lst2 = getMp3File(child); | |
if(null != lst2){ | |
lst.addAll(lst2); | |
} | |
}else{ | |
if(isMp3(child)){ | |
lst.add(child); | |
} | |
} | |
} | |
return lst; | |
} | |
/** | |
* Retorna true se contem mmp3 no nome do arquivo | |
* @param file | |
* @return boolean | |
*/ | |
private boolean isMp3(final File file){ | |
if(null == file) return false; | |
return file.getName().contains("mp3"); | |
} | |
/** | |
* Toca um arquivo atraves do {@link MediaPlayer} | |
* @param tabSom3 | |
*/ | |
public void play(final TabSom3 tabSom3){ | |
if(null == tabSom3) return; | |
if(null != mPlayer){ | |
mPlayer.stop(); | |
} | |
try { | |
mPlayer = new MediaPlayer(); | |
mPlayer.setDataSource(getActivity(), Uri.parse(tabSom3.ArqSom)); | |
mPlayer.prepare(); | |
mPlayer.start(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Classe controlada pelo {@link Adapter} | |
*/ | |
static class TabSom3{ | |
public String NomeSom; | |
public String ArqSom; | |
} | |
/** | |
*Controla a {@link ListView}. | |
*Gerencia o click sobre um elemento da lista. | |
*/ | |
@SuppressLint("ResourceAsColor") | |
static class Adapter extends ArrayAdapter<TabSom3> implements View.OnClickListener{ | |
private final LayoutInflater inflater; | |
final Mp3Fragment fragment; | |
public Adapter(Mp3Fragment context, final List<TabSom3> list) { | |
super(context.getActivity(), R.layout.audio_item, list); | |
this.inflater = LayoutInflater.from(context.getActivity()); | |
this.fragment = context; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
TabSom3 tab = null; | |
if(convertView == null){ | |
convertView = inflater.inflate(R.layout.audio_item, null); | |
tab = getItem(position); | |
convertView.setTag(tab); | |
convertView.setOnClickListener(this); | |
}else{ | |
if(null != convertView.getTag()){ | |
tab = TabSom3.class.cast(convertView.getTag()); | |
} | |
} | |
if(null != tab){ | |
TextView.class.cast(convertView.findViewById(R.id.nameSom)).setText(tab.NomeSom); | |
} | |
return convertView; | |
} | |
@Override | |
public void onClick(final View v) { | |
if(null == v || null == v.getTag()) return; | |
fragment.play(TabSom3.class.cast(v.getTag())); | |
v.setBackgroundColor(android.R.color.darker_gray); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment