10天學通Android開發(fā)(8)-多媒體-創(chuàng)新互聯(lián)

一、播放聲音SoundPool

成都創(chuàng)新互聯(lián)公司云計算的互聯(lián)網(wǎng)服務提供商,擁有超過13年的服務器租用、成都多線機房、云服務器、網(wǎng)頁空間、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗,已先后獲得國家工業(yè)和信息化部頒發(fā)的互聯(lián)網(wǎng)數(shù)據(jù)中心業(yè)務許可證。專業(yè)提供云主機、網(wǎng)頁空間、域名注冊、VPS主機、云服務器、香港云服務器、免備案服務器等。

SoundPool播放短的音效,不能播放歌曲

可以用soundpool,用soundpool可以播一些短的反應速度要求高的聲音,
比如游戲中的爆破聲,而mediaplayer適合播放長點的。
SoundPool載入音樂文件使用了獨立的線程,不會阻塞UI主線程的操作, SoundPool類支持同時播放多個音效,這對于游戲來說是十分必要的,而MediaPlayer類是同步執(zhí)行的只能一個文件一個文件的播放。

實例:

sp=new SoundPool(1,AudioManager.STREAM_MUSIC,0);

加載:

soundId = sp.load(this, R.raw.note1, 1);

播放,可設置慢速、快速、頻率高低等

sp.play(soundId, 1, 1, 0, 0, 2.0f);

二、播放聲音MediaPlay

MediaPlayer可播放長的聲音,可后臺播放

創(chuàng)建:

mp= MediaPlayer.create(this,R.raw.song);

釋放:

mp.release();

相關資源準備、該下載就下載

mp.prepare();

播放:

mp.start();

三、錄音MediaRecord

寫入外部存儲的權限:

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

錄音權限:

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

創(chuàng)建目錄:

File dir=new File(Environment.getExternalStorageDirectory(),"sonunds");

            if(!dir.exists())

            {

               dir.mkdir();//創(chuàng)建目錄

            }

創(chuàng)建文件:

File soundFile=new File(dir,System.currentTimeMillis()+".amr");

            if(!soundFile.exists())

            {

               try//捕獲一個異常

               {

               soundFile.createNewFile();

               }

               catch(IOException e)

               {

                  e.printStackTrace();

               }

            }

指定輸出

mr=new MediaRecorder();

mr.setOutputFile(soundFile.getAbsolutePath());/

準備并開始錄制:

mr.prepare();

mr.start();

停止錄制:

mr.stop();

案例分析:

  1. 界面,四個按鈕:

<Button

android:id="@+id/btnPlaySound"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="PlaySound" />

   <Button

android:id="@+id/btnPlaySong"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="PlaySong" />

      <Button

android:id="@+id/btnRecordBegin"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="開始錄制"/>

        <Button

android:id="@+id/btnRecordStop"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="停止錄制"/>

  1. 播放短的音效

sp=new SoundPool(1,AudioManager.STREAM_MUSIC,0);

        soundId = sp.load(this, R.raw.note1, 1);

     findViewById(R.id.btnPlaySound).setOnClickListener(new View.OnClickListener() {

         @Override

         publicvoid onClick(View v) {

            sp.play(soundId, 1, 1, 0, 0,2.0f);

         }

   });

  1. 播放歌曲

findViewById(R.id.btnPlaySong).setOnClickListener(newView.OnClickListener() {

         @Override

         publicvoid onClick(View v) {

            if (mp!=null) {

               mp.start();

            }

         }

     });

4)錄音

findViewById(R.id.btnRecordBegin).setOnClickListener(new View.OnClickListener() {

     @Override

     publicvoid onClick(View v) {

          startRecord();

     }

     privatevoid startRecord() {

         if(mr==null)

         {

            File dir=new File(Environment.getExternalStorageDirectory(),"sonunds");

            if(!dir.exists())

            {

               dir.mkdir();//創(chuàng)建目錄

            }

            File soundFile=new File(dir,System.currentTimeMillis()+".amr");

            if(!soundFile.exists())

            {

               try//捕獲一個異常

               {

               soundFile.createNewFile();

               }

               catch(IOException e)

               {

                  e.printStackTrace();

               }

            }

            mr=new MediaRecorder();

            mr.setAudioSource(MediaRecorder.AudioSource.MIC);//指定輸入源

           mr.setOutputFormat(MediaRecorder.AudioEncoder.AMR_WB);//輸出編碼格式

           mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);//輸入編碼格式

            mr.setOutputFile(soundFile.getAbsolutePath());//指定輸出

            try {

               mr.prepare();

               mr.start();

            } catch (IllegalStateException e) {

               // TODO Auto-generatedcatch block

               e.printStackTrace();

            } catch (IOException e) {

               // TODO Auto-generatedcatch block

               e.printStackTrace();

            }

         }

     }

  });

5)停止錄音

findViewById(R.id.btnRecordStop).setOnClickListener(new View.OnClickListener() {

     @Override

     publicvoid onClick(View v) {

          stopRecord();

     }

     privatevoid stopRecord() {

         if(mr!=null)

         {

            mr.stop();

            mr.release();

            mr=null;

         }

     }

  });

6)權限設置

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

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

×××:

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前題目:10天學通Android開發(fā)(8)-多媒體-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://bm7419.com/article32/gigpc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制開發(fā)電子商務、網(wǎng)站排名、小程序開發(fā)自適應網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站