我想了解Android媒体播放器。我设计了一个媒体播放器,当我在我的平板电脑上测试时,当我选择播放按钮时,它将获取网址和播放歌曲,如果我选择不播放的按钮或发出错误,则不会发生崩溃
我有一个问题,在研究时我是否需要身份验证,因为我正在将url链接到服务器
任何人都可以帮助我
我已经发布了我甚至使用过logcat详细信息的代码。 logcat将告诉GPU所以我在我的平板电脑上测试了,但我仍然无法收听mp3文件
public class MainActivity extends Activity
{
static final String AUDIO_PATH = "url";
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void doClick(View view) {
switch(view.getId()) {
case R.id.play:
try {
playAudio(AUDIO_PATH);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.stop:
if(mediaPlayer != null) {
mediaPlayer.stop();
}
break;
}
}
private void playAudio(String url) throws Exception
{
killMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
killMediaPlayer();
}
private void killMediaPlayer() {
if(mediaPlayer!=null) {
try {
mediaPlayer.release();
}
catch(Exception e) {
e.printStackTrace();
}
}
}}
main.xml文件如下所示
<EditText
android:id="@+id/EditTextSongURL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.54"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/ButtonTestPlayPause1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play" />
<SeekBar
android:id="@+id/SeekBarTestPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
在logcat中没有错误,因为不包括Gpu
答案 0 :(得分:1)
试试这个:
MediaPlayer mp=new MediaPlayer();
try {
mp.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
}
答案 1 :(得分:1)
MediaPlayer mediaPlayer = new MediaPlayer();
mp.setDataSource(context, Uri.parse("http://www.domian.com/ola.mp3"));
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// Display error
return false;
}
});