Java 在android中从url播放mp3文件
我在做什么:我正在尝试从android的url播放mp3文件。并通过progressbar控制其位置 正在发生的事情: 因为下面的strings.xml代码工作正常,所以正在播放歌曲,我可以使用progressbar定位进度Java 在android中从url播放mp3文件,java,android,Java,Android,我在做什么:我正在尝试从android的url播放mp3文件。并通过progressbar控制其位置 正在发生的事情: 因为下面的strings.xml代码工作正常,所以正在播放歌曲,我可以使用progressbar定位进度 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Android mp3 player</string> <s
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android mp3 player</string>
<string name="testsong_20_sec">http://users.skynet.be/fa046054/home/P22/track06.mp3</string>
我在寻找什么:如何确保我的代码播放正确的音乐,我可以切换使用进度条在第二个网址正确
StreamingMp3Player.java
main.xml
这是因为GoogleDriveHTTP服务器没有返回正确长度的文件 $wget-Shttps://286898c83aa5e6a4f7b29cdbef3b2f54430b4e71.googledrive.com/host/0B6a-NfRKFX8HQVZRSzFON1ExZEE/Chadh%20Gayi%20Maa%20Tere%20Naam%20Ki%20Sherewali%20Ka%20Sancha.mp3 ... 长度:未指定[音频/mpeg]
您没有总流长度,因此您没有相对于总长度的位置。运气不好。@Sanny。。。。哦,是。。。那么,有没有办法用谷歌硬盘解决这个问题。。。。。。。。。。。。。。所以如果我需要流的长度。。。。我必须在我自己的域url中托管。。。。这将解决这个问题吗?如果您自己使用正确返回长度的服务器托管此服务器,它将解决您的问题。99%的流行服务器将为您提供服务。也许有一种方法可以让谷歌硬盘的返回长度变长,但我不确定。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android mp3 player</string>
<string name="testsong_20_sec">https://286898c83aa5e6a4f7b29cdbef3b2f54430b4e71.googledrive.com/host/0B6a-NfRKFX8HQVZRSzFON1ExZEE/Chadh%20Gayi%20Maa%20Tere%20Naam%20Ki%20Sherewali%20Ka%20Sancha.mp3</string>
</resources>
public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;
private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/EditTextSongURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:height="100dp"
android:lines="3"
android:maxLines="3"
android:minLines="1" >
<requestFocus />
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/ButtonTestPlayPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/EditTextSongURL"
android:layout_centerHorizontal="true"
android:contentDescription="TestPlayPause"
android:onClick="onClick"
android:src="@drawable/button_play" />
<SeekBar
android:id="@+id/SeekBarTestPlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ButtonTestPlayPause" />
</RelativeLayout>