반응형
How to Stream Play online audio mp3 from URL in android without downloading.
Code for Please add internet permission to your AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
Code for MainActivity.java file.
package com.playonlineaudiomp3_android_examples.com; import java.io.IOException; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button buttonStop,buttonStart ; String AudioURL = "http://www.android-examples.com/wp-content/uploads/2016/04/Thunder-rumble.mp3"; MediaPlayer mediaplayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonStart = (Button)findViewById(R.id.button1); buttonStop = (Button)findViewById(R.id.button2); mediaplayer = new MediaPlayer(); mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC); buttonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { mediaplayer.setDataSource(AudioURL); mediaplayer.prepare(); } 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(); } mediaplayer.start(); } }); buttonStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mediaplayer.stop(); } }); } }
Code for activity_main.xml layout file.
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.playonlineaudiomp3_android_examples.com.MainActivity" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:text="Stop Music From URL" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="157dp" android:text="Start Music From URL" /> </RelativeLayout>
반응형
'MOBILE' 카테고리의 다른 글
모바일 디바이스에서 화면 가로세로 변경 시 화면 크기 고정 (0) | 2023.09.26 |
---|---|
android permission 정리 (0) | 2023.09.26 |
안드로이드에서 비디오 재생하기: MediaPlayer (0) | 2016.11.07 |
[ANDROID(안드로이드) 앱 개발 기초] MEDIAPLAYER 음악 재생하기 (0) | 2016.11.07 |
Android VideoView error 처리하기 (0) | 2016.11.04 |
안드로이드: 서비스 Service 예제 (0) | 2016.11.04 |
[Android]MediaPlayer에서 사운드를 재생하는 두 가지 경로 (0) | 2016.11.04 |
[안드로이드] 미디어플레이어 에러 대처하기 (-38, -19) (0) | 2016.11.04 |
댓글