کنترل صدای زنگ گوشی با استفاده ازseekbar در اندروید
دوشنبه 12 تیر 1396دراین مقاله قصد داریم یک نمونه ی اندروید برای شما قرار دهیم که این نمونه مدیریت روی صداهای آهنگ هشدار گوشی و نوتیفیکیشن و زنگ گوشی را مدیریت می کند این نمونه با seekbar درست شده است.
در این نمونه زنگ گوشی و نوتیقیکیشن ها و زنگ هشدار را می توانید با استفاده از seekbar ساده مدیریت نمایید ابتدا در کلاس جاوا قطعه کد زیر را قرار دهید:
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
public class MainActivity extends Activity {
SeekBar alarm, mediaPlayer, ringer, notification ;
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarm = (SeekBar)findViewById(R.id.seekBar1);
mediaPlayer = (SeekBar)findViewById(R.id.seekBar2);
ringer = (SeekBar)findViewById(R.id.seekBar3);
notification = (SeekBar)findViewById(R.id.seekBar4);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
alarm.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
mediaPlayer.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
ringer.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_RING));
notification.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
alarm.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, i, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mediaPlayer.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
ringer.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
audioManager.setStreamVolume(AudioManager.STREAM_RING, i, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
notification.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, i, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
حالا در قسمت layout قطعه کد زیر را بنویسید:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.audiomanager_android_examples.com.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Set Alarm Volume" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginTop="10dp" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/seekBar1" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="Set Media Player Volume" android:textAppearance="?android:attr/textAppearanceLarge" /> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="10dp" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/seekBar2" android:layout_centerHorizontal="true" android:text="Set Ringer Volume" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginTop="10dp" /> <SeekBar android:id="@+id/seekBar3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginTop="10dp" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/seekBar3" android:layout_centerHorizontal="true" android:text="Set Notification Volume" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginTop="10dp" /> <SeekBar android:id="@+id/seekBar4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView4" android:layout_marginTop="10dp" /> </RelativeLayout>
خروجی شما به شکل زیر خواهد بود:

- Android
- 2k بازدید
- 2 تشکر