score:65
So I found this sample and it works for me, here it is if you have the same issue:
in myMain.java
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class myMain extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://usa8-vn.mixstream.net:8138");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
in the XML (main.xml) code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Source: (Radio La Chevere)"
android:layout_marginTop="10dip" android:gravity="center" />
<ProgressBar android:id="@+id/progressBar1"
android:indeterminateOnly="false" android:progressDrawable="@android:drawable/progress_horizontal"
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
android:minHeight="20dip" android:maxHeight="20dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_marginTop="10dip"></ProgressBar>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_marginTop="20dip" android:gravity="center">
<Button android:text="Play" android:id="@+id/buttonPlay"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Stop" android:id="@+id/buttonStopPlay"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
and the android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package.your.RadioStream"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".myMain"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
score:1
In onResume or wrvr you want! Paste this code.
String url = "http://server2.crearradio.com:8371"; // your URL here
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare(); // might take long! (for buffering, etc)
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
In the manifest.xml add the Internet permission.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
score:4
Well, if you expect a progress bar, you get disapointing, this is a stream, which by default has no time to end is endless. About the URL you can use the mount point in shoutcast2/icecast server and the / in the shoutcast1.
Source: stackoverflow.com
Related Query
- Online radio streaming app for Android
- Typical .gitignore file for an Android app
- Phonegap - add a splash screen for Android app
- Can you change min. API level for your Android app in Eclipse?
- How to sign an android app for the market?
- why this error in my PhoneGap app for Android ?
- Eclipse to Android show app icons for each class when deployed
- Making a restart button for an Android app game
- Easy way to build Android system app ? (Eclipse for example)
- IntelliJ/Eclipse not respecting minimum SDK version for Android app
- A lot of error when create new project in eclipse for Android App
- Android - sharedpreferences for radio button
- Running Android app from Eclipse after setting up for obfuscation
- How to automatically logout the Android app if it is not touched(inactive) for 30 mins?
- Using Eclipse for Android App: App won't install freshly because it's already on the device
- What does it mean that Android Studio is the "official" IDE for Android app development?
- Is it possible to run java (jdk-windows) , Eclipse (indigo) and Android-sdk FOR android app development from external flash drive
- List view for images - android mobile app
- How do I create custom GUI controls in Eclipse for use in Android app layouts?
- How to get logcat for an Android app on BlackBerry 10 devices
- How to set up an environment for android app development?
- Cant able to run Android app and error: Error retrieving parent for item error
- Using the same android app as a base for a new one
- First app for android after eclipse install
- Android service, alarmmanager, check online for updates, status bar notification
- Can't install Google App Engine tools for Android
- Building workspace has encountered a prob in eclipse for Android app
- Developing mobile app for android
- How to remove app for Multiple Users Android
- Building workspace creating a prob in eclipse for android app
More Query from same tag
- In Eclipse / STS / maven, where do I define the deployed servlet name?
- Tomcat v7.0 Server setup for ports 8086 and 8009
- Looking for up-to-date eclipse plugin for C#
- build error after importing maven projects
- Eclipse & Tomcat - Class loading
- Android Programing with OpenCV - App is crashing
- What JSON editor am I using in Eclipse?
- Want option back to choose between running app in device or emulator
- Command line arguments not getting counted
- How do I use Glass fish server with eclipse luna for Java EE?
- Media Player problems on click stop
- Generated a Maven Web Application Structure but cant import this into Eclipse IDE .
- LayoutLib is too recent. Update your tool?
- Eclipse maven - product not installing my plugin
- how can i debugg an android service - activity that extends service class -
- Eclipse plugin menu item is not visible
- PyDev messing encoding settings when running Python script from inside Eclipse
- wait( ) function not running properly
- Eclipse - ButtonCLick --> new screen with text
- Eclipse cannot find my android 4.1.2 device
- Suppose there are 2 classes A and B, is it possible to create an object of A in B and of B in A?
- Transform coordinates from EPSG:4326 to EPSG:31467 in Java /GWT server side
- Getting path from exported jar not working
- Change mirror of update sites of eclipse plugins
- Service builder is unable to delete its jar file while building
- Is there a tool for the conversion of duplicate literals into final Strings/magic numbers in Java?
- Is there an Eclipse equivalent to VS Studio "Go to All" (CTRL + T)
- Unusual Syntax Error in PyGame using Eclipse
- Why will nothing paint on my screen?
- Error in POM.xml