私のアプリケーションには、URL から mp3 形式のデータをロードするラジオ ストリーミング機能が含まれています。次のコードに基づいてライブ ラジオを再生します。
public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;
private MediaPlayer mediaPlayer;
// this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
private int mediaFileLengthInMilliseconds;
private final Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
/** This method initialise all the views in project*/
private void initView() {
buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
buttonPlayPause.setOnClickListener(this);
seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);
seekBarProgress.setMax(99); // It means 100% .0-99
seekBarProgress.setOnTouchListener(this);
editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
editTextSongURL.setText(R.string.testsong_20_sec);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
/** Method which updates the SeekBar primary progress by current song playing position*/
private void primarySeekBarProgressUpdater() {
seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification,1000);
}
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.ButtonTestPlayPause){
/** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
try {
mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer.
} catch (Exception e) {
e.printStackTrace();
}
mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
buttonPlayPause.setImageResource(R.drawable.button_pause);
}else {
mediaPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.button_play);
}
primarySeekBarProgressUpdater();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getId() == R.id.SeekBarTestPlay){
/** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
if(mediaPlayer.isPlaying()){
SeekBar sb = (SeekBar)v;
int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
mediaPlayer.seekTo(playPositionInMillisecconds);
}
}
return false;
}
@Override
public void onCompletion(MediaPlayer mp) {
/** MediaPlayer onCompletion event handler. Method which calls then song playing is complete*/
buttonPlayPause.setImageResource(R.drawable.button_play);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
/** Method which updates the SeekBar secondary progress by current song loading from URL position*/
seekBarProgress.setSecondaryProgress(percent);
}
}
私の問題は、このコードが約 1 時間の長さの mp3 URL では機能しないことです。誰かが長いmp3 URLを再生する方法を知っていますか
回答1から更新してみました:
public class Mp3 extends Activity implements OnClickListener {
Button button;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
mediaPlayer = new MediaPlayer();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
startSound("http://str2.creacast.com:80/pha-michy1");
break;
}
}
public void startSound(final String URL) {
button.setText("STOP");
new Thread() {
public void run() {
try {
Uri uri = Uri.parse(URL);
mediaPlayer = MediaPlayer.create(Mp3.this, uri);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
button.setEnabled(true);
button.setText("PLAY");
}
});
} catch (Exception e) {
}
// myProgressDialog.dismiss();
}
}.start();
}
}
私のログはこのエラーを示しています。
05-24 09:09:02.652: I/MediaPlayer(1548): uri
05-24 09:09:02.652: I/MediaPlayer(1548): path is null
05-24 09:09:02.652: D/MediaPlayer(1548): Couldn't open file on client side, trying server side
05-24 09:09:16.707: E/MediaPlayer(1548): error (1, -2147483648)
05-24 09:09:16.707: D/MediaPlayer(1548): create failed:
05-24 09:09:16.707: D/MediaPlayer(1548): java.io.IOException: Prepare failed.: status=0x1
05-24 09:09:16.707: D/MediaPlayer(1548): at android.media.MediaPlayer.prepare(Native Method)
05-24 09:09:16.707: D/MediaPlayer(1548): at android.media.MediaPlayer.create(MediaPlayer.java:636)
05-24 09:09:16.707: D/MediaPlayer(1548): at android.media.MediaPlayer.create(MediaPlayer.java:613)
05-24 09:09:16.707: D/MediaPlayer(1548): at com.hrupin.streamingmedia.Mp3$1.run(Mp3.java:60)
strong text* update: 2 番目の URL を試すと、ログにこのエラーが記録されました。確認できますか。*強いテキスト
05-24 09:59:14.398: D/dalvikvm(2228): Debugger has detached; object registry had 1 entries 05-24 09:59:14.582: E/MediaPlayer(2228): start called in state 4 05-24 09:59:14.582: E/MediaPlayer(2228): error (-38, 0) 05-24 09:59:14.585: E/MediaPlayer(2228): Error (-38,0) 05-24 09:59:14.632: E/MediaPlayer(2228): error (1, -1002) 05-24 09:59:14.683: E/MediaPlayer(2228): Error (1,-1002) 05-24 10:00:07.843: I/MediaPlayer(2371): uri is:http://www.usa8-vn.mixstream.net:8138 05-24 10:00:07.843: I/MediaPlayer(2371): path is null 05-24 10:00:07.847: D/MediaPlayer(2371): Couldn't open file on client side, trying server side 05-24 10:00:07.863: E/MediaPlayer(2371): error (1, -1002) 05-24 10:00:07.863: D/MediaPlayer(2371): create failed: 05-24 10:00:07.863: D/MediaPlayer(2371): java.io.IOException: Prepare failed.: status=0x1 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.prepare(Native Method) 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.create(MediaPlayer.java:636) 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.create(MediaPlayer.java:613) 05-24 10:00:07.863: D/MediaPlayer(2371): at com.hrupin.streamingmedia.Mp3$1.run(Mp3.java:68) 05-24 10:00:38.300: I/MediaPlayer(2411): uri is:http://usa8-vn.mixstream.net:8138 05-24 10:00:38.300: I/MediaPlayer(2411): path is null 05-24 10:00:38.300: D/MediaPlayer(2411): Couldn't open file on client side, trying server side