Android使用SoundPool實(shí)現(xiàn)播放音頻
最近做一個(gè)播放音頻的小功能,使用毛坯界面簡單記錄下(點(diǎn)擊上邊的ImageButton播放,下邊的ImageView請無視)

activity_picture.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".PictureActivity"> <ImageButton android:id="@+id/ibCogVideo" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/ivCogPicture" android:layout_width="300dp" android:layout_height="300dp" android:layout_marginTop="100dp" android:src="@mipmap/ic_launcher" /> </LinearLayout>
PictureActivity.java頁面:
package com.example.two;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import java.util.HashMap;
public class PictureActivity extends AppCompatActivity implements View.OnClickListener {
private ImageButton ibCogVideo;
private ImageView ivCogPicture;
SoundPool mSoundPool; //一般用來播放短音頻
HashMap<Integer,Integer> map=new HashMap<>(); //創(chuàng)建集合存放數(shù)據(jù)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
initViews();
bindViews();
initDatas();
}
/*初始化數(shù)據(jù)*/
private void initDatas() {
mSoundPool=new SoundPool(3, AudioManager.STREAM_MUSIC,0); //創(chuàng)建音頻對象,參數(shù)為(可容納音頻個(gè)數(shù),聲音類型,音頻品質(zhì)默認(rèn)為0)
map.put(1,mSoundPool.load(this,R.raw.abc,100)); //設(shè)置第一個(gè)音頻
}
/*綁定點(diǎn)擊事件*/
private void bindViews() {
ibCogVideo.setOnClickListener(this);
}
/*初始化控件*/
private void initViews() {
ibCogVideo=findViewById(R.id.ibCogVideo);
ivCogPicture=findViewById(R.id.ivCogPicture);
}
/*點(diǎn)擊事件*/
@Override
public void onClick(View v) {
mSoundPool.play(map.get(1),1,1,100,0,1); //參數(shù)為(要播放的音頻,左聲道音量,右聲道音量,音頻優(yōu)先級,循環(huán)次數(shù),速率)
}
}
另外,音頻文件我放到了項(xiàng)目中,及res中的raw文件。貌似音頻文件可以放入raw或者assets中,不同是raw一般放小型素材并且在代碼中可以直接使用R.raw.xxx調(diào)用,而assets不可以。
AndroidStudio添加raw的方法:


點(diǎn)擊OK,然后把音頻文件拖入即可。
(get一個(gè)軟件,可以使用格式工廠進(jìn)行截取音頻,超級方便?。。。?/p>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SoundPool實(shí)現(xiàn)簡短小音效
- Android使用SoundPool播放短音效
- Android使用SoundPool實(shí)現(xiàn)播放音效
- Android使用SoundPool播放音效實(shí)例
- Android中SoundPool的使用步驟實(shí)例
- Android使用SoundPool播放音效
- Android多媒體應(yīng)用使用SoundPool播放音頻
- android使用SoundPool播放音效的方法
- Android編程實(shí)現(xiàn)使用SoundPool播放音樂的方法
- Android利用SoundPool實(shí)現(xiàn)音樂池
相關(guān)文章
Android編程自定義AlertDialog樣式的方法詳解
這篇文章主要介紹了Android編程自定義AlertDialog樣式的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義AlertDialog樣式的具體布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Gradle?Build?Cache引發(fā)的Task緩存編譯問題
這篇文章主要為大家介紹了Gradle?Build?Cache引發(fā)的Task緩存編譯問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android實(shí)現(xiàn)單頁顯示3個(gè)Item的ViewPager炫酷切換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)單頁顯示3個(gè)Item的ViewPager炫酷切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android筆記之:App應(yīng)用之發(fā)布各廣告平臺版本的詳解
Android的廣告平臺是很多的,各市場對各平臺的接受程度是不一樣的,Android的開發(fā)者如果想集成廣告基本要考慮下面兩個(gè)問題2013-04-04
Android webview轉(zhuǎn)PDF的方法示例
本篇文章主要介紹了Android webview轉(zhuǎn)PDF的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
詳細(xì)講解Android中使用LoaderManager加載數(shù)據(jù)的方法
這篇文章主要介紹了Android中使用LoaderManager加載數(shù)據(jù)的方法,講到了LoaderManager的異步加載與聲明周期的管理等相關(guān)用法,需要的朋友可以參考下2016-04-04

