SoundPool
SoundPool類管理和播放音頻資源的應用。因爲MediaPlayer在播放音樂時會資源佔用量較高、延遲時間較長、不支持多個音頻同時播放等。,但有時一些系統提示音很小,就沒有必要用MediaPlayer去播放,就用到了SoundPool.SoundPool載入音樂文件使用了獨立的線程,不會阻塞UI主線程的操作。SoundPool主要用於播放一些較短的聲音片段,與MediaPlayer相比,SoundPool的優勢在於CPU資源佔用量低和反應延遲小。另外,SoundPool還支持自行設置聲音的品質、音量、 播放比率等參數。
SoundPlayer 播放音頻的實現步驟
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_soundpool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示音"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button btn_soundpool;
private SoundPool pool=null;
private int voiceID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceID=initSoundpool();
btn_soundpool= (Button) findViewById(R.id.btn_soundpool);
btn_soundpool.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound();
}
});
}
public void playSound(){
//play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) ,
// soundID該方法的第一個參數指定播放哪個聲音,其中leftVolume和rightVolume表示左右音量,
// priority表示優先級,loop表示循環次數,rate表示速率,如
//速率最低0.5最高爲2,1代表正常速度
pool.play(voiceID,1,1,0,1,1);
}
private int initSoundpool() {
//判斷sdk的版本號,高於21 的可以用下面的方法
if (Build.VERSION.SDK_INT >= 21) {
//通過SoundPool.Builder創建SoundPool得到實例對象
SoundPool.Builder builder=new SoundPool.Builder();
//setMaxStreams()方法設置可以同時播放的同時流的最大數量。參數爲大於等於1的值
builder.setMaxStreams(2);
//AudioAttributes類封裝的屬性的集合來描述一個音頻流的信息。
//AudioAttributes.Builder用於生成AudioAttributes類
AudioAttributes.Builder attrBuilder=new AudioAttributes.Builder();
//AudioAttributes.Builder的setLegacyStreamType()方法從傳統的流類型中推斷出的屬性。
//setLegacyStreamType參數AudioManager.STREAM_MUSIC爲音樂播放的音頻流
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
//setAudioAttributes()方法得到AudioAttributes集合,參數爲attribute類型的非空值
builder.setAudioAttributes(attrBuilder.build());
pool=builder.build();
}else{
//SDK版本低於21的可以用下面的方法
//第一個參數是聲音的大小,第二個參數是數量流的類型,第三個參數爲採樣率轉化質量,當前無效果,使用0作爲默認值
pool=new SoundPool(2,AudioManager.STREAM_MUSIC,0);
}
//int load(String path, int priority) 從APK資源載入或 resld 所對應的資源加載聲音。
//最後一個參數爲優先級。
//加載聲音之後,該方法都會返回該聲音的的ID,以後程序就可以通過該聲音的ID 來播放指定聲音。
return pool.load(getApplicationContext(),R.raw.msg,1);
}
}
MediaRecorder錄音
MediaRecorder的機制圖,錄音需要系統的錄音權限
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
基本的步驟爲:
1、創建一個實例對象
2、創建一個Mediarecorder的類,然後調用Mediarecorder的方法完成設置audio源、設置輸出文件格式、audio編碼格式、設置輸出文件
3、準備錄音,開始錄音,停止錄音,釋放相關連接對象
>
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()+"/myrecord.3gp");
mediaRecorder.prepare();
mediaRecorder.start();
mediaRecorder.stop();
//reset()重啓mediarecorder其空閒狀態
mediaRecorder.reset();
//release()釋放資源這個mediarecorder對象關聯
mediaRecorder.release();
activity_main.xml
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_StartRecorder;
private Button btn_StopRecorder;
private MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceID=initSoundpool();
btn_StartRecorder= (Button) findViewById(R.id.btn_start_recorder);
btn_StartRecorder.setOnClickListener(this);
btn_StopRecorder= (Button) findViewById(R.id.btn_stop_recorder);
btn_StopRecorder.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_recorder:
//創建一個MediaRecorder類的實例對象
mediaRecorder=new MediaRecorder();
//setAudioSource()方法設置用於錄製的音頻源,參數爲使用的音頻源
//MediaRecorder.AudioSource定義的音頻源.MIC爲麥克風音頻源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//setOutputFormat()方法爲設置輸出格式爲.3gp
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//setAudioEncoder()方法爲設置編碼格式,
// 參數爲定義的音頻編碼格式,AMR_NB爲AMR(窄帶)音頻編解碼器
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//setOutputFile()方法設置輸出文件的路徑,
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()+"/myrecord.3gp");
try {
//prepare()準備錄音機開始捕獲和編碼數據。
mediaRecorder.prepare();
//start()開始捕獲和指定setoutputfile()文件數據編碼
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.btn_stop_recorder:
//stop()停止錄製
mediaRecorder.stop();
//reset()重啓mediarecorder其空閒狀態
mediaRecorder.reset();
//release()釋放資源這個mediarecorder對象關聯
mediaRecorder.release();
break;
}
}
}
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_start_recorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始錄音"/>
<Button
android:id="@+id/btn_stop_recorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="結束錄音"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.soundpool" >
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/music"
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>
<activity android:name=".SurfaceViewActivity"></activity>
</application>
</manifest>
VideoView和SurfaceView播放視頻
用VideoView控件來播放視頻,顯示視頻文件。VideoView類可以從各種來源加載圖像(如資源或內容提供商),負責計算測量從視頻,它可以用在任何佈局管理器,提供了各種各樣的顯示選項,如縮放和着色。
SurfaceView和View最本質的區別在於,surfaceView是在一個新起的單獨線程中可以重新繪製畫面而View必須在UI的主線程中更新畫面。 當使用surfaceView 由於是在新的線程中更新畫面所以不會阻塞你的UI主線程。雖然VideoView可以很容易地播放視頻,但播放位置和播放大小並不受控制,因此,需要用SurfaceView來播放視頻
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_surfaceview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始播放surfacview"/>
<Button
android:id="@+id/btn_start_playvideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始播放"/>
<VideoView
android:id="@+id/videoview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.soundpool" >
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/music"
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>
<activity android:name=".SurfaceViewActivity"></activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_StartPlay;
private VideoView mVideoView;
private Button btn_surfaceview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_StartPlay= (Button) findViewById(R.id.btn_start_playvideo);
btn_StartPlay.setOnClickListener(this);
mVideoView= (VideoView) findViewById(R.id.videoview);
btn_surfaceview= (Button) findViewById(R.id.btn_surfaceview);
btn_surfaceview.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_playvideo:
//setVideoPath設置視頻的文件來源位置
mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");
//setMediaController設置進度條在界面上顯示
mVideoView.setMediaController(new MediaController(MainActivity.this));
//start()開始播放視頻
mVideoView.start();
break;
case R.id.btn_surfaceview:
Intent intent=new Intent(MainActivity.this,SurfaceViewActivity.class);
startActivity(intent);
break;
}
}
}
SurfaceViewActivity.java啓動另一個界面來說明surfaceView播放視頻
public class SurfaceViewActivity extends Activity implements View.OnClickListener{
private Button btn_playsvvideo;
private SurfaceView mSurfaceView;
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surfaceview);
btn_playsvvideo= (Button) findViewById(R.id.btn_playsurfaceview);
btn_playsvvideo.setOnClickListener(this);
mSurfaceView= (SurfaceView) findViewById(R.id.surfaceview);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_playsurfaceview:
if (player==null){
player=new MediaPlayer();
}
player.reset();
try {
//設置視頻位置
player.setDataSource(Environment.getExternalStorageDirectory()+"/aa.mp4");
player.setAudioStreamType(AudioManager.STREAM_MUSIC);//設置聲音類型
Log.d("空指針",player+""+mSurfaceView);
player.setDisplay(mSurfaceView.getHolder());//設置視頻播放位置
player.prepare();//準備
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();//開始播放
}
});
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
activity_surfaceview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_playsurfaceview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始播放"/>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
調用系統攝像頭
調用系統的攝像頭進行拍照並顯示在ImageView上,還可以調用系統的相冊,選取其中的一張在ImageView上顯示,有時候照片質量過高,無法顯示,可以對照片進行壓縮,在進行存儲並顯示。
MainActivity.xml(這裏將圖片壓縮寫到另一個類裏)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_StartCamera;
private Button btn_OpenGallery;
private ImageView mImageView;
private File file;
private int GET_PIC_FORM_GALLERY=0x24;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_StartCamera= (Button) findViewById(R.id.btn_startcamera);
btn_StartCamera.setOnClickListener(this);
mImageView= (ImageView) findViewById(R.id.imageview);
btn_OpenGallery= (Button) findViewById(R.id.btn_open_gallery);
btn_OpenGallery.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_startcamera:
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隱式啓動系統相機
file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));//告訴系統相機將照片保存的位置
startActivityForResult(intent, 0x23);//開始啓動
break;
//調用系統相冊按鈕事件監聽
case R.id.btn_open_gallery:
Intent intentOpenGallery=new Intent(Intent.ACTION_GET_CONTENT);
//得到intent的類型爲圖片
intentOpenGallery.setType("image/*");
startActivityForResult(intentOpenGallery, GET_PIC_FORM_GALLERY);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
if (requestCode==0x23){
ImageZip.zipImage(file.getAbsolutePath());//壓縮圖片
mImageView.setImageURI(Uri.fromFile(file));//得到圖片
}if (requestCode==GET_PIC_FORM_GALLERY){
//從系統相冊中得到圖片
Uri uri=data.getData();
mImageView.setImageURI(uri);
}
}
}
}
ImageZip.java(壓縮圖片的方法)
public class ImageZip {
public static void zipImage(String savePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePath, options);
options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
try {
FileOutputStream fos = new FileOutputStream(savePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
bitmap = null;
System.gc();
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
activity_main.xml
<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">
<Button
android:id="@+id/btn_startcamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="調用系統相機"/>
<Button
android:id="@+id/btn_open_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="打開相冊"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.camera" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/camera"
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>
</manifest>