URL 상의 mp3 스트리밍
Filayout : testaudiofromurl.xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/widget31" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <EditText android:id="@+id/EditTextSongURL" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:ems="10" android:height="100dp" android:lines="3" android:maxLines="3" android:minLines="1" > <requestFocus /> <requestFocus /> </EditText> <ImageButton android:id="@+id/ButtonTestPlayPause" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/EditTextSongURL" android:layout_centerHorizontal="true" android:contentDescription="TestPlayPause" android:onClick="onClick"/> <SeekBar android:id="@+id/SeekBarTestPlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/ButtonTestPlayPause" /> </RelativeLayout>//Code ::Mainactivity.java
package com.example.androidtest2; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnBufferingUpdateListener; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.os.Handler; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.SeekBar; public class testaudiofromurl extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{ private ImageButton buttonPlayPause; private SeekBar seekBarProgress; public EditText editTextSongURL; private MediaPlayer mediaPlayer; private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class private final Handler handler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testaudiofromurl); initView(); } /** This method initialise all the views in project*/ private void initView() { buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause); buttonPlayPause.setOnClickListener(this); seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay); seekBarProgress.setMax(99); // It means 100% .0-99 seekBarProgress.setOnTouchListener(this); editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL); editTextSongURL.setText("http://tiennamdinh.freevnn.com/mp3voa/nguyenvanthanh.mp3"); mediaPlayer = new MediaPlayer(); mediaPlayer.setOnBufferingUpdateListener(this); mediaPlayer.setOnCompletionListener(this); } /** Method which updates the SeekBar primary progress by current song playing position*/ private void primarySeekBarProgressUpdater() { seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length" if (mediaPlayer.isPlaying()) { Runnable notification = new Runnable() { public void run() { primarySeekBarProgressUpdater(); } }; handler.postDelayed(notification,1000); } } @Override public void onClick(View v) { if(v.getId() == R.id.ButtonTestPlayPause){ /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */ try { mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. } catch (Exception e) { e.printStackTrace(); } mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL if(!mediaPlayer.isPlaying()){ mediaPlayer.start(); buttonPlayPause.setImageResource(R.drawable.ic_launcher); }else { mediaPlayer.pause(); buttonPlayPause.setImageResource(R.drawable.ic_launcher); } primarySeekBarProgressUpdater(); } } @Override public boolean onTouch(View v, MotionEvent event) { if(v.getId() == R.id.SeekBarTestPlay){ /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/ if(mediaPlayer.isPlaying()){ SeekBar sb = (SeekBar)v; int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress(); mediaPlayer.seekTo(playPositionInMillisecconds); } } return false; } @Override public void onCompletion(MediaPlayer mp) { /** MediaPlayer onCompletion event handler. Method which calls then song playing is complete*/ buttonPlayPause.setImageResource(R.drawable.ic_launcher); } @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { /** Method which updates the SeekBar secondary progress by current song loading from URL position*/ seekBarProgress.setSecondaryProgress(percent); } }
[출처] http://www.droidcourse.com/2014/07/android-play-audio-file-from-url.html
'프로그래밍 > Android (Java)' 카테고리의 다른 글
이미지 버튼에 이미지 크기 맞추기 (0) | 2018.06.07 |
---|---|
웹뷰 (webview) 의 현재 URL 확인하기 (0) | 2018.06.07 |
EditText 에 focus 주고 (커서 이동) 소프트 키보드 자동 띄우기 (0) | 2018.04.11 |
Layout 에 관한 모든것 (0) | 2018.04.10 |
EditText 입력 후 Button 누르면 Toast 로 출력시키는 예제 (0) | 2018.04.10 |