I am developing one application where i want to play live audio radio. I have an url using which i will stream the audio. I have two urls,
http://54.36.166.45:9308/;__idm_id__=1010706433
http://54.36.166.45:9306/;__idm_id__=1010706433
The first one work properly, but the second one doesn't work, though both of these urls works fine in browser.
Here is my code:
public class ZTest1 extends AppCompatActivity {
private Button btn;
private boolean playPause;
private MediaPlayer mediaPlayer;
private ProgressDialog progressDialog;
private boolean initialStage = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ztest1);
btn = (Button) findViewById(R.id.audioStreamBtn);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
progressDialog = new ProgressDialog(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!playPause) {
btn.setText("Pause Streaming");
if (initialStage) {
new Player().execute("http://54.36.166.45:9306/;__idm_id__=1010706433");
} else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
btn.setText("Launch Streaming");
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
playPause = false;
}
}
});
}
class Player extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
Boolean prepared = false;
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
initialStage = true;
playPause = false;
btn.setText("Launch Streaming");
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
prepared = true;
} catch (Exception e) {
Log.e("MyAudioStreamingApp", e.getMessage());
prepared = false;
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (progressDialog.isShowing()) {
progressDialog.cancel();
}
mediaPlayer.start();
initialStage = false;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Buffering...");
progressDialog.show();
}
}
}
I added both "RECORD_AUDIO" and "MODIFY_AUDIO_SETTINGS" permissions.
Can anyone help me??
i use audioPlayer in a Dilaog
class AudioPlayerDialog(context: Context, audioLink: String) : Dialog(context) {
private var mContext = context
private var play = false
private var mediaPlayer: MediaPlayer? = null
private var total: Int = 0
var view: View? = null
private val link = audioLink
init {
setCanceledOnTouchOutside(true)
window.setBackgroundDrawable(
ColorDrawable(Color.TRANSPARENT))
val params = AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
val inflater = this.layoutInflater;
view = inflater.inflate(R.layout.player_dialog, null)
params.width = 700
params.height = 680
window.attributes.windowAnimations = R.style.PauseDialogAnimation;
setContentView(view, params)
view!!.visibility = View.VISIBLE
try {
init()
} catch (e: Exception) {
e.printStackTrace()
}
initContent()
StaticDataClassJava.scaleViewAndChildren(view!!, StaticDataClassJava.scaleFactor)
}
private fun initContent() {
playBtn!!.setOnClickListener {
if (!play) {
playBtn.setBackgroundResource(R.drawable.pause);
play()
} else {
playBtn.setBackgroundResource(R.drawable.play);
mediaStop()
}
}
leaveBtn.setOnClickListener {
mediaStop()
dismiss()
}
}
@Throws(IOException::class)
private fun init() {
try {
mediaPlayer = MediaPlayer()
val streamPath = link//enter path
mediaPlayer!!.setDataSource(streamPath)
mediaPlayer!!.prepare()
total = mediaPlayer!!.duration
musicSeekBar.max = total
play()
getSeekBarStatus()
} catch (ex: MalformedURLException) {
throw RuntimeException("Wrong url for mediaplayer! $ex")
} catch (ex: IllegalStateException) {
} catch (ex: IllegalArgumentException) {
throw RuntimeException("Wrong url for mediaplayer! $ex")
}
}
private fun play() {
mediaPlayer!!.start()
play = true
}
private fun mediaStop() {
mediaPlayer!!.pause()
play = false
}
private fun getSeekBarStatus() {
Thread(Runnable {
var currentPosition = 0
while (mediaPlayer != null && currentPosition < total) {
try {
Thread.sleep(1000)
currentPosition = mediaPlayer!!.currentPosition
} catch (e: InterruptedException) {
e.printStackTrace()
}
musicSeekBar!!.progress = currentPosition
}
}).start()
musicSeekBar!!.setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
internal var progress = 0
override fun onProgressChanged(seekBar: SeekBar, ProgressValue: Int,
fromUser: Boolean) {
if (fromUser) {
mediaPlayer!!.seekTo(ProgressValue)//if user drags the seekbar, it
gets the position and updates in textView.
}
val mMinutes = (ProgressValue / 1000 / 60).toLong()//converting into
minutes
val mSeconds = ProgressValue / 1000 % 60//converting into seconds
// SongProgress.setText(mMinutes + ":" + mSeconds);
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
}
})
}
}
and this when i call the dialog
val dialog = AudioPlayerDialog(this@OrderDetailScreenActivity, audioLink)
dialog.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.