SHARE
TWEET

Android Music Player (Main Activity.java)

Zeemax Jun 21st, 2015 (edited) 591 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Environment;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ListView;
  13.  
  14. import java.io.File;
  15. import java.util.ArrayList;
  16.  
  17.  
  18. public class MainActivity extends ActionBarActivity {
  19.  
  20.     private ListView listView;
  21.     private String [] items;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.  
  28.         listView = (ListView) findViewById(R.id.listView);
  29.         final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
  30.         items = new String [mySongs.size()];
  31.  
  32.         for (int i = 0 ; i < mySongs.size(); i++){
  33.             items[i] = mySongs.get(i).getName().toString();
  34.         }
  35.  
  36.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.song_item,R.id.textView,items);
  37.         listView.setAdapter(adapter);
  38.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  39.             @Override
  40.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  41.                 startActivity(new Intent(MainActivity.this, Player.class).putExtra("pos",position).putExtra("songlist",mySongs));
  42.             }
  43.         });
  44.  
  45.  
  46.     }
  47.  
  48.     public ArrayList<File > findSongs (File root){
  49.  
  50.         ArrayList<File> all = new ArrayList<File>();
  51.  
  52.         File [] files = root.listFiles();
  53.  
  54.         for (File singleFile: files){
  55.  
  56.             if (singleFile.isDirectory() && !singleFile.isHidden()){
  57.                 all.addAll(findSongs(singleFile));
  58.             }
  59.             else
  60.                 if (singleFile.getName().endsWith(".mp3")){
  61.                     all.add(singleFile);
  62.                 }
  63.         }
  64.         return all;
  65.     }
  66.  
  67.     @Override
  68.     public boolean onCreateOptionsMenu(Menu menu) {
  69.         // Inflate the menu; this adds items to the action bar if it is present.
  70.         getMenuInflater().inflate(R.menu.menu_main, menu);
  71.         return true;
  72.     }
  73.  
  74.     @Override
  75.     public boolean onOptionsItemSelected(MenuItem item) {
  76.         // Handle action bar item clicks here. The action bar will
  77.         // automatically handle clicks on the Home/Up button, so long
  78.         // as you specify a parent activity in AndroidManifest.xml.
  79.         int id = item.getItemId();
  80.  
  81.         //noinspection SimplifiableIfStatement
  82.         if (id == R.id.action_settings) {
  83.             return true;
  84.         }
  85.  
  86.         return super.onOptionsItemSelected(item);
  87.     }
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top