媒体播放器在模拟器或真实设备上运行时没有任何错误,但是当我点击开始按钮时,媒体不会启动或发出任何声音。我已尝试从else
方法移除PreparePlayer
,但它确实有效,但需要很长时间才能打开。
SeekBar seekBar1;
System.Timers.Timer t;
t = new System.Timers.Timer();
t.Interval = 3000;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
string filePath = "http://server10.mp3quran.net/ajm/128/001.mp3";
PreparePlayer(filePath);
Button button = FindViewById<Button>(Resource.Id.bustart);
button.Click += delegate
{
if (!player.IsPlaying)
{
button.SetBackgroundResource(Resource.Drawable.pauseicon);
player.Start();
t.Start();
}
else
{
button.SetBackgroundResource(Resource.Drawable.starticonn);
player.Pause();
t.Stop();
}
};
protected MediaPlayer player;
public void PreparePlayer(String filePath)
{
if (player == null)
{
player = new MediaPlayer();
}
else
{
player.Reset();
player.SetDataSource(filePath);
player.Prepare();
seekBar1.Max = player.Duration;
}
}
protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
seekBar1.Progress = player.CurrentPosition;
}
答案 0 :(得分:0)
您应该使用流媒体功能。
String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
这不是FilePath。 https://developer.android.com/guide/topics/media/mediaplayer.html