音频的播放
1.
<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="wyf.ytl.MainActivity" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="没有播放任何声音" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用MediaPlayer播放声音" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂停MediaPlayer声音" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用SoundPool播放声音" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂停SoundPool声音" /> </LinearLayout>
2.
public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener{ Button button1; Button button2; Button button3; Button button4; TextView textView; MediaPlayer mMediaPlayer; SoundPool soundPool; HashMap<Integer, Integer> soundPoolMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initSounds(); textView = (TextView) findViewById(R.id.textView); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button4 = (Button) findViewById(R.id.button4); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); } public void initSounds(){ //初始化mediaplayer mMediaPlayer = MediaPlayer.create(this, R.raw.backsound); soundPool = new SoundPool(4,AudioManager.STREAM_MUSIC, 100); soundPoolMap = new HashMap<Integer, Integer>(); soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1)); } //用SoundPool播放声音的方法 public void playSound(int sound,int loop) { AudioManager mgr = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = streamVolumeCurrent/streamVolumeMax; soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f); } @Override public void onClick(View v) { int key = v.getId(); switch (key) { case R.id.button1: textView.setText("use MediaPlayer to play sound"); if(!mMediaPlayer.isPlaying()){ mMediaPlayer.start(); } break; case R.id.button2: textView.setText("suspend MediaPlayer's sound "); if(mMediaPlayer.isPlaying()){ mMediaPlayer.pause(); } break; case R.id.button3: textView.setText("use SoundPool to play sound"); this.playSound(1, 0); break; case R.id.button4: textView.setText("suspend the SoundPool's sound"); soundPool.pause(1); break; default: break; } } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28