实验报告
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年4月21日 |
实验项目名称 |
在应用程序中播放音频和视频 |
实验地点 |
S3010 |
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
2 |
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||
|
|||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||
三、实验内容及步骤 |
|||
1)新建工程; 2)修改布局文件main.xml; 3)完善Activity类。 |
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)
代码:
MainActivity:
package com.example.mediaplayer; import java.io.File; import java.io.IOException; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.MediaController; import android.widget.TextView; import android.widget.Toast; import android.widget.VideoView; public class MainActivity extends Activity { Button source,local,network,stop,video,exit; private MediaPlayer mediaplayer=new MediaPlayer(); private File file; private TextView tv; private String uri="http://y.qq.com/#type=song&mid=003e0OzZ0wFBRX&tpl=yqq_song_detail"; private VideoView videoView; MediaController mc; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.textView1); source=(Button)findViewById(R.id.button1); local=(Button)findViewById(R.id.button2); network=(Button)findViewById(R.id.button3); stop=(Button)findViewById(R.id.button4); video=(Button)findViewById(R.id.button5); videoView=(VideoView)findViewById(R.id.videoView1); File file=new File("/sdcard/bluevird.mp4"); mc=new MediaController(MainActivity.this); source.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { // TODO Auto-generated method stub mediaplayer=MediaPlayer.create(MainActivity.this, R.raw.what); mediaplayer.start(); Toast.makeText(MainActivity.this,"playing the music",Toast.LENGTH_SHORT).show(); } }); stop.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { // TODO Auto-generated method stub if(mediaplayer.isPlaying()){ mediaplayer.stop(); Toast.makeText(MainActivity.this,"stop",Toast.LENGTH_SHORT).show(); } } }); local.setOnClickListener(new OnClickListener() { @SuppressLint("SdCardPath") @Override public void onClick(View arg0) { // TODO Auto-generated method stub try { mediaplayer.reset(); mediaplayer.setDataSource("/sdcard/Louis Armstrong - What A Wonderful World.mp3"); mediaplayer.prepare(); mediaplayer.start(); Toast.makeText(MainActivity.this,"playing the sdcard music",Toast.LENGTH_SHORT).show(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); network.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub mediaplayer.reset(); try{ mediaplayer.setDataSource(uri); }catch(IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(MainActivity.this,"network music",Toast.LENGTH_SHORT).show(); Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(uri)); startActivity(intent); } }); video.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub File file=new File("/sdcard/bluebird.mp4"); videoView.setVideoPath(file.getAbsolutePath()); videoView.setMediaController(mc); try{ videoView.start(); } catch (Exception e){ e.printStackTrace(); } Toast.makeText(MainActivity.this,"playing the video",Toast.LENGTH_SHORT).show(); } }); } @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; } }
Main layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="mediaplayertest" android:textSize="35px" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:text="源文件音乐" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:text="本地音乐" /> <Button android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" android:text="网络音乐" /> <Button android:id="@+id/button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button3" android:layout_below="@+id/button3" android:text="停止" /> <Button android:id="@+id/button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button4" android:layout_below="@+id/button4" android:text="视频" /> <VideoView android:id="@+id/videoView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/button5" /> </RelativeLayout>
运行结果:(截图)
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)
通过这次实验知道了如何往模拟器中的sd卡放入文件以及如何从sd卡里使用。