实验报告
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016.4.15 |
||
实验项目名称 |
实验6 在应用程序中播放音频和视频 |
实验地点 |
S3010 |
||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
2 |
||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||
|
|||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||||
三、实验内容及步骤 |
|||||
|
|||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
|||||
代码: // TODO Auto-generated method stub try{ if(myPlayer1.isPlaying()==true) { myPlayer1.reset(); } myPlayer1=MediaPlayer.create(MainActivity.this,R.raw.song1 ); myPlayer1.start(); Toast.makeText(MainActivity.this, "开始播放源文件",Toast.LENGTH_LONG).show(); } catch (IllegalStateException e) { Toast.makeText(MainActivity.this, "播放源失败",Toast.LENGTH_LONG).show(); } } }); btn2Local=(Button) findViewById(R.id.buttonLocal); btn2Local.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String sdCard=Environment.getExternalStorageDirectory().getPath(); try{ if(myPlayer1.isPlaying()==true) { myPlayer1.reset(); } myPlayer1.setDataSource(sdCard+File.separator+"song2.mp3"); myPlayer1.prepare(); myPlayer1.start(); Toast.makeText(MainActivity.this, "开始播放SD卡文件",Toast.LENGTH_LONG).show(); } catch (IllegalStateException e) { Toast.makeText(MainActivity.this, "播放SD卡文件失败",Toast.LENGTH_LONG).show(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btn3Inter=(Button) findViewById(R.id.buttonInternet); btn3Inter.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub try{ if(myPlayer1.isPlaying()==true) { myPlayer1.reset(); } myPlayer1.setDataSource("http://www.yy521.com/2012zuixingequ/plays/wangluogequ/index21274.html"); myPlayer1.start(); Toast.makeText(MainActivity.this, "开始播放网络音乐",Toast.LENGTH_LONG).show(); } catch (IllegalStateException e) { Toast.makeText(MainActivity.this, "播放网络音乐失败",Toast.LENGTH_LONG).show(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btnstarMedia=(Button) findViewById(R.id.buttonStarMedia); btnstarMedia.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub } }); btnStop=(Button) findViewById(R.id.buttonStop); btnStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub myPlayer1.stop(); } }); btnExit=(Button) findViewById(R.id.buttonExit); btnExit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub System.exit(0); } }); btnservice=(Button) findViewById(R.id.MusicServiceButton); btnservice.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub startService(new Intent(MainActivity.this,MusicService.class)); } }); btnserStop=(Button) findViewById(R.id.MusicServiceStop); btnserStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub stopService(new Intent(MainActivity.this,MusicService.class)); } }); btnstarMedia=(Button) findViewById(R.id.buttonStarMedia); btnstarMedia.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent1=new Intent(); intent1.setClass(MainActivity.this, VideoActivity.class); startActivity(intent1); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } public class MusicService extends Service{ private MediaPlayer mp; private String TAG="Main"; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); mp=MediaPlayer.create(this, R.raw.song2); Toast.makeText(this, "Service开始",Toast.LENGTH_LONG).show(); Log.i(TAG, "MusicService onCreate被运行"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub mp.start(); Log.i(TAG, "MusicService onStartCommand被运行"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub mp.stop(); super.onDestroy(); Log.i(TAG, "MusicService onDestroy被运行"); } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//去掉title // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //设置全屏 setContentView(R.layout.video); video=(VideoView) findViewById(R.id.videoView1); MediaController mc=new MediaController(this); video.setMediaController(mc); btnGet=(Button) findViewById(R.id.buttonGETvideo); btnGet.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub video.setVideoURI(Uri.parse("/storage/sdcard/video1.mp4")); } }); btnStar=(Button) findViewById(R.id.buttonStar); btnStar.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub video.start(); } }); btnstop=(Button) findViewById(R.id.buttonStop); btnstop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub video.pause(); } }); } } 运行结果:(截图)
|
|||||
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
|||||
这个实验相对之前的实验来说比较难,还是要通过网络和请教同学才勉强完成了这次的实验,特别是实现播放视频,到最后还是没有实现。 |
|||||
实验评语 |
|
||||
实验成绩 |
|
指导教师签名: 年 月 日 |
|||