Basic Script Streaming Radio Android Studio

Tech Antuzia - Hallo guys disini saya akan membagikan sebuah script untuk membuat sebuah class java dalam pembuatan sebuah aplikasi streaming radio, yang menggunakan software Android Studio,Eclipse atau yang lainnya




public class MainActivity extends Activity {

    private final static String stream = "YOUR_URL_STREAMING_RADIO";
    Button play;
    MediaPlayer mediaPlayer;
    boolean started = false;
    boolean prepared = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play = (Button) findViewById(R.id.play);
        play.setEnabled(false);
        play.setText("Loading..");
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (started) {
                    mediaPlayer.pause();
                    started = false;
                    play.setText("Play");
                } else {
                    mediaPlayer.start();
                    started = true;
                    play.setText("Pause");
                }

            }
        });

        new PlayTask().execute(stream);
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private class PlayTask extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... strings) {

            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            play.setEnabled(true);
            play.setText("Play");

        }
    }
}
Semoga bermanfaat terima-kasih

0 Response to "Basic Script Streaming Radio Android Studio "

Posting Komentar