음악 재생이 되게끔 앱개발 하려면
아래 경로를 어떻게 해야하나요
public class MyMusic extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.playButton);
stopButton = (Button) findViewById(R.id.stopButton);
progressbar = (ProgressBar) findViewById(R.id.ProgressBar01);
progressbar.setVisibility(8); //GONE
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressbar.setVisibility(0); //VISIBLE
play(); } });
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressbar.setVisibility(8); //GONE
stop(); } }); }
@Override
protected void onPause() {
super.onPause();
progressbar.setVisibility(8); //GONE
stop(); }
public void play( ) {
stop();
audio_play = new MediaPlayer();
try{
audio_play.setDataSource("C:\Documents and Settings\Android\My Documents\Apps\music.mp3"); // mp3파일 경로
audio_play.prepare(); // 준비
audio_play.setLooping(false); // 반복재생 false
audio_play.start(); // 시작
}catch(IOException e){
Toast.makeText(MyMusic.this, "Error!!",
Toast.LENGTH_SHORT).show(); } }
public void stop() {
if (audio_play != null) {
audio_play.stop(); // 중지
audio_play.release(); // 자원 반환
audio_play = null; } } }
음... 리소스 폴더 있잖아요. res 폴더.. drawable-hdpi 있고 그런데요.. 거기다가 raw 라는 폴더를 생성하신후
mp3 파일을 넣으신담에, 경로를 R.raw.sample_1 이렇게 넣어 주심 되지 않나요?
저는 좀 다른식으로 쓰긴 했지만요...
저 같은 경우에는 근접 경보시 미디어 재생을 하게 되는데,
아래와 같이 했어요.
저도 초보라 확답은 못드리겠지만, 저는 이렇게 했더니 되더라구요^^
public void onCreate() {
//통지 관리자 구하기 (4)
nm=(NotificationManager)getSystemService(
Context.NOTIFICATION_SERVICE);
//플레이어 생성
try {
player=MediaPlayer.create(this,R.raw.sample_01);
player.prepare();
} catch (Exception e) {
}
}
//초기화
@Override
public void onStart(Intent intent,int startID) {
//통지 표시
showNotification(this,R.drawable.icon,
"BGM을 재생합니다 .",
"플레이어 서비스",
"플레이어 서비스를 시작했습니다 .");
//음악 재생
try {
player.seekTo(0);
player.start();
} catch (Exception e) {
}
}
//마무리 처리
@Override
public void onDestroy() {
//통지 취소 (6)
nm.cancel(0);
//음악의 정지 및 해제
player.stop();
player.release();
//토스트 표시
showToast(this,"플레이어가 정지되었습니다 .");
}
//바인드
@Override
public IBinder onBind(Intent intent) {
return null;
}
//통지의 표시
private void showNotification(Context context,
int iconID,String ticker,String title,String message) {
//통지 객체의 생성 (5)
Notification notification=new Notification(iconID,
ticker,System.currentTimeMillis());
PendingIntent intent=PendingIntent.getActivity(context,0,
new Intent(context, GPSActivity_01.class),0);
notification.setLatestEventInfo(context,
title,message,intent);
//통지의 취소 (6)
nm.cancel(0);
//통지의 표시 (7)
nm.notify(0,notification);
}
식으로 했어요