본문 바로가기

컴퓨터/안드로이드

안드로이드 - MP3 재생

안드로이드 - MP3 재생 하기 예제

 

1. 리소스 안의 mp3 재생

 - getResources().getIdentifier()로 경로를 알아온다.

tmpID = getApplicationContext().getResources().getIdentifier( "sound", "raw", "aa.bb.man");

Context con = getApplicationContext();

 

mp = MediaPlayer.create(con, tmpID); 

 

mp.start();

 

2. sdcard안의 mp3재생

 - setDataSource()를 이용해서 음원의 경로를 설정

 

String songPath = "/sdcard/man.mp3";

mp.setDataSource(songPath);

 

주요 메서드 :

     1.prepare() - 재생을 준비

     2.start() - 재생을 시작

     3.stop() - 재생 중단

 

private MediaPlayer mp;

 

public void TestMediaPlayer(){

    if( mp == null ){

        mp = new MediaPlayer();

}

 

try {

        // raw 리소스에 있는 mp3를 재생할 때

        /*

         tmpID = getApplicationContext().getResources().getIdentifier( "sound", "raw", "aa.bb.man");

        Context con = getApplicationContext();

        mp = MediaPlayer.create(con, tmpID); 

        mp.start();   

        */

 

        // sdcard안에 있는 mp3를 재생할 때

        /*

         String songPath = "/sdcard/man.mp3";

         mp.setDataSource(songPath);

         mp.prepare();

         mp.start();

       */

       

} catch (Exception e) {

        Log.d("ERROR", e.getMessage());

}      


}