Advertisement
shashiskills

Untitled

Oct 29th, 2012
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import com.kant.MangoPlayer.R;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.media.AudioManager;
  8. import android.media.MediaPlayer;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.SeekBar;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. /**
  17.  * @author kantsh
  18.  *         This code can be used for development purposes and is free to use.
  19.  *         Completed on Tuesday,May 01,2012
  20.  *         mailto "kant.shashi@hotmail.com"
  21.  */
  22. public class MangoMediaPlayerActivity extends Activity implements Runnable {
  23.  /* Called when the activity is first created. */
  24.  
  25.  MangoMediaPlayerActivity mangoplayer;
  26.  Thread currentThread;
  27.  private String songUrl;
  28.  private int MediaFetchResultCode = 11;
  29.  private TextView fileSelected;
  30.  private Button playIt;
  31.  private Button stopIt;
  32.  
  33.  private MediaPlayer mp;
  34.  private AudioManager audioManager;
  35.  private SeekBar mediabar;
  36.  private int total;
  37.  
  38.  private boolean Complete;
  39.  
  40.  @Override
  41.  public void onCreate(Bundle savedInstanceState) {
  42.   super.onCreate(savedInstanceState);
  43.   setContentView(R.layout.main);
  44.  
  45.   fileSelected = (TextView) findViewById(R.id.autoCompleteTextViewer);
  46.   Button browser = (Button) findViewById(R.id.bSelectFile);
  47.   playIt = (Button) findViewById(R.id.bPlay);
  48.   stopIt = (Button) findViewById(R.id.bstop);
  49.   mediabar = (SeekBar) findViewById(R.id.mediaSeekBar);
  50.  
  51.   mangoplayer = this;
  52.  
  53.   mp = new MediaPlayer();
  54.   mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  55.  
  56.    @Override
  57.    public void onCompletion(MediaPlayer mp) {
  58.     Complete = true;
  59.    }
  60.   });
  61.  
  62.   browser.setOnClickListener(new View.OnClickListener() {
  63.    @Override
  64.    public void onClick(View v) {
  65.     // intent name ="com.kant.MangoPlayer.FileExplore"
  66.     Intent intent = new Intent("com.kant.MangoPlayer.FileExplore");
  67.     startActivityForResult(intent, MediaFetchResultCode);
  68.    }
  69.   });
  70.  
  71.   playIt.setOnClickListener(new View.OnClickListener() {
  72.  
  73.    @Override
  74.    public void onClick(View v) {
  75.     try {
  76.      Complete = true; // if u browse while playing then thread must be stopped    
  77.      mp.reset();
  78.      mp.setDataSource(songUrl);
  79.      mp.prepare();
  80.  
  81.      total = mp.getDuration(); // total duration of the song
  82.      mediabar.setProgress(0);
  83.      mediabar.setMax(total);
  84.  
  85.      Complete = false;
  86.      currentThread = new Thread(mangoplayer);
  87.      currentThread.start(); // start the thread to start listening
  88.      mp.start(); // now start the media player
  89.     } catch (IllegalArgumentException e) {
  90.      // TODO Auto-generated catch block
  91.      e.printStackTrace();
  92.     } catch (IllegalStateException e) {
  93.      // TODO Auto-generated catch block
  94.      e.printStackTrace();
  95.     } catch (IOException e) {
  96.      // TODO Auto-generated catch block
  97.      e.printStackTrace();
  98.     }
  99.    }
  100.   });
  101.  
  102.   stopIt.setOnClickListener(new View.OnClickListener() {
  103.  
  104.    @Override
  105.    public void onClick(View v) {
  106.     mp.stop();
  107.     Complete = true;
  108.    }
  109.   });
  110.  
  111.   // for Volume SeekBar--------------------------------
  112.   SeekBar volumeCtrl = (SeekBar) findViewById(R.id.volSeekBar);
  113.   audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  114.   int maxVolume = audioManager
  115.     .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  116.   int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
  117.  
  118.   volumeCtrl.setMax(maxVolume);
  119.   volumeCtrl.setProgress(curVolume);
  120.   volumeCtrl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  121.  
  122.      public void onStopTrackingTouch(SeekBar seekBar) {
  123.      }
  124.  
  125.      public void onStartTrackingTouch(SeekBar seekBar) {
  126.      }
  127.  
  128.      public void onProgressChanged(SeekBar seekBar,
  129.        int progress, boolean fromUser) {
  130.       audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  131.         progress, 0);
  132.      }
  133.     });
  134.  
  135.   mediabar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  136.  
  137.    @Override
  138.    public void onStopTrackingTouch(SeekBar seekBar) {
  139.    }
  140.  
  141.    @Override
  142.    public void onStartTrackingTouch(SeekBar seekBar) {
  143.    }
  144.  
  145.    @Override
  146.    public void onProgressChanged(SeekBar seekBar, int progress,
  147.      boolean fromUser) {
  148.     if (fromUser) {
  149.      mp.seekTo(progress);
  150.     }
  151.    }
  152.   });
  153.  
  154.  }
  155.  
  156.  @Override
  157.  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  158.   // TODO Auto-generated method stub
  159.   super.onActivityResult(requestCode, resultCode, data);
  160.   if (requestCode == MediaFetchResultCode) {
  161.    if (resultCode == RESULT_OK) {
  162.     songUrl = data.getStringExtra("fileChosen");
  163.     File file = new File(songUrl);    
  164.     fileSelected.setText(file.getName());
  165.     Toast.makeText(getApplicationContext(),
  166.       "U have selected:" + file.getName(), Toast.LENGTH_LONG).show();
  167.    }
  168.   }
  169.  }
  170.  
  171.  @Override
  172.  public void run() {
  173.   // TODO Auto-generated method stub
  174.   try {
  175.    while (Complete == false) {
  176.     int currentPosition = mp.getCurrentPosition();
  177.     mediabar.setProgress(currentPosition);
  178.    }
  179.   } catch (Exception e) {
  180.    // TODO Auto-generated catch block
  181.    e.printStackTrace();
  182.   }
  183.  }
  184.  
  185.  @Override
  186.  protected void onDestroy() {
  187.   super.onDestroy();
  188.   mp.stop();
  189.   Complete=true;
  190.  }
  191.  
  192.  @Override
  193.  protected void onPause() {
  194.   super.onPause();
  195.   mp.stop();
  196.   Complete=true;
  197.  }
  198.  
  199.  @Override
  200.  protected void onRestart() {
  201.   super.onRestart();  
  202.  }
  203.  
  204.  @Override
  205.  protected void onResume() {
  206.   super.onResume();  
  207.  }
  208.  
  209.  @Override
  210.  protected void onStart() {
  211.   super.onStart();
  212.  }
  213.  
  214.  @Override
  215.  protected void onStop() {
  216.   super.onStop();
  217.   mp.stop();
  218.   Complete=true;
  219.  }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement