sel.xml파일 생성및 res/raw/mp3파일 넣어주기




main.xml
<?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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sel" 
        android:layout_gravity="center"/>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="stop" />

</LinearLayout>
sel.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 눌러졌을때 보여질 이미지 선택 -->
    <item android:state_pressed="true"
          android:drawable="@drawable/red"/>
	
    <!-- 기본 상태에서 보여질 이미지 -->
	<item android:drawable="@drawable/yellow"/>
	    
</selector>




ServiceActivity.java
package com.gusfree.service;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class ServiceActivity extends Activity {
    ImageButton imageButton;
    Button button;
    MediaPlayer mp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageButton=(ImageButton) findViewById(R.id.imageButton1);
        button = (Button)findViewById(R.id.button1);
       
        ClickListener listener = new ClickListener();
        imageButton.setOnClickListener(listener);
        button .setOnClickListener(listener) ;                

        mp = MediaPlayer.create(this, R.raw.riverside);
        
        
    }
    class ClickListener implements android.view.View.OnClickListener{
  
		@Override
		public void onClick(View v) {
			
			if(v==imageButton){//음악 시작
				imageButton.setEnabled(false);//못누르게
				mp.start();
			}else if(v==button) {//음악 종료
				imageButton.setEnabled(true);
				mp.pause();
			}
		}
    }
}



음악이 재생된다~~


+ Recent posts