Android-audiomanager

提供:Dev Guides
移動先:案内検索

Android-オーディオマネージャー

Androidで着信音量と着信プロファイルi-e:(silent、vibrate、loud e.t.c)を簡単に制御できます。 Androidは、これらのコントロールへのアクセスを提供するAudioManagerクラスを提供します。

AndroidManagerクラスを使用するには、まず* getSystemService()*メソッドを呼び出してAudioManagerクラスのオブジェクトを作成する必要があります。 その構文は次のとおりです。

private AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

AudioManagerクラスのオブジェクトをインスタンス化したら、 setRingerMode メソッドを使用して、デバイスのオーディオまたは呼び出し音プロファイルを設定できます。 その構文は次のとおりです。

myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

メソッドsetRingerModeは、整数値をパラメーターとして受け取ります。 各モードには、異なるモードを区別する整数が割り当てられます。 可能なモードは次のとおりです。

Sr.No Mode & Description
1

RINGER_MODE_VIBRATE

このモードは、デバイスを振動モードに設定します。

2

RINGER_MODE_NORMAL

このモードは、デバイスを通常(大)モードに設定します。

3

RINGER_MODE_SILENT

このモードは、デバイスをサイレントモードに設定します。

modeを設定したら、* getRingerMode()*メソッドを呼び出して、システムの設定状態を取得できます。 その構文は次のとおりです。

int mod = myAudioManager.getRingerMode();

getRingerModeメソッドとは別に、AudioManagerクラスでボリュームやその他のモードを制御するために使用できる他のメソッドがあります。 それらは以下にリストされています。

Sr.No Method & description
1

adjustVolume(int direction, int flags)

このメソッドは、最も関連性のあるストリームの音量を調整します

2

getMode()

このメソッドは、現在のオーディオモードを返します

3

getStreamMaxVolume(int streamType)

このメソッドは、特定のストリームの最大ボリュームインデックスを返します

4

getStreamVolume(int streamType)

このメソッドは、特定のストリームの現在のボリュームインデックスを返します

5

isMusicActive()

このメソッドは、アクティブな音楽があるかどうかを確認します。

6

startBluetoothSco()

この方法は、Bluetooth SCOオーディオ接続を開始します

7

stopBluetoothSco()

このメソッドは、Bluetooth SCOオーディオ接続を停止します。

次の例は、AudioManagerクラスの使用方法を示しています。 デバイスに異なる呼び出しモードを設定できるアプリケーションを作成します。

この例を試すには、実際のデバイスでこれを実行する必要があります。

Steps Description
1 You will use Android studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add AudioManager code
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4 Modify res/values/string.xml file and add necessary string components.
5 Modify AndroidManifest.xml to add necessary permissions.
6 Run the application and choose a running android device and install the application on it and verify the results.
*src/MainActivity.java* のコンテンツは次のとおりです。
package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {
   Button mode,ring,vibrate,silent;
   private AudioManager myAudioManager;

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

      vibrate=(Button)findViewById(R.id.button3);
      ring=(Button)findViewById(R.id.button2);

      mode=(Button)findViewById(R.id.button);
      silent=(Button)findViewById(R.id.button4);
      myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

      vibrate.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            Toast.makeText(MainActivity.this,"Now in Vibrate Mode",
               Toast.LENGTH_LONG).show();
         }
      });

      ring.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            Toast.makeText(MainActivity.this,"Now in Ringing Mode",
               Toast.LENGTH_LONG).show();
         }
      });

      silent.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            Toast.makeText(MainActivity.this,"Now in silent Mode",
               Toast.LENGTH_LONG).show();
         }
      });

      mode.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int mod=myAudioManager.getRingerMode();
            if(mod==AudioManager.RINGER_MODE_VIBRATE){
               Toast.makeText(MainActivity.this,"Now in Vibrate Mode",
                  Toast.LENGTH_LONG).show();
            } else if(mod==AudioManager.RINGER_MODE_NORMAL){
               Toast.makeText(MainActivity.this,"Now in Ringing Mode",
                  Toast.LENGTH_LONG).show();
            } else {
               Toast.makeText(MainActivity.this,"Now in Vibrate Mode",
                  Toast.LENGTH_LONG).show();
            }
         }
      });
   }
}
*activity_main.xml* のコンテンツは次のとおりです。

'_ここでabcはfinddevguidesのロゴを示します_

<?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"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Android Audio Recording"
      android:id="@+id/textView"
      android:textSize="30dp"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"/>

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="finddevguides"
      android:id="@+id/textView2"
      android:textColor="#ff3eff0f"
      android:textSize="35dp"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"/>

   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView2"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Mode"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="59dp"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Ring"
      android:id="@+id/button2"
      android:layout_alignTop="@+id/button"
      android:layout_centerHorizontal="true"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="vibrate"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button2"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Silent"
      android:id="@+id/button4"
      android:layout_below="@+id/button2"
      android:layout_alignLeft="@+id/button2"
      android:layout_alignStart="@+id/button2"/>
</RelativeLayout>
*Strings.xml* のコンテンツは次のとおりです。
<resources>
   <string name="app_name">My Application</string>
</resources>
*AndroidManifest.xml* のコンテンツは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >

      <activity
         android:name="com.example.sairamkrishna.myapplication"
         android:label="@string/app_name" >

         <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>

      </activity>

   </application>
</manifest>

アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 Androidスタジオは画像を表示します

サイレントボタンを選択すると、通知バーにサイレントアイコンが表示されます

Android Capture Tutorial

リングボタンを選択し、現在のモードボタンを押して、ステータスが設定されているかどうかを確認します。

Android Captureチュートリアル

バイブレーションボタンを押してから、現在のモードボタンを押して、設定されているかどうかを確認します。次の画面が表示されます。

Android Captureチュートリアル