음악파일 위치
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/startPlayerBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="오디오 재생 시작" /> <Button android:id="@+id/restartPlayerBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="재생 재개" /> <Button android:id="@+id/pausePlayerBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="재생 일시중지" /> </LinearLayout>
package com.proandroid; import android.app.Activity; import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SimpleAudio extends Activity { static final String AUDIO_PATH = "http://211.183.2.90/web/upload/the_boys.mp3"; private MediaPlayer mediaPlayer; private int playbackPosition=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button startPlayerBtn = (Button)findViewById(R.id.startPlayerBtn); Button pausePlayerBtn = (Button)findViewById(R.id.pausePlayerBtn); Button restartPlayerBtn = (Button)findViewById(R.id.restartPlayerBtn); startPlayerBtn.setOnClickListener(new OnClickListener(){ public void onClick(View view){ try{ playLocalAudio(); //playAudio(AUDIO_PATH); //playSdcardAudio(); //playLocalAudio_UsingDescriptor(); }catch(Exception e){ e.printStackTrace(); } } }); pausePlayerBtn.setOnClickListener(new OnClickListener(){ public void onClick(View view){ if(mediaPlayer!=null){ //음악을 일시 정지 시킬때 정지되기 직전의 position값 저장 playbackPosition = mediaPlayer.getCurrentPosition(); //일시정지 mediaPlayer.pause(); } } }); restartPlayerBtn.setOnClickListener(new OnClickListener(){ public void onClick(View view){ //MediaPlayer 객체가 존재하고 현재 실행중이 아닐때 if(mediaPlayer!=null && !mediaPlayer.isPlaying()){ //음악이 일시정지되기 직전의 position값으로 셋팅 mediaPlayer.seekTo(playbackPosition); mediaPlayer.start(); } } }); } private void playLocalAudio()throws Exception{ //어플리케이션에 내장되 있는 자원을 호출해서 MediaPlayer객체 생성 mediaPlayer=MediaPlayer.create(this, R.raw.the_boys); //MediaPlayer 객체가 가지고 있는 음악 정보를 start mediaPlayer.start(); } private void playAudio(String url)throws Exception{ killMediaPlayer(); mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(url); mediaPlayer.prepare(); mediaPlayer.start(); } private void playSdcardAudio()throws Exception{ mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource("/sdcard/twoneone.mp3"); mediaPlayer.prepare(); mediaPlayer.start(); } private void playLocalAudio_UsingDescriptor() throws Exception { AssetFileDescriptor fileDesc = getResources().openRawResourceFd(R.raw.twoneone); if(fileDesc!=null){ mediaPlayer=new MediaPlayer(); mediaPlayer.setDataSource( fileDesc.getFileDescriptor(), fileDesc.getStartOffset(), fileDesc.getLength()); mediaPlayer.prepare(); mediaPlayer.start(); fileDesc.close(); } } @Override protected void onDestroy(){ super.onDestroy(); killMediaPlayer(); } private void killMediaPlayer() { if(mediaPlayer!=null){ try{ //MediaPlayer 자원해제 mediaPlayer.release(); }catch(Exception e){ e.printStackTrace(); } } } }
'Android > 기본' 카테고리의 다른 글
Android surface(마우스에 이미지 따라다니기) (0) | 2012.04.28 |
---|---|
Android Video View (동영상 재생) (0) | 2012.04.28 |
Android Constants Provider (0) | 2012.04.28 |
Android FileSearch Gallery(사진선택) (0) | 2012.04.28 |
Android Animaiton 으로 효과 (2) (0) | 2012.04.28 |