Powered by Blogger.

Followers

Friday, 12 July 2013

Stream Online mp3 on Android

Posted by Sanjeet A On 12:11 No comments
package com.thenewtechnoworld.radiosmart;

import java.io.IOException;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private static String mp3_url = "http://192.168.252.1/Android/Mp3/YeResmiZulfein.mp3";
                                 //address of mp3 file on
Button play, stop;
MediaPlayer mp = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.bPlay);
stop = (Button) findViewById(R.id.bStop);
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try {
if (!mp.isPlaying()) {

mp.setDataSource(mp3_url);
mp.prepare();
mp.start();

} else {

mp.prepare();
mp.stop();
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

try {

if (mp.isPlaying()) {
mp.prepare();
mp.stop();
Log.d("Stop Pressed", "yes");
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


Xml file is-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Radio Smart" />

    <Button
        android:id="@+id/bPlay"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Paly" />

    <Button
        android:id="@+id/bStop"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Stop" />

</LinearLayout>

0 comments:

Post a Comment

Site search