Andriod audio streaming code from News's blog

Just click this ads  And get free 10 Blogs per day!   Hurry Up "Limited period offer"    For Advertisement Call +91 8939007002 package com.ssaurel.audiostreaming;
import android.app.ProgressDialog; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button;
public class MainActivity 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_main); 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("https://www.ssaurel.com/tmp/mymusic.mp3"); } else { if (!mediaPlayer.isPlaying()) mediaPlayer.start(); }
playPause = true;
} else { btn.setText("Launch Streaming");
if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); }
playPause = false; } } }); }
@Override protected void onPause() { super.onPause();
if (mediaPlayer != null) { mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null; } }
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(); } } }


MediaPlayermp =newMediaPlayer();mp.setDataSource(URL_OF_FILE);mp.prepare();mp.start();

Share:
Previous post     
Just click this ads  And get free 10 Blogs per day!   For Advertisement Call +91 8939007002
     Blog home

The Wall

No comments
You need to sign in to comment
Just click this ads  And get free 10 Blogs per day!  

Post

By News
Added Sep 14

Rate

Your rate:
Total: (0 rates)

Archives