Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tushar.primeplay2;
- import android.annotation.TargetApi;
- import android.database.Cursor;
- import android.graphics.Color;
- import android.graphics.PorterDuff;
- import android.graphics.PorterDuffColorFilter;
- import android.graphics.Typeface;
- import android.graphics.drawable.ColorDrawable;
- import android.media.MediaPlayer;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Handler;
- import android.provider.MediaStore;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ImageButton;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class player extends ActionBarActivity {
- private ImageButton play;
- private ArrayList songs = new ArrayList();
- private boolean thread = true;
- private double startTime = 0;
- private double finalTime = 0;
- private int forwardTime = 5000;
- private int backwardTime = 5000;
- private SeekBar seekbar;
- private Thread t;
- private int tdf = 0;
- private TextView timer, duration;
- private MediaPlayer mp;
- Runnable r;
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_player);
- getSupportActionBar().hide();
- String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
- Cursor cursor;
- String[] projection = {
- MediaStore.Audio.Media.DATA
- };
- cursor = this.getContentResolver().query(
- MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
- projection,
- selection,
- null,
- null);
- while(cursor.moveToNext()){
- songs.add(cursor.getString(0));
- }
- Bundle extras = getIntent().getExtras();
- String songId = null;
- play = (ImageButton) findViewById(R.id.play);
- seekbar = (SeekBar)findViewById(R.id.seekBar);
- if(extras != null){
- songId = extras.getString("id");
- play.setBackgroundResource(R.drawable.pause);
- }
- find(songId);
- seekbar.setMax(mp.getDuration());
- timer = (TextView)findViewById(R.id.timer);
- duration = (TextView)findViewById(R.id.duration);
- Typeface type = Typeface.createFromAsset(getAssets(), "fonts/EuropeUnderground_black.ttf");
- timer.setTypeface(type);
- duration.setTypeface(type);
- int seconds=(mp.getDuration() / 1000) % 60;
- long minutes=((mp.getDuration() - seconds) / 1000) / 60;
- duration.setText("" + minutes + ":" + seconds);
- final Handler handler = new Handler();
- final Handler timerHandler = new Handler();
- final Runnable updateTask = new Runnable() {
- @Override
- public void run() {
- update();
- handler.postDelayed(this,1000);
- }
- };
- final Runnable timerTextTask=new Runnable() {
- @Override
- public void run() {
- timerText();
- timerHandler.postDelayed(this, 1000);
- }
- };
- timerHandler.postDelayed(timerTextTask,0);
- timerHandler.postDelayed(updateTask,1000);
- play.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(mp.isPlaying()){
- play.setBackgroundResource(R.drawable.play);
- mp.pause();
- } else {
- play.setBackgroundResource(R.drawable.pause);
- mp.start();
- }
- }
- });
- seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- if(fromUser){
- //mp.seekTo(progress);
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- mp.seekTo(seekBar.getProgress());
- }
- });
- }
- private void find(String songName) {
- for(int t = 0; t < songs.size(); t++){
- if(("" + songs.get(t)).endsWith(songName)){
- Uri myUri = Uri.parse("" + songs.get(t));
- stopPlaying();
- mp = MediaPlayer.create(this, myUri);
- mp.setLooping(false);
- mp.start();
- break;
- }
- }
- }
- private void stopPlaying() {
- if (mp!=null && mp.isPlaying()) {
- mp.stop();
- mp.release();
- mp = null;
- }
- }
- private void timerText(){
- if(mp.isPlaying()){
- int seconds=(seekbar.getProgress() / 1000) % 60;
- long minutes=((seekbar.getProgress() - seconds) / 1000) / 60;
- timer.setText("" + minutes + ":" + seconds);
- }
- }
- private void update() {
- if(mp.isPlaying()){
- seekbar.setProgress(mp.getCurrentPosition());
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_player, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement