音频的播放
16lz
2021-01-23
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;@Overrideprotected 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(){//初始化mediaplayermMediaPlayer = 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);}@Overridepublic 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;}}}
更多相关文章
- Android耳机声音自动调整
- Android中动态初始化布局参数以及ConstraintLayout使用中遇到的
- Android播放ogg声音文件
- Android 8 声音调整过程
- Android获取声音长度
- Android GPS学习笔记—LMS初始化
- 修改Android开机图片、动画、声音
- android kernel 初始化 2