Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import com.kant.MangoPlayer.R;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- * @author kantsh
- * This code can be used for development purposes and is free to use.
- * Completed on Tuesday,May 01,2012
- * mailto "kant.shashi@hotmail.com"
- */
- public class MangoMediaPlayerActivity extends Activity implements Runnable {
- /* Called when the activity is first created. */
- MangoMediaPlayerActivity mangoplayer;
- Thread currentThread;
- private String songUrl;
- private int MediaFetchResultCode = 11;
- private TextView fileSelected;
- private Button playIt;
- private Button stopIt;
- private MediaPlayer mp;
- private AudioManager audioManager;
- private SeekBar mediabar;
- private int total;
- private boolean Complete;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- fileSelected = (TextView) findViewById(R.id.autoCompleteTextViewer);
- Button browser = (Button) findViewById(R.id.bSelectFile);
- playIt = (Button) findViewById(R.id.bPlay);
- stopIt = (Button) findViewById(R.id.bstop);
- mediabar = (SeekBar) findViewById(R.id.mediaSeekBar);
- mangoplayer = this;
- mp = new MediaPlayer();
- mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- Complete = true;
- }
- });
- browser.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // intent name ="com.kant.MangoPlayer.FileExplore"
- Intent intent = new Intent("com.kant.MangoPlayer.FileExplore");
- startActivityForResult(intent, MediaFetchResultCode);
- }
- });
- playIt.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- Complete = true; // if u browse while playing then thread must be stopped
- mp.reset();
- mp.setDataSource(songUrl);
- mp.prepare();
- total = mp.getDuration(); // total duration of the song
- mediabar.setProgress(0);
- mediabar.setMax(total);
- Complete = false;
- currentThread = new Thread(mangoplayer);
- currentThread.start(); // start the thread to start listening
- mp.start(); // now start the media player
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- stopIt.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mp.stop();
- Complete = true;
- }
- });
- // for Volume SeekBar--------------------------------
- SeekBar volumeCtrl = (SeekBar) findViewById(R.id.volSeekBar);
- audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- int maxVolume = audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
- int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
- volumeCtrl.setMax(maxVolume);
- volumeCtrl.setProgress(curVolume);
- volumeCtrl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- public void onProgressChanged(SeekBar seekBar,
- int progress, boolean fromUser) {
- audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- }
- });
- mediabar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- if (fromUser) {
- mp.seekTo(progress);
- }
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == MediaFetchResultCode) {
- if (resultCode == RESULT_OK) {
- songUrl = data.getStringExtra("fileChosen");
- File file = new File(songUrl);
- fileSelected.setText(file.getName());
- Toast.makeText(getApplicationContext(),
- "U have selected:" + file.getName(), Toast.LENGTH_LONG).show();
- }
- }
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- while (Complete == false) {
- int currentPosition = mp.getCurrentPosition();
- mediabar.setProgress(currentPosition);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- mp.stop();
- Complete=true;
- }
- @Override
- protected void onPause() {
- super.onPause();
- mp.stop();
- Complete=true;
- }
- @Override
- protected void onRestart() {
- super.onRestart();
- }
- @Override
- protected void onResume() {
- super.onResume();
- }
- @Override
- protected void onStart() {
- super.onStart();
- }
- @Override
- protected void onStop() {
- super.onStop();
- mp.stop();
- Complete=true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement