Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. package com.example.mp3;
  2.  
  3.  
  4. import java.io.File;
  5. import java.util.ArrayList;
  6.  
  7. import android.app.ActionBar;
  8. import android.app.Activity;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.graphics.Color;
  12. import android.graphics.drawable.ColorDrawable;
  13. import android.media.MediaPlayer;
  14. import android.media.MediaPlayer.OnCompletionListener;
  15. import android.net.Uri;
  16. import android.os.Bundle;
  17. import android.os.Environment;
  18. import android.preference.PreferenceManager;
  19.  
  20. import android.util.Log;
  21. import android.view.MenuItem;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24.  
  25. import android.widget.AdapterView;
  26. import android.widget.ArrayAdapter;
  27. import android.widget.Button;
  28. import android.widget.EditText;
  29. import android.widget.ImageButton;
  30. import android.widget.ListView;
  31. import android.widget.TextView;
  32. import android.widget.AdapterView.OnItemClickListener;
  33. import android.widget.Toast;
  34.  
  35.  
  36. public class MainActivity extends Activity implements View.OnClickListener, OnCompletionListener {
  37.  
  38. ListView list;
  39. ArrayAdapter<String> listAdapter ;
  40. ArrayList<String> listTest;
  41. ImageButton play,stop,back,next;
  42. String songpath,song;
  43. File[] listFile;
  44. SharedPreferences sharedPref;
  45. MediaPlayer mp;
  46. private static final String TAG = MainActivity.class.getSimpleName();
  47.  
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50.  
  51.  
  52.  
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.activity_main);
  55.  
  56. sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  57. song = sharedPref.getString("songname", "name");
  58.  
  59. mp=new MediaPlayer();
  60.  
  61. mp.setOnCompletionListener(this);
  62.  
  63.  
  64. list = (ListView)findViewById(R.id.list);
  65. listTest = new ArrayList<String>( );
  66.  
  67. play = (ImageButton)findViewById(R.id.play);
  68. back = (ImageButton)findViewById(R.id.prev);
  69. next = (ImageButton)findViewById(R.id.next);
  70.  
  71. //adding listeners
  72. play.setOnClickListener(this);
  73. back.setOnClickListener(this);
  74. next.setOnClickListener(this);
  75.  
  76.  
  77. //action bar controls
  78. ActionBar bar = getActionBar();
  79. bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));
  80.  
  81. Scanner("/sdcard/");////storage path
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. /////////////////////////////////////*Adding listener to songs*//////////////////////////////////////////////////
  86. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87.  
  88. if(listTest.size() != 0)
  89. {
  90. listAdapter = new ArrayAdapter<String> (MainActivity.this,R.layout.simplerow, listTest);
  91. list.setAdapter(listAdapter);
  92. list.setOnItemClickListener(new OnItemClickListener()
  93. {
  94. public void onItemClick(AdapterView<?> parent, View view, int position, long id) //can two text be accessed here??
  95. {
  96.  
  97.  
  98. //accessing the song name
  99. String name = (String) ((TextView) view).getText();
  100. //Log.e(TAG, name);
  101.  
  102. Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
  103. try{
  104. mp.reset();
  105. mp.setDataSource(name);//source
  106. mp.prepare();
  107. mp.start();
  108. play.setImageResource(R.drawable.pause);
  109. }
  110. catch(Exception e){e.printStackTrace();}
  111.  
  112.  
  113. }
  114.  
  115.  
  116. });
  117.  
  118.  
  119.  
  120.  
  121. }
  122.  
  123. }
  124.  
  125.  
  126.  
  127. ////////////////////////////////////////////////////////////////////////////////////////////
  128. //////////////////////////////////*Songs added here to list*////////////////////////////////
  129. ////////////////////////////////////////////////////////////////////////////////////////////
  130.  
  131. private void Scanner(String path) {
  132. // TODO Auto-generated method stub
  133. {
  134. try
  135. {
  136. File fl = new File(path);
  137. File[] listOfFiles = fl.listFiles();
  138.  
  139. for (File listOfFile : listOfFiles)
  140. {
  141. String s = listOfFile.getName();
  142.  
  143. if(s.endsWith(".mp3"))
  144. {
  145.  
  146. songpath = listOfFile.getPath();
  147. listTest.add(songpath);//adding song names to list
  148.  
  149.  
  150. }
  151.  
  152.  
  153. /////////////////////////////////
  154. File f = new File(path+s+"/");
  155. if (f.exists() && f.isDirectory()) {
  156. Scanner(path+s+"/");
  157. }
  158. ////////////////////////////////
  159.  
  160.  
  161. }
  162.  
  163.  
  164.  
  165. }
  166. catch (Exception e) { }
  167. }
  168.  
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. @Override
  179. public void onClick(View v) {
  180. // TODO Auto-generated method stub
  181.  
  182. if (v.equals(play))
  183. {
  184. if(mp.isPlaying())
  185. {
  186. mp.pause();
  187. Toast.makeText(MainActivity.this, "paused", Toast.LENGTH_SHORT).show();
  188. //change in button image//
  189. play.setImageResource(R.drawable.play);
  190.  
  191. }
  192. else
  193. {
  194. mp.start();
  195. Toast.makeText(MainActivity.this, "started", Toast.LENGTH_SHORT).show();
  196. //change in button image//
  197. play.setImageResource(R.drawable.pause);
  198. //
  199. }
  200. }
  201.  
  202.  
  203. if (v.equals(back))
  204. {
  205. mp.setDataSource(name);
  206. Toast.makeText(MainActivity.this, "back", Toast.LENGTH_SHORT).show();
  207.  
  208.  
  209.  
  210. }
  211.  
  212. }
  213.  
  214.  
  215. @Override
  216. public void onCompletion(MediaPlayer arg0) {
  217. // TODO Auto-generated method stub
  218.  
  219. }
  220.  
  221. @Override
  222. protected void onPause() {
  223. super.onPause();
  224. mp.stop();
  225. Toast.makeText(getApplicationContext(), "paused", Toast.LENGTH_LONG).show();
  226.  
  227. }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement