Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone


Android SeekBar Control Audio Sound Volume (AudioManager)

Android SeekBar Control Audio Sound Volume (AudioManager) ตัวอย่างการใช้ SeekBar ในการแสดงสถานะของ Audio Sound Volume หรือความดังของลำโพงบนเครื่อง Smartphone หรือ Tablets โดยจะใช้ SeekBar แสดงตำแหน่ง และสามารถใช้ SeekBar ทำการเพิ่มหรือลดเสียงของลำโพง

Android SeekBar Audio Control Sound Volume


ในการเขียน Android เพื่อควบคุม Audio Sound จะใช้ class ของ AudioManager และ AUDIO_SERVICE

1.AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
2.// audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)) // Sound Volume

โดยใช้คำสั่งง่าย ๆ ก็สามารถเรียกค่าของ Sound Volume มาแสดงได้


Example 1 แสดง Audio Sound Volume บน SeekBar แบบง่าย ๆ

โครงสร้างของไฟล์ประกอบด้วย 2 ไฟล์คือ MainActivity.java, activity_main.xml

Android SeekBar Control Audio Sound Volume

activity_main.xml
01.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.    xmlns:tools="http://schemas.android.com/tools"
03.    android:layout_width="match_parent"
04.    android:layout_height="match_parent" >
05. 
06.    <TextView
07.        android:id="@+id/textView1"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content"
10.        android:layout_alignParentTop="true"
11.        android:layout_centerHorizontal="true"
12.        android:layout_marginTop="34dp"
13.        android:text="TextView" />
14. 
15.    <SeekBar
16.        android:id="@+id/seekBar2"
17.        android:layout_width="match_parent"
18.        android:layout_height="wrap_content"
19.        android:layout_alignParentLeft="true"
20.        android:layout_below="@+id/textView1"
21.        android:layout_marginTop="26dp" />
22. 
23.</RelativeLayout>


MainActivity.java
01.package com.myapp;
02. 
03.import android.media.AudioManager;
04.import android.os.Bundle;
05.import android.app.Activity;
06.import android.content.Context;
07.import android.view.Menu;
08.import android.widget.SeekBar;
09.import android.widget.SeekBar.OnSeekBarChangeListener;
10.import android.widget.TextView;
11. 
12.public class MainActivity extends Activity {
13. 
14.     
15.    @Override
16.    public void onCreate(Bundle savedInstanceState) {
17.        super.onCreate(savedInstanceState);
18.        setContentView(R.layout.activity_main);
19.         
20.        // call method ValumnControls()
21.        ValumnControls();     
22.         
23.    }
24.   
25.     
26.     
27.    private void ValumnControls()
28.    {
29.        try
30.        {
31.            final TextView txtView = (TextView)findViewById(R.id.textView1); // TextView
32.            SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2); // SeekBar
33.             
34.            final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
35.            seekBar2.setMax(audioManager
36.                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
37.            seekBar2.setProgress(audioManager
38.                    .getStreamVolume(AudioManager.STREAM_MUSIC));  
39.             
40.            // TextView
41.            txtView.setText(String.valueOf(audioManager
42.                    .getStreamVolume(AudioManager.STREAM_MUSIC)));
43. 
44. 
45.            seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
46.            {
47.                public void onStopTrackingTouch(SeekBar arg0)
48.                {
49.                }
50. 
51.                public void onStartTrackingTouch(SeekBar arg0)
52.                {
53.                }
54. 
55.                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
56.                {
57.                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
58.                            progress, 0);
59.                    txtView.setText(String.valueOf(progress));
60.                }
61.            });
62.        }
63.        catch (Exception e)
64.        {
65.            e.printStackTrace();
66.        }
67.    }
68.     
69.    @Override
70.    public boolean onCreateOptionsMenu(Menu menu) {
71.        getMenuInflater().inflate(R.menu.activity_main, menu);
72.        return true;
73.    }
74.     
75.}


Screenshot

Android SeekBar Control Audio Sound Volume

แสดง SeekBar และตำแหน่งของ Audio Sound Volume ซึ่งสามารถเลื่อน SeekBar เพื่อเพิ่มหรือลดเสียงได้



Example 2 ทดสอบใช้งาน Audio Sound Volume ร่วมกับการเล่นเพลง mp3

โครงสร้างของไฟล์ประกอบด้วย 2 ไฟล์คือ MainActivity.java, activity_main.xml

สำหรับตัวอย่างการเช่นไฟล์ mp3 สามารถอ่านได้จาก 2 ตัวอย่างนี้


Android SeekBar Control Audio Sound Volume

ออกแบบ XML Layout ดังภาพ

activity_main.xml
01.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.    xmlns:tools="http://schemas.android.com/tools"
03.    android:layout_width="match_parent"
04.    android:layout_height="match_parent" >
05. 
06.    <Button
07.        android:id="@+id/button1"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content"
10.        android:layout_alignParentLeft="true"
11.        android:layout_alignParentTop="true"
12.        android:layout_marginLeft="62dp"
13.        android:layout_marginTop="89dp"
14.        android:text="Play" />
15. 
16.    <TextView
17.        android:id="@+id/textView1"
18.        android:layout_width="wrap_content"
19.        android:layout_height="wrap_content"
20.        android:layout_alignParentTop="true"
21.        android:layout_centerHorizontal="true"
22.        android:layout_marginTop="34dp"
23.        android:text="TextView" />
24. 
25.    <Button
26.        android:id="@+id/button2"
27.        android:layout_width="wrap_content"
28.        android:layout_height="wrap_content"
29.        android:layout_alignBaseline="@+id/button1"
30.        android:layout_alignBottom="@+id/button1"
31.        android:layout_toRightOf="@+id/button1"
32.        android:text="Pause" />
33. 
34.    <Button
35.        android:id="@+id/button3"
36.        android:layout_width="wrap_content"
37.        android:layout_height="wrap_content"
38.        android:layout_alignBaseline="@+id/button2"
39.        android:layout_alignBottom="@+id/button2"
40.        android:layout_toRightOf="@+id/button2"
41.        android:text="Stop" />
42. 
43.    <SeekBar
44.        android:id="@+id/seekBar1"
45.        android:layout_width="match_parent"
46.        android:layout_height="wrap_content"
47.        android:layout_above="@+id/button2"
48.        android:layout_alignParentLeft="true" />
49. 
50.    <SeekBar
51.        android:id="@+id/seekBar2"
52.        android:layout_width="match_parent"
53.        android:layout_height="wrap_content"
54.        android:layout_alignParentLeft="true"
55.        android:layout_centerVertical="true" />
56. 
57.    <TextView
58.        android:id="@+id/textView2"
59.        android:layout_width="wrap_content"
60.        android:layout_height="wrap_content"
61.        android:layout_above="@+id/seekBar2"
62.        android:layout_centerHorizontal="true"
63.        android:layout_marginBottom="16dp"
64.        android:text="Sound Volume Control" />
65. 
66.</RelativeLayout>


