我正在尝试播放此流中的音频(请注意,这不是文件)https://listen.radioaktywne.pl:8443/ramp3,很遗憾,没有运气。将此链接复制粘贴到我的Web浏览器时,它通常会播放在线广播。
我尝试将setAudioStreamType()
和AudioAttributes
弄乱,但以上似乎都不起作用。另外,当我尝试使用指向简单mp3文件的URL时,一切正常。有什么建议吗?
这是整个MainActivity.java
文件,关键部分只是initializeMediaPlayer()
。
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
// The audio url to play
String audioUrl = "https://listen.radioaktywne.pl:8443/ramp3";
private FloatingActionButton fab;
private boolean playing=false;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initializeMediaPlayer();
fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, R.string.now_listening, Snackbar.LENGTH_LONG).show();
if (!playing) {
startPlaying();
} else {
stopPlaying();
}
}
});
}
private void stopPlaying() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
initializeMediaPlayer();
playing=false;
}
}
private void startPlaying() {
mediaPlayer.prepareAsync();
playing =true;
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}
/**
* Take a look here!
*/
private void initializeMediaPlayer() {
mediaPlayer = new MediaPlayer();
// mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// AudioAttributes audioAttributes = new AudioAttributes.Builder()
// .setUsage(AudioAttributes.USAGE_MEDIA)
// .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
// .build();
//
// mediaPlayer.setAudioAttributes(audioAttributes);
try {
mediaPlayer.setDataSource(audioUrl);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}