android 音訊播放 以及 二維碼模組中音訊播放和震動(vibrate)的實現
參考:《第一行程式碼》第8章
########################################################################
在Android中播放音訊檔案一般都是使用MediaPlayer類來實現,它對多種格式的音訊檔案提供了非常全面的控制方法,從而使得播放音樂的工作變得非常簡單。
常用MediaPlayer類的控制方法:
1.setDataSource():設定要播放的音訊檔案的位置
2.prepare():在開始播放之前呼叫這個方法完成準備工作
3.start():開始或繼續播放音訊
4.pause():暫停播放音訊
5.reset():將MediaPlayer物件重置到剛剛建立的狀態
6.seekTo():從指定的位置開始播放音訊
7.stop():停止播放音訊。呼叫這個方法後的MediaPlayer無法再播放音訊
8.release():釋放與MediaPlayer物件相關的資源
9.isPlaying():判斷當前MediaPlayer是否正在播放音訊
10.getDuration():獲取載入的音訊檔案的時長
簡單的工作流程:首先需要創建出一個MediaPlayer物件,然後呼叫setDataSource()方法來設定音訊檔案的路徑,再呼叫prepare()方法使MediaPlayer進入到準備狀態,接下來呼叫start()方法就可以開始播放音訊,呼叫pause()方法就會暫停播放,呼叫reset()方法就會停止播放。
新建專案AudioTest
MainActivity.java:
package com.zj.audiotest; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import java.io.File; public class MainActivity extends Activity implements View.OnClickListener { private Button play; private Button pause; private Button stop; private MediaPlayer mediaPlayer = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play = (Button)findViewById(R.id.play); pause = (Button)findViewById(R.id.pause); stop = (Button)findViewById(R.id.stop); play.setOnClickListener(this); pause.setOnClickListener(this); stop.setOnClickListener(this); initMediaPlayer(); //initialize } private void initMediaPlayer() { try { File file = new File(Environment.getExternalStorageDirectory(), "music.mp3"); mediaPlayer.setDataSource(file.getPath()); //Specify the path for the audio file mediaPlayer.prepare(); //Let MediaPlayer enter the ready state } catch (Exception e) { e.printStackTrace(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.play: if (!mediaPlayer.isPlaying()) { mediaPlayer.start(); //start playing } break; case R.id.pause: if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); //pause playback } break; case R.id.stop: if (mediaPlayer.isPlaying()) { mediaPlayer.reset(); //stop playing and return to the state of the uninialization initMediaPlayer(); //initialize } break; default: break; } } @Override protected void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"
/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop"
/>
</LinearLayout>
訪問外部儲存需要讀許可權
在AndroidManifest.xml上加入:
<!-- 訪問外部儲存的許可權 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
##################################################################################3333
震動VIBRATE:
long VIBRATE_DURATIN = 200L;
Vibrator vibrate = (Vibrator)getSystemService(VIBRATOR_SERVICE);
vibrate.vibrate(VIBRATE_DURATIN);
需要許可權:
<!-- permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
函式解析:
/**
* Use with {@link #getSystemService} to retrieve a {@link
* android.os.Vibrator} for interacting with the vibration hardware.
*
* @see #getSystemService
* @see android.os.Vibrator
*/
public static final String VIBRATOR_SERVICE = "vibrator";
使用函式getSystemService去檢索一個震動功能,為了和震動硬體互動
/**
* Vibrate constantly for the specified period of time.
* <p>This method requires the caller to hold the permission
* {@link android.Manifest.permission#VIBRATE}.
*
* @param milliseconds The number of milliseconds to vibrate.
*/
public void vibrate(long milliseconds) {
vibrate(milliseconds, null);
}
在指定時間內不斷的震動。此方法需要擁有許可權
android.permission.VIBRATE
引數milliseconds表示振動的毫秒數
##################################################################################
二維碼模組的音訊播放和震動:
修改上面AudioTest專案:
MainActivity.java如下:
package com.zj.audiotest;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends Activity implements View.OnClickListener {
private MediaPlayer mediaPlayer;
private boolean playBeep;
private static final float BEEP_VOLUME = 0.10f;
private boolean vibrate;
private Button btnVibrate;
private static final long VIBRATE_DURATION = 200L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnVibrate = (Button)findViewById(R.id.vibrate);
btnVibrate.setOnClickListener(this);
playBeep = true;
AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
playBeep = false;
}
initBeepSound();
vibrate = true;
}
private void initBeepSound() {
if (playBeep && mediaPlayer == null) {
// The volume on STREAM_SYSTEM is not adjustable, and users found it
// too loud,
// so we now play on the music stream.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.setOnCompletionListener(beepListener);
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(),
file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
} catch (IOException e) {
mediaPlayer = null;
}
}
}
/**
* When the beep has finished playing, rewind to queue up another one.
*/
private final MediaPlayer.OnCompletionListener beepListener = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.seekTo(0);
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.vibrate:
playBeepSoundAndVibrate();
break;
default:
break;
}
}
public void playBeepSoundAndVibrate() {
if (playBeep && mediaPlayer != null) {
mediaPlayer.start();
}
if (vibrate) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_DURATION);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
activity_main.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/vibrate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Vib"
/>
</LinearLayout>
需要加上震動的許可權,故AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zj.audiotest" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
<!-- 訪問外部儲存的許可權 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
#########################################################################