๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’š ๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ

[์•ˆ๋“œ๋กœ์ด๋“œ] Android ์ง„๋™/mp3ํŒŒ์ผ ๋„ฃ๊ธฐ

by devshin.kr 2022. 5. 16.
728x90
ํ•ด๋‹น ๊ธ€์€ 2018๋…„ 12์›” 8์ผ์— ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ง„๋™๊ณผ ์‚ฌ์šด๋“œ๊ฐ€ ์šธ๋ฆฌ๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ๋‹ค.

xml ํ˜•์‹์€ ์‰ฝ๊ฒŒ RelativeLayout ์œผ๋กœ ๋งŒ๋“ค์ž.

1. ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

res > layout > activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์ง„๋™"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์†Œ๋ฆฌ" 
        android:layout_toRightOf="@+id/btn1"/>

</RelativeLayout>

 

 

activity_main.xml์„ ์œ„ ์ฝ”๋“œ๋Œ€๋กœ ๋งŒ๋“ค๋ฉด ์œ„์™€ ๊ฐ™์€ ์ฐฝ์ด ๋งŒ๋“ค์–ด ์ง„๋‹ค.

โ€‹

 

 

2. Manifest file์— ์ง„๋™ permission์„ ๊ผญ! ์ถ”๊ฐ€ํ•œ๋‹ค.

์Œ์•…์„ ์œ„ํ•œ permission์€ ํ•„์š” ์—†๋‹ค.

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

๊ถŒํ•œ์€ <application></application> ๋ฐ–์—์„œ! ๋„ฃ์–ด ์ค€๋‹ค. (์•„๋ž˜ manifest file ์ฐธ๊ณ )

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="shin.kr.myapplication">


    <uses-permission android:name="android.permission.VIBRATE"></uses-permission>

    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

โ€‹

3. MainActivity.java

package shin.kr.myapplication;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    MediaPlayer mp = MediaPlayer.create(this, R.raw.twinkle);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button vib = findViewById(R.id.btn1);
        Button sound = findViewById(R.id.btn2);

        vib.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                vibrator.vibrate(1000);
            }
        });

        sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mp.start();
            }
        });

    }
}

 

 

โ€‹

4. MediaPlayer์— ๋“ค์–ด ๊ฐˆ mp3ํŒŒ์ผ์„ ๋„ฃ๊ธฐ ์œ„ํ•ด resํด๋” ์•ˆ์— rawํด๋”๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์•ˆ์— mp3ํŒŒ์ผ์„ ๋„ฃ๋Š”๋‹ค.

 

 

 

์ถ”๊ฐ€ ์„ค๋ช… :

- ์ง„๋™ : vibrator.vibrate(1000); ์˜ ์˜๋ฏธ. ์—ฌ๊ธฐ์—์„œ์˜ ๋‹จ์œ„๋Š” milliseconds๋กœ 1000ms = 1s. ์ฆ‰ 1์ดˆ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. 1์ดˆ ๋™์•ˆ ์ง„๋™์ด ์šธ๋ฆฌ๊ฒŒ ๋งŒ๋“œ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์‘์šฉํ•˜๋ฉด 0.5์ดˆ๋Š” 500์œผ๋กœ, 2์ดˆ๋Š” 2000์œผ๋กœ ๊ฐ’์„ ๋ฐ”๊ฟ” ์ฃผ๋ฉด ๋œ๋‹ค.

- ์‚ฌ์šด๋“œ : if (mp.isPlaying()) mp.stop(); ์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ๋„ฃ์œผ๋ฉด ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์Œ์•…์ด ์žฌ์ƒ๋˜๊ณ , ๋‹ค์‹œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์Œ์•…์ด ์ •์ง€๋˜๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๋Œ“๊ธ€