实验6 201306114104彭得源

实验报告

课程名称

基于Android平台移动互联网开发

实验日期

16/04/15

实验项目名称

实验6

实验地点

S3010

实验类型

□验证型    √设计型    □综合型

学  时

 

一、实验目的及要求(本实验所涉及并要求掌握的知识点)

实现在应用程序中处理音频和视频。

 

二、实验环境(本实验所使用的硬件设备和相关软件)

Android资源管理机制。

 

三、实验内容及步骤

1)新建工程

 2)修改布局文件main.xml

3)完善Activity类

 

 

四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)

 

代码:实验代码

package com.example.mediaplay;

 

import java.io.File;

import java.io.IOException;

 

 

import android.media.MediaPlayer;

import android.os.Bundle;

import android.os.Environment;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.MediaController;

import android.widget.VideoView;

 

public class MainActivity extends Activity {

private Button btnStartResours,btnStartLocal,btnStartnet,btnStop,btnQuit,btnplayvideo;

private VideoView video;

public MediaPlayer mediaplayer= new MediaPlayer();     

 

private File file,filevideo;

@Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

                   btnStartResours=(Button)findViewById(R.id.button1);

                   btnStartLocal=(Button)findViewById(R.id.button2);

                   btnStartnet=(Button)findViewById(R.id.button3);

                   btnStop=(Button)findViewById(R.id.button4);                

                   btnQuit=(Button)findViewById(R.id.button5);

                   btnplayvideo=(Button)findViewById(R.id.playvideobtn);

                  

                   video=(VideoView)findViewById(R.id.videoView1);

                   file = new File(Environment.getExternalStorageDirectory().getPath()+"/nan.mp3");

                   filevideo = new File(Environment.getExternalStorageDirectory().getPath()+"/videoviewdemo.mp4");

                  

                  

                   btnStartResours.setOnClickListener(new OnClickListener() {

                           

                            @Override

                            public void onClick(View arg0) {

                                     // TODO Auto-generated method stub

                                    

                                    

                                     mediaplayer=MediaPlayer.create(MainActivity.this, R.raw.nan);

                                     mediaplayer.start();

                            }

                   });

                   btnStartLocal.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 Auto-generated catch block

                                               e.printStackTrace();

                                     } catch (SecurityException e) {

                                               // TODO Auto-generated catch block

                                               e.printStackTrace();

                                     } catch (IllegalStateException e) {

                                               // TODO Auto-generated catch block

                                               e.printStackTrace();

                                     } catch (IOException e) {

                                               // TODO Auto-generated catch block

                                               e.printStackTrace();

                                     }

                                    

                            }

                   });

                   btnStartnet.setOnClickListener(new OnClickListener() {

                           

                            @Override

                            public void onClick(View arg0) {

                                     // TODO Auto-generated method stub

                                     file = new File("http://bd.kuwo.cn/yinyue/1043947?from=baidu");

                                     try {

                                               mediaplayer.setDataSource(file.getAbsolutePath());

                                               mediaplayer.prepare();

                                               mediaplayer.start();

                                     } catch (IllegalArgumentException e) {

                                               // TODO Auto-generated catch block

                                               e.printStackTrace();

                                     } catch (SecurityException e) {

                                               // TODO Auto-generated catch block

                                               e.printStackTrace();

                                     } catch (IllegalStateException e) {

                                               // TODO Auto-generated catch block

                                               e.printStackTrace();

                                     } catch (IOException e) {

                                               // TODO Auto-generated catch block

                                               e.printStackTrace();

                                     }

                            }

                   });

                   btnStop.setOnClickListener(new OnClickListener() {

                           

                            @Override

                            public void onClick(View arg0) {

                                     // TODO Auto-generated method stub

                                     if(mediaplayer.isPlaying()){

                                               mediaplayer.stop();

                                     }

                                    

                            }

                   });

                   btnQuit.setOnClickListener(new OnClickListener() {

                           

                            @Override

                            public void onClick(View arg0) {

                                     // TODO Auto-generated method stub

                                     onDestroy();

                                     finish();

                            }

                   });

                   MediaController mc = new MediaController(MainActivity.this);

                   if(file.exists()){

                            video.setVideoPath(filevideo.getAbsolutePath());

                            video.setMediaController(mc);

                            video.requestFocus();

                            btnplayvideo.setOnClickListener(new OnClickListener() {

                                    

                                     @Override

                                     public void onClick(View arg0) {

                                               // TODO Auto-generated method stub

                                               video.start();

                                              

                                     }

                            });

                           

                   }

                  

         }

@Override

         protected void onDestroy() {

                   // TODO Auto-generated method stub

                   mediaplayer.stop();

                   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;

         }

 

}

 

 

运行结果:(截图):

 

五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)

学会了播放视频和音乐。

喜欢下次老师可以一边讲我们一边做,老师把演示窗口化,我们就可以跟着老师的步骤一边听一边做了。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           
posted on 2016-04-29 09:18  04彭得源  阅读(185)  评论(0编辑  收藏  举报