实验报告
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016.4.30 |
||
实验项目名称 |
实验7 BindService模拟通信 |
实验地点 |
S30010 |
||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
2学时 |
||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||
目的:实现启动端和BindService之间的双向通信 要求:1.实现从启动端传递一个数据至BindService端; 2.实现使用BindService服务播放项目源文件中的音乐; 3.实现在启动端通过“增加”和“降低”两个按钮控制音频音量大小。 4.实现在启动端通过“暂停”按钮控制音频暂停播放。 |
|||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||||
三、实验内容及步骤 |
|||||
1.在MusicAndVideoPlay中添加一个通过绑定事件播放音乐的按钮和一个设置音乐的菜单选项. 2.创建一个BindService 3.创建一个SettingMusic |
|||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
|||||
代码: MusicAndVideoPlay.java public class MusicAndVideoPlay extends Activity { private Button playSourceMusicbt, playLocalMusicbt, playMusicOnbangding,stopPlaybt, playVideobt, exitPlaybt; private MediaPlayer myPlayer1 = new MediaPlayer(); private VideoView video; /*private ReceiverVolum receiverVolum;*/ static BindService.MyBinder binder; private ServiceConnection conn=new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub System.out.println("Service is disconnected"); } @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { // TODO Auto-generated method stub System.out.println("Service Connected"); binder=(BindService.MyBinder) arg1; //创建连接获取IBinder } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.musicandvideo_layout); playSourceMusicbt = (Button) findViewById(R.id.playSourceMusic); playLocalMusicbt = (Button) findViewById(R.id.playLocalMusic); playMusicOnbangding= (Button) findViewById(R.id.playMusicOnbangding); stopPlaybt = (Button) findViewById(R.id.stopPlay); playVideobt = (Button) findViewById(R.id.playVideo); exitPlaybt = (Button) findViewById(R.id.exitPlay); video=(VideoView) findViewById(R.id.video); playSourceMusicbt.setOnClickListener(new MyClickListener()); playLocalMusicbt.setOnClickListener(new MyClickListener()); playMusicOnbangding.setOnClickListener(new MyClickListener()); stopPlaybt.setOnClickListener(new MyClickListener()); playVideobt.setOnClickListener(new MyClickListener()); exitPlaybt.setOnClickListener(new MyClickListener()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub // MenuInflater inflater=new MenuInflater(this); getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Intent intent; switch (item.getItemId()) { case R.id.musicSetting: Toast.makeText(MusicAndVideoPlay.this, "Service 的音乐声音值为:"+binder.getCurrentSount(), Toast.LENGTH_LONG).show(); Bundle bundle = new Bundle(); bundle.putInt("currentSount", binder.getCurrentSount()); //创建启动MainActivity的Intent intent=new Intent(this,SettingMusic.class); intent.putExtras(bundle); //添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); break; default: break; } return super.onOptionsItemSelected(item); } class MyClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.playSourceMusic: startService(new Intent(MusicAndVideoPlay.this, MusicService.class)); break; case R.id.playLocalMusic: try { if(myPlayer1.isPlaying()==true) { /*把 MediaPlayer重设*/ myPlayer1.reset(); } /*设定 MediaPlayer读取SDcard的档案*/ myPlayer1.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/nan.mp3"); myPlayer1.prepare(); /*把 MediaPlayer开始播放*/ myPlayer1.start(); }catch (Exception e) { // TODO: handle exception } break; case R.id.playMusicOnbangding: //绑定方式创建Service final Intent intent=new Intent(MusicAndVideoPlay.this,BindService.class); bindService(intent, conn, Service.BIND_AUTO_CREATE); break; case R.id.stopPlay: if(myPlayer1.isPlaying()==true) { /*把 MediaPlayer重设*/ myPlayer1.stop(); } break; case R.id.playVideo: File file=new File(Environment.getExternalStorageDirectory().getPath()+"/wat.avi"); MediaController mc=new MediaController(MusicAndVideoPlay.this); if(file.exists()){ video.setVideoPath(file.getAbsolutePath()); video.setMediaController(mc); video.requestFocus(); try{ video.start(); }catch(Exception e){ } video.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub Toast.makeText(MusicAndVideoPlay.this, "视频播放完毕!", Toast.LENGTH_SHORT).show(); } }); }else{ Toast.makeText(MusicAndVideoPlay.this, "要播放的视频文件不存在", Toast.LENGTH_SHORT).show(); } break; case R.id.exitPlay: break; default: break; } } } public BindService.MyBinder getBinder() { return binder; } public void setBinder(BindService.MyBinder binder) { this.binder = binder; } } BindService.java public class BindService extends Service { //系统使用MediaPlayer播放音乐,定义对象mp private MediaPlayer mp; private String TAG="Main"; //音量控制,初始化定义 private AudioManager mAudioManager ; private MyBinder binder=new MyBinder(); public class MyBinder extends Binder{ public int getCurrentSount(){ return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); } public void setCurrentSount(int v){ mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, v, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub System.out.println("Service is Binded"); return binder; } @Override public void onCreate() { super.onCreate(); //初始化MediaPlayer对象,准备播放音乐,音乐文件放在文件夹/res/raw中 mp=MediaPlayer.create(this,R.raw.nan); Log.i(TAG, "MusicService onCreate被运行"); //开始播放音乐 mp.start(); mAudioManager= (AudioManager)getSystemService(Context.AUDIO_SERVICE); Log.i(TAG, "MusicService onStartCommand被运行"); } @Override public void onDestroy() { super.onDestroy(); //停止播放音乐 mp.stop(); Log.i(TAG, "MusicService onDestroy被运行"); } } SettingMusic.java public class SettingMusic extends Activity { private SeekBar sBar; private AudioManager audioManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting_music); sBar=(SeekBar) findViewById(R.id.seekBar1); sBar.setProgress(getIntent().getExtras().getInt("currentSount")); Log.i("shezhimu", "shezhimu"); audioManager=(AudioManager) getSystemService(Context.AUDIO_SERVICE); sBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub MusicAndVideoPlay.binder.setCurrentSount(arg0.getProgress()); Toast.makeText(SettingMusic.this, "Service 的音乐声音被设置为:"+arg0.getProgress(), Toast.LENGTH_LONG).show(); } @Override public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { // TODO Auto-generated method stub } }); } } 运行结果:(截图) |
|||||
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
|||||
刚开始不是很懂如何通过绑定传递数据,后来自己在网上查阅了资料之后就懂了,才能把老师上课做出的部分功能实现.在最后一步通过移动滚动条来改变音乐的大小这个功能,我利用了静态变量来实现了,很有收获. |
|||||
实验评语 |
|
||||
实验成绩 |
|
指导教师签名: 年 月 日 |
|||