一、实验目的及要求(本实验所涉及并要求掌握的知识点)
1.实现在应用程序中处理音频和视频。
2.实现播放音频,音频播放控制;
3. 实现播放视频,视频播放控制;
4. 使用Service服务播放项目源文件中的音乐。
二、实验环境(本实验所使用的硬件设备和相关软件)
(1)PC机
(2)操作系统:Windows XP
(3)软件: Eclipse, JDK1.6,Android SDK,ADT
三、实验内容及步骤
1.新建一个名为MediaPlayer的工程
2.修改布局文件main.xml
3.完善Activity类
4. 新建Service类,使用Service服务播放项目源文件中的音乐,实现后台继续能播放音频。
四、实验结果
代码: MainActivity.java类: public class MainActivity extends Activity { private MediaPlayer mediaPlayer=new MediaPlayer(); private TextView tView; private Button startSourceBtn,startLocalBtn,startInternetBtn,startVideoBtn,stopBtn,pauseBtn,exitBtn; private File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tView=(TextView)findViewById(R.id.textView1); startSourceBtn=(Button)findViewById(R.id.startSourceBtn); startLocalBtn=(Button)findViewById(R.id.startLocalBtn); startInternetBtn=(Button)findViewById(R.id.startInternetBtn); startVideoBtn=(Button)findViewById(R.id.startVidioBtn); stopBtn=(Button)findViewById(R.id.stopBtn); pauseBtn=(Button)findViewById(R.id.pauseBtn); exitBtn=(Button)findViewById(R.id.exitBtn); file=new File(Environment.getExternalStorageDirectory().getPath()+"/nan.mp3"); if(file.exists()){ mediaPlayer=MediaPlayer.create(MainActivity.this, Uri.parse(file.getAbsolutePath())); }else{ Toast.makeText(MainActivity.this, "文件不存在!", Toast.LENGTH_LONG).show(); } startSourceBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mediaPlayer=MediaPlayer.create(MainActivity.this, R.raw.nan); mediaPlayer.start(); } }); startLocalBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub mediaPlayer.reset(); try{ mediaPlayer.setDataSource(file.getAbsolutePath()); mediaPlayer.prepare(); mediaPlayer.start(); }catch (IllegalArgumentException e) { // TODO: handle exception e.printStackTrace(); }catch (SecurityException e) { // TODO: handle exception e.printStackTrace(); }catch (IllegalStateException e) { // TODO: handle exception e.printStackTrace(); }catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } }); startInternetBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub } }); startVideoBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClass(MainActivity.this, VideoActivity.class); startActivity(intent); } }); stopBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); } } }); pauseBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); }else { mediaPlayer.start(); } } }); exitBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(mediaPlayer.isPlaying()){ mediaPlayer.release(); } } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); } mediaPlayer.release(); super.onDestroy(); } @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; } } VideoActivity,java类: public class VideoActivity extends Activity{ private VideoView video; private File file; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.video); video=(VideoView)findViewById(R.id.video); file=new File("/"); MediaController mc=new MediaController(VideoActivity.this); if(file.exists()){ video.setVideoPath(file.getAbsolutePath()); video .setMediaController(mc); video.requestFocus(); try{ video.start(); }catch (Exception e){ e.printStackTrace(); } video.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub Toast.makeText(VideoActivity.this, "视频播放完毕!", Toast.LENGTH_SHORT).show(); } }); }else{ Toast.makeText(this, "要播放的视频不存在!", Toast.LENGTH_SHORT).show(); } } }
运行结果:(截图)
心得体会:实验过程中诸多坎坷,代码上出现了混乱,后来细心修改才能运行。虚拟机开启上也出了大问题,不知道为什么,总会提示找不到合适的虚拟机,然后手动选一个还是运行不了。通过别人的PC完成实验结果截图。