sumber : http://agusharyanto.net/wordpress/?p=1013&cpage=1#comment-3556
Mari kita mulai.
- Buat Project baru dengan nama “RadioStreaming”
- Sekarang mari kita siapkan layout user interfacenya, Edit activity_main.xml lalu ketikan kode berikut.
03 |
android:layout_width = "match_parent" |
04 |
android:layout_height = "match_parent" |
05 |
android:paddingBottom = "@dimen/activity_vertical_margin" |
06 |
android:paddingLeft = "@dimen/activity_horizontal_margin" |
07 |
android:paddingRight = "@dimen/activity_horizontal_margin" |
08 |
android:paddingTop = "@dimen/activity_vertical_margin" |
09 |
tools:context = ".MainActivity" > |
12 |
android:id = "@+id/textViewRadioUrl" |
13 |
android:layout_width = "wrap_content" |
14 |
android:layout_height = "wrap_content" |
15 |
android:layout_alignParentLeft = "true" |
16 |
android:layout_alignParentTop = "true" |
17 |
android:text = "<span class=" intexthighlight" style = "color: rgb(26, 117, 255); line-height: normal;" phasehl = "metakeyword:en-US" id = "tRJeP" >Radio</ span > Url" /> |
20 |
android:id = "@+id/buttonPlay" |
21 |
android:layout_width = "wrap_content" |
22 |
android:layout_height = "wrap_content" |
23 |
android:layout_alignLeft = "@+id/progressBar1" |
24 |
android:layout_below = "@+id/progressBar1" |
25 |
android:layout_marginTop = "14dp" |
26 |
android:text = "Play" /> |
29 |
android:id = "@+id/progressBar1" |
30 |
style = "?android:attr/progressBarStyleHorizontal" |
31 |
android:layout_width = "wrap_content" |
32 |
android:layout_height = "wrap_content" |
33 |
android:layout_alignLeft = "@+id/textViewRadioUrl" |
34 |
android:layout_alignParentRight = "true" |
35 |
android:layout_below = "@+id/textViewRadioUrl" |
36 |
android:layout_marginTop = "14dp" |
37 |
android:indeterminateDrawable = "@android:drawable/progress_indeterminate_horizontal" |
38 |
android:indeterminateOnly = "false" |
39 |
android:maxHeight = "20dip" |
40 |
android:minHeight = "20dip" |
41 |
android:progressDrawable = "@android:drawable/progress_horizontal" /> |
44 |
android:id = "@+id/buttonStop" |
45 |
android:layout_width = "wrap_content" |
46 |
android:layout_height = "wrap_content" |
47 |
android:layout_alignBaseline = "@+id/buttonPlay" |
48 |
android:layout_alignBottom = "@+id/buttonPlay" |
49 |
android:layout_toRightOf = "@+id/buttonPlay" |
50 |
android:text = "Stop" /> |
3. Sekarang kita melangkah ke otak dari aplikasi radio streaming ini, edit class MainActivity.java lalu ketikan kode berikut
001 |
package net.agusharyanto.radiostreaming; |
003 |
import java.io.IOException; |
005 |
import android.app.Activity; |
006 |
import android.media.MediaPlayer; |
007 |
import android.media.MediaPlayer.OnBufferingUpdateListener; |
008 |
import android.media.MediaPlayer.OnPreparedListener; |
009 |
import android.os.Bundle; |
010 |
import android.util.Log; |
011 |
import android.view.View; |
012 |
import android.view.View.OnClickListener; |
013 |
import android.widget.Button; |
014 |
import android.widget.ProgressBar; |
015 |
import android.widget.TextView; |
017 |
public class MainActivity extends Activity implements OnClickListener { |
020 |
private ProgressBar playSeekBar; |
022 |
private TextView tvRadioUrl; |
023 |
private Button buttonPlay; |
025 |
private Button buttonStopPlay; |
027 |
private MediaPlayer player; |
029 |
/** Called when the activity is first created. */ |
031 |
public void onCreate(Bundle savedInstanceState) { |
032 |
super .onCreate(savedInstanceState); |
033 |
setContentView(R.layout.activity_main); |
035 |
initializeUIElements(); |
037 |
initializeMediaPlayer(); |
040 |
private void initializeUIElements() { |
042 |
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1); |
043 |
playSeekBar.setMax( 100 ); |
044 |
playSeekBar.setVisibility(View.INVISIBLE); |
045 |
playSeekBar.setIndeterminate( true ); |
046 |
buttonPlay = (Button) findViewById(R.id.buttonPlay); |
047 |
buttonPlay.setOnClickListener( this ); |
049 |
buttonStopPlay = (Button) findViewById(R.id.buttonStop); |
050 |
buttonStopPlay.setEnabled( false ); |
051 |
buttonStopPlay.setOnClickListener( this ); |
052 |
tvRadioUrl = (TextView) findViewById(R.id.textViewRadioUrl); |
053 |
tvRadioUrl.setText( "Radio url : " +url_radio); |
056 |
public void onClick(View v) { |
057 |
if (v == buttonPlay) { |
059 |
} else if (v == buttonStopPlay) { |
064 |
private void startPlaying() { |
065 |
buttonStopPlay.setEnabled( true ); |
066 |
buttonPlay.setEnabled( false ); |
068 |
playSeekBar.setVisibility(View.VISIBLE); |
070 |
player.prepareAsync(); |
072 |
player.setOnPreparedListener( new OnPreparedListener() { |
074 |
public void onPrepared(MediaPlayer mp) { |
083 |
private void stopPlaying() { |
084 |
if (player.isPlaying()) { |
087 |
initializeMediaPlayer(); |
090 |
buttonPlay.setEnabled( true ); |
091 |
buttonStopPlay.setEnabled( false ); |
092 |
playSeekBar.setIndeterminate( true ); |
093 |
playSeekBar.setVisibility(View.INVISIBLE); |
097 |
private void initializeMediaPlayer() { |
098 |
player = new MediaPlayer(); |
100 |
player.setDataSource(url_radio); |
101 |
} catch (IllegalArgumentException e) { |
103 |
} catch (IllegalStateException e) { |
105 |
} catch (IOException e) { |
109 |
player.setOnBufferingUpdateListener( new OnBufferingUpdateListener() { |
111 |
public void onBufferingUpdate(MediaPlayer mp, int percent) { |
112 |
playSeekBar.setIndeterminate( false ); |
113 |
playSeekBar.setSecondaryProgress( 100 ); |
114 |
Log.i( "Buffering" , "" + percent); |
120 |
protected void onPause() { |
122 |
if (player.isPlaying()) { |
4. Jangan lupa karena aplikasi kita menggunakan jaringan internet maka kita harus menambahkan permission internet pada AndroidManifest.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
03 |
package = "net.agusharyanto.radiostreaming" |
04 |
android:versionCode = "1" |
05 |
android:versionName = "1.0" > |
08 |
android:minSdkVersion = "8" |
09 |
android:targetSdkVersion = "17" /> |
11 |
< uses-permission android:name = "android.permission.INTERNET" ></ uses-permission > |
14 |
android:allowBackup = "true" |
15 |
android:icon = "@drawable/ic_launcher" |
16 |
android:label = "@string/app_name" |
17 |
android:theme = "@style/AppTheme" > |
19 |
android:name = "net.agusharyanto.radiostreaming.RadioActivity" |
20 |
android:label = "@string/app_name" > |
22 |
< action android:name = "android.intent.action.MAIN" /> |
24 |
< category android:name = "android.intent.category.LAUNCHER" /> |
5. Saatnya menjalankan project RadioStreaming yang telah kita buat.
Klik Button Play tunggu beberapa saat dan dari handphone anda akan terdengar suara siaran Radio.
Nah Gampangkan buatnya, ayoo semangat. Yang lagi skripsi tingkatkan semangat dan usahanya, semoga bisa cepat selesai jangan ditunda-tunda lagi.
Untuk yang sudah sukses dengan tutorial ini bisa mencoba tantangannya dengan membuat aplikasi Radio streaming yang bisa memainkan lebih dari 1 station radio
Semoga Bermanfaat
Komentar Terbaru