我正在尝试构建一个可以从网络流式传输mp3内容的应用。当我运行此代码时,我没有得到任何错误,但没有播放音频。有帮助吗?感谢
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
try {
mediaPlayer.setDataSource("my link.mp3"); //
mediaPlayer.prepareAsync();
//mediaPlayer.prepare(); //
if(! mediaPlayer.isPlaying())
{
mediaPlayer.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void initView() {
mediaPlayer = new MediaPlayer();
}
}
答案 0 :(得分:0)
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
try {
mediaPlayer.setDataSource("my link.mp3"); //
// mediaPlayer.prepareAsync();
mediaPlayer.prepare();
//如果您想要异步操作,则此方法会阻止UI,然后使用mediaPlayer.prepareAsync();
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
试试这个:
ProgressDialog progressDialog = ProgressDialog
.show(this, "Loading ", "Please Wait...");
progressDialog.setCancelable(false);
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.setDataSource("http://my link.mp3");
player.setOnErrorListener(this);
player.setOnPreparedListener(this);
player.prepareAsync();
} catch (IllegalArgumentException e) {
Toast.makeText(YourActivity.this,
"Internet Access is not Provided", 0).show();
e.printStackTrace();
} catch (IllegalStateException e) {
Toast.makeText(YourActivity.this,
"Internet Access is not Provided", 0).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(YourActivity.this,
"Internet Access is not Provided", 0).show();
e.printStackTrace();
}
@Override
public void onPrepared(MediaPlayer play) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
play.start();
}
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (player != null) {
player.release();
player = null;
}
return false;
}