1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package com.hrupin.mp3player;
import com.hrupin.mp3player.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
import android.media.AudioManager;
public class Mp3player extends Activity {
private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private final Handler handler = new Handler();
// Here i override onCreate method.
//
// setContentView() method set the layout that you will see then
// the application will starts
//
// initViews() method i create to init views components.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
initViews();
}
// This method set the setOnClickListener and method for it (buttonClick())
private void initViews() {
buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
buttonPlayStop.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {buttonClick();}});
mediaPlayer = MediaPlayer.create(this, Uri.parse("mms://stream.mosaiquefm.net/mosaique64k"));
seekBar = (SeekBar) findViewById(R.id.SeekBar01);
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false; }
});
}
public void startPlayProgressUpdater() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification,1000);
}else{
mediaPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
}
// This is event handler thumb moving event
private void seekChange(View v){
if(mediaPlayer.isPlaying()){
SeekBar sb = (SeekBar)v;
mediaPlayer.seekTo(sb.getProgress());
}
}
// This is event handler for buttonClick event
private void buttonClick(){
/*
mediaPlayer = new MediaPlayer();
String urlfichier = "url";
try {
mediaPlayer.setDataSource(urlfichier);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
/* if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
try{
mediaPlayer.start();
//startPlayProgressUpdater();
}catch (IllegalStateException e) {
mediaPlayer.pause();
}
}else {
buttonPlayStop.setText(getString(R.string.play_str));
mediaPlayer.pause();
}*/
} |
Partager