In this tutorial we learn that how play sound file from internet in android.
Your activity_main.xml file is like below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play Sound" /> </LinearLayout>
In the above line code pass url of sound file where sound file is available on internet.
Your MainActivity.java file is like this.
Click below link for more update about android
public class MainActivity extends Activity { private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); // add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String url = "http://........"; // your URL here MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(url); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); mediaPlayer.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { // You can show error message to user return false; } }); } }); } }
Click below link for more update about android
No comments:
Post a Comment