Advertisement
Guest User

Untitled

a guest
Jan 12th, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. package com.example.tushar.primeplay2;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.database.Cursor;
  5. import android.graphics.Color;
  6. import android.graphics.PorterDuff;
  7. import android.graphics.PorterDuffColorFilter;
  8. import android.graphics.Typeface;
  9. import android.graphics.drawable.ColorDrawable;
  10. import android.media.MediaPlayer;
  11. import android.net.Uri;
  12. import android.os.Build;
  13. import android.os.Handler;
  14. import android.provider.MediaStore;
  15. import android.support.v7.app.ActionBarActivity;
  16. import android.os.Bundle;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.widget.ImageButton;
  21. import android.widget.SeekBar;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24.  
  25. import java.util.ArrayList;
  26.  
  27.  
  28. public class player extends ActionBarActivity {
  29.  
  30. private ImageButton play;
  31. private ArrayList songs = new ArrayList();
  32. private boolean thread = true;
  33. private double startTime = 0;
  34. private double finalTime = 0;
  35. private int forwardTime = 5000;
  36. private int backwardTime = 5000;
  37. private SeekBar seekbar;
  38. private Thread t;
  39. private int tdf = 0;
  40. private TextView timer, duration;
  41. private MediaPlayer mp;
  42. Runnable r;
  43.  
  44.  
  45. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_player);
  50. getSupportActionBar().hide();
  51. String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
  52. Cursor cursor;
  53. String[] projection = {
  54. MediaStore.Audio.Media.DATA
  55. };
  56. cursor = this.getContentResolver().query(
  57. MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  58. projection,
  59. selection,
  60. null,
  61. null);
  62. while(cursor.moveToNext()){
  63. songs.add(cursor.getString(0));
  64. }
  65. Bundle extras = getIntent().getExtras();
  66. String songId = null;
  67. play = (ImageButton) findViewById(R.id.play);
  68. seekbar = (SeekBar)findViewById(R.id.seekBar);
  69. if(extras != null){
  70. songId = extras.getString("id");
  71. play.setBackgroundResource(R.drawable.pause);
  72. }
  73. find(songId);
  74. seekbar.setMax(mp.getDuration());
  75. timer = (TextView)findViewById(R.id.timer);
  76. duration = (TextView)findViewById(R.id.duration);
  77. Typeface type = Typeface.createFromAsset(getAssets(), "fonts/EuropeUnderground_black.ttf");
  78. timer.setTypeface(type);
  79. duration.setTypeface(type);
  80. int seconds=(mp.getDuration() / 1000) % 60;
  81. long minutes=((mp.getDuration() - seconds) / 1000) / 60;
  82. duration.setText("" + minutes + ":" + seconds);
  83. final Handler handler = new Handler();
  84. final Handler timerHandler = new Handler();
  85. final Runnable updateTask = new Runnable() {
  86. @Override
  87. public void run() {
  88. update();
  89. handler.postDelayed(this,1000);
  90. }
  91. };
  92. final Runnable timerTextTask=new Runnable() {
  93. @Override
  94. public void run() {
  95. timerText();
  96. timerHandler.postDelayed(this, 1000);
  97. }
  98. };
  99.  
  100. timerHandler.postDelayed(timerTextTask,0);
  101. timerHandler.postDelayed(updateTask,1000);
  102. play.setOnClickListener(new View.OnClickListener(){
  103.  
  104. @Override
  105. public void onClick(View v) {
  106. if(mp.isPlaying()){
  107. play.setBackgroundResource(R.drawable.play);
  108. mp.pause();
  109. } else {
  110. play.setBackgroundResource(R.drawable.pause);
  111. mp.start();
  112. }
  113. }
  114. });
  115.  
  116. seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  117. @Override
  118. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  119. if(fromUser){
  120. //mp.seekTo(progress);
  121. }
  122. }
  123.  
  124. @Override
  125. public void onStartTrackingTouch(SeekBar seekBar) {
  126.  
  127. }
  128.  
  129. @Override
  130. public void onStopTrackingTouch(SeekBar seekBar) {
  131. mp.seekTo(seekBar.getProgress());
  132. }
  133. });
  134.  
  135. }
  136.  
  137. private void find(String songName) {
  138. for(int t = 0; t < songs.size(); t++){
  139. if(("" + songs.get(t)).endsWith(songName)){
  140. Uri myUri = Uri.parse("" + songs.get(t));
  141. stopPlaying();
  142. mp = MediaPlayer.create(this, myUri);
  143. mp.setLooping(false);
  144. mp.start();
  145. break;
  146. }
  147. }
  148. }
  149.  
  150. private void stopPlaying() {
  151. if (mp!=null && mp.isPlaying()) {
  152. mp.stop();
  153. mp.release();
  154. mp = null;
  155. }
  156. }
  157.  
  158. private void timerText(){
  159. if(mp.isPlaying()){
  160. int seconds=(seekbar.getProgress() / 1000) % 60;
  161. long minutes=((seekbar.getProgress() - seconds) / 1000) / 60;
  162. timer.setText("" + minutes + ":" + seconds);
  163. }
  164. }
  165.  
  166. private void update() {
  167. if(mp.isPlaying()){
  168. seekbar.setProgress(mp.getCurrentPosition());
  169.  
  170. }
  171.  
  172. }
  173.  
  174.  
  175. @Override
  176. public boolean onCreateOptionsMenu(Menu menu) {
  177. // Inflate the menu; this adds items to the action bar if it is present.
  178. getMenuInflater().inflate(R.menu.menu_player, menu);
  179. return true;
  180. }
  181.  
  182. @Override
  183. public boolean onOptionsItemSelected(MenuItem item) {
  184. // Handle action bar item clicks here. The action bar will
  185. // automatically handle clicks on the Home/Up button, so long
  186. // as you specify a parent activity in AndroidManifest.xml.
  187. int id = item.getItemId();
  188.  
  189. //noinspection SimplifiableIfStatement
  190. if (id == R.id.action_settings) {
  191. return true;
  192. }
  193.  
  194. return super.onOptionsItemSelected(item);
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement