1. 오디오 재생

  • 오디오는 스마트폰 사용자들이 즐겨 이용하는 매체 중의 하나이다.

1.1 주요 오디오 파일 유형

.3gp, .mp3, .mp4, .mid, .ogg, .wav

1.2 오디오 테스트 샘플

  • 이미지처럼 오디오도 같은 위치에 복사 res/drawable-ldpi/music.mp3

res/layout/main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/img_name">

    <!--  text 정의 -->
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/audio_title" />
    
    <!-- 오디오 실행을 위한 토글버튼  정의 -->
    <ToggleButton 
        android:id="@+id/btn_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="stop"
        android:textOff="listen"  />
 
</LinearLayout>



res/values/strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestAudio</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    
    <!-- text 명 정의 -->
    <string name="audio_title">audio test</string>
	
</resources>

src/com/example/testaudio/MainAudioActivity.java


package com.example.testaudio;

import android.os.Bundle;
import android.app.Activity;
// 아래는 import 된 클래스들
import android.view.View;
import android.media.MediaPlayer;
import android.view.View.OnClickListener;
import android.widget.Button;

//아래 implements 추가해야 함
public class MainAudioActivity extends Activity  implements OnClickListener{

	MediaPlayer mp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//액티비티 생성
		super.onCreate(savedInstanceState);
		//액티비티 main.xml 레이아웃 출력
		setContentView(R.layout.main);
		
		//미디어 플레이어 초기화:시작
		//drawable 폴더에 있는 music.mp3 파일 읽음
		mp = MediaPlayer.create(this, R.drawable.music);
		
		//오디오 파일읠 실행 시 반복 여부(미반복)
		mp.setLooping(false);
		
		//버튼 객체에 할당
		Button btn = (Button)this.findViewById(R.id.btn_play);
		//버튼 클릭하면 onClick 메소드 실행
		btn.setOnClickListener((OnClickListener) this);
	}
	
	public void onClick(View v){
		
		if(mp.isPlaying()){
			mp.pause();
		}else{
			mp.start();
		}
	}

}