我想实现另外两个button来增加/减少音量并设置为媒体播放器.
请帮我在android中访问音量增大/减小.
活动代码:
control = (ImageVIEw) findVIEwByID(R.ID.control);mediaPlayer = new MediaPlayer();mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);control.setonClickListener(pausePlay);control.setBackgroundResource(R.drawable.pause);control ID is my play and pause button :{ // Todo auto-generated method stub // Todo auto-generated method stub if (playPause) { control.setBackgroundResource(R.drawable.play); if (mediaPlayer.isPlaying()) mediaPlayer.pause(); media.stop(); intialStage = false; playPause = false; } else { control.setBackgroundResource(R.drawable.pause); if (intialStage) { new Player() .execute("http://streaming.shoutcast.com/MUKILFMRAdio"); } else { if (!mediaPlayer.isPlaying()) mediaPlayer.start(); } playPause = true; }}
布局代码:
<linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" androID:gravity="center"> <ImageVIEw androID:layout_wIDth="40dp" androID:layout_height="40dp" androID:ID="@+ID/control1" androID:layout_margin="10dp" androID:layout_gravity="center" androID:background="@drawable/decrement" androID:layout_above="@+ID/latestAddedSongs" androID:layout_alignEnd="@+ID/musicArtistname" /> <ImageVIEw androID:layout_wIDth="70dp" androID:layout_height="70dp" androID:ID="@+ID/control" androID:layout_margin="10dp" androID:layout_gravity="center" androID:background="@drawable/play" androID:layout_above="@+ID/latestAddedSongs" androID:layout_alignEnd="@+ID/musicArtistname" /> <ImageVIEw androID:layout_wIDth="40dp" androID:layout_height="40dp" androID:ID="@+ID/control2" androID:layout_margin="10dp" androID:layout_gravity="center" androID:background="@drawable/increment" androID:layout_above="@+ID/latestAddedSongs" androID:layout_alignEnd="@+ID/musicArtistname" /></linearLayout>
在AndroID中以编程方式提供有关静音/取消静音和音量调高/降低的链接或教程.
解决方法 为音频管理器创建一个对象AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUdio_SERVICE);button upbutton = (button) findVIEwByID(R.ID.upbutton); upbutton.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) {//To increase media player volume audioManager.adjustVolume(AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND); } }); button downbutton = (button) findVIEwByID(R.ID.downbutton); downbutton.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) {//To decrease media player volume audioManager.adjustVolume(AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND); } });
上面的例子使用了button标签
音量上下
码
@OverrIDe public boolean dispatchKeyEvent(KeyEvent event) { int action = event.getAction(); int keyCode = event.getKeyCode(); switch (keyCode) { case KeyEvent.KEYCODE_VolUME_UP: if (action == KeyEvent.ACTION_DOWN) { audioManager.adjustVolume(AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND); } return true; case KeyEvent.KEYCODE_VolUME_DOWN: if (action == KeyEvent.ACTION_DOWN) { audioManager.adjustVolume(AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND); } return true; default: return super.dispatchKeyEvent(event); } }总结
以上是内存溢出为你收集整理的如何在android中以编程方式增加和减少音量全部内容,希望文章能够帮你解决如何在android中以编程方式增加和减少音量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)