MainActivity.java
001.package com.myapp;
002. 
003. 
004.import java.io.IOException;
005. 
006.import android.media.AudioManager;
007.import android.media.MediaPlayer;
008.import android.os.Bundle;
009.import android.os.Handler;
010.import android.app.Activity;
011.import android.content.Context;
012.import android.view.Menu;
013.import android.view.View;
014.import android.view.View.OnTouchListener;
015.import android.widget.Button;
016.import android.view.MotionEvent;
017.import android.widget.SeekBar;
018.import android.widget.SeekBar.OnSeekBarChangeListener;
019.import android.widget.TextView;
020. 
021.public class MainActivity extends Activity {
022. 
023.    private MediaPlayer mMedia;
024.    private Handler handler = new Handler();
025.    private SeekBar seekBar1;
026. 
027.     
028.    @Override
029.    public void onCreate(Bundle savedInstanceState) {
030.        super.onCreate(savedInstanceState);
031.        setContentView(R.layout.activity_main);
032.         
033.        // call method ValumnControls()
034.        ValumnControls();
035.         
036.         
037.        if(mMedia != null){
038.         mMedia.release(); 
039.        }
040.         
041.        final TextView txtView = (TextView)findViewById(R.id.textView1);
042.        txtView.setText("Source : music.mp3");
043.         
044.        /* Resource in R.
045.         * mMedia = MediaPlayer.create(this, R.raw.music);
046.         * mMedia.start();
047.         */
048.         
049.        /*
050.         * from DataSource
051.         * mMedia = new MediaPlayer();
052.         * mMedia.setDataSource("https://www.thaicreate.com/music/mymusic.mp3");
053.         * mMedia.start();
054.         *
055.         */
056.        mMedia = MediaPlayer.create(this, R.raw.music);   
057.         
058.        // seekBar1 Control media
059.        seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
060.        seekBar1.setMax(mMedia.getDuration());
061.        seekBar1.setOnTouchListener(new OnTouchListener() {
062.            public boolean onTouch(View v, MotionEvent event) {
063.                UpdateseekChange(v);
064.            return false;
065.            }
066.        });
067. 
068. 
069. 
070.         
071.        final Button btn1 = (Button) findViewById(R.id.button1); // Start
072.        final Button btn2 = (Button) findViewById(R.id.button2); // Pause
073.        final Button btn3 = (Button) findViewById(R.id.button3); // Stop
074.         
075.         
076.        // Start
077.        btn1.setOnClickListener(new View.OnClickListener() {
078.            public void onClick(View v) {
079.                txtView.setText("Playing : music.mp3....");
080.                mMedia.start();
081.                startPlayProgressUpdater();
082.                btn1.setEnabled(false);
083.                btn2.setEnabled(true);
084.                btn3.setEnabled(true);
085.            }
086.        });
087.         
088.        // Pause
089.        btn2.setOnClickListener(new View.OnClickListener() {
090.            public void onClick(View v) {
091.                txtView.setText("Pause : music.mp3");
092.                mMedia.pause();
093.                btn1.setEnabled(true);
094.                btn2.setEnabled(false);
095.                btn3.setEnabled(false);
096.            }
097.        }); 
098.         
099.        // Stop
100.        btn3.setOnClickListener(new View.OnClickListener() {
101.            public void onClick(View v) {
102.                txtView.setText("Stop Play : music.mp3");
103.                mMedia.stop();
104.                btn1.setEnabled(true);
105.                btn2.setEnabled(false);
106.                btn3.setEnabled(false);
107.                try {
108.                    mMedia.prepare();
109.                    mMedia.seekTo(0);
110.                } catch (IllegalStateException e) {
111.                    // TODO Auto-generated catch block
112.                    e.printStackTrace();
113.                } catch (IOException e) {
114.                    // TODO Auto-generated catch block
115.                    e.printStackTrace();
116.                }
117.                 
118.            }
119.        });
120.         
121.    }
122.     
123.    private void UpdateseekChange(View v){
124.        if(mMedia.isPlaying()){
125.            SeekBar sb = (SeekBar)v;
126.            mMedia.seekTo(sb.getProgress());
127.        }
128.    }
129.     
130.    public void startPlayProgressUpdater() {
131.        seekBar1.setProgress(mMedia.getCurrentPosition());
132. 
133.        if (mMedia.isPlaying()) {
134.            Runnable notification = new Runnable() {
135.                public void run() {
136.                    startPlayProgressUpdater();
137.                }
138.            };
139.            handler.postDelayed(notification,1000);
140.        }
141.    }
142.     
143.     
144.    @Override
145.    protected void onDestroy() {
146.   // TODO Auto-generated method stub
147.     super.onDestroy();
148.     if(mMedia != null){
149.         mMedia.release(); 
150.     }
151.    }
152.     
153.     
154.    private void ValumnControls()
155.    {
156.        try
157.        {
158.            SeekBar seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
159.            final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
160.            seekBar2.setMax(audioManager
161.                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
162.            seekBar2.setProgress(audioManager
163.                    .getStreamVolume(AudioManager.STREAM_MUSIC));  
164. 
165. 
166.            seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
167.            {
168.                public void onStopTrackingTouch(SeekBar arg0)
169.                {
170.                }
171. 
172.                public void onStartTrackingTouch(SeekBar arg0)
173.                {
174.                }
175. 
176.                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
177.                {
178.                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
179.                            progress, 0);
180.                }
181.            });
182.        }
183.        catch (Exception e)
184.        {
185.            e.printStackTrace();
186.        }
187.    }
188.     
189.    @Override
190.    public boolean onCreateOptionsMenu(Menu menu) {
191.        getMenuInflater().inflate(R.menu.activity_main, menu);
192.        return true;
193.    }
194.     
195.}




Screenshot

Android SeekBar Control Audio Sound Volume

ทดสอบการใช้งานจริงร่วมกับการเล่นเพลง mp3

   
Hate it
Don't like it
It's ok
Like it
Love it
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-08-25 15:47:51 / 2017-03-26 22:41:58
  Download : No files
 Sponsored Links / Related

 
Android Play VDO เล่นวีดีโอไฟล์ง่าย ๆ บน Android ด้วย VideoView
Rating :

 
Android Play mp3 เล่น Music media ไฟล์ mp3 และไฟล์อื่น ๆ ด้วย MediaPlayer
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่