课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年4月15日 |
实验项目名称 |
数独游戏界面设计 |
实验地点 |
S3010 |
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
|
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||
【目的】 实现在应用程序中处理音频和视频。 【要求】
|
|||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||
三、实验内容及步骤 |
|||
1) 导入工程Sodoku 2) 修改布局文件main.xml 3) 完善Activity类 1.这个类主要用来测试调用Andriod.media.MediaPlayer包里面的函数实现音乐播放的功能; 2. 分别播放了工程中资源文件中的音乐文件、本地文件系统中的文件,以及网络上的音乐; 3. 视频播放功能,使用了VideoView控件; 4. 代码中指定了本地文件系统中音频和视频文件的路径,在测试本程序前请按照代码中的路径和音频、视频文件的名称在手机中添加文件; 4) 新建Service类,使用Service服务播放项目源文件中的音乐,实现后台继续能播放音频。 |
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)
代码:
MainActivity类:
package com.example.sukodu; import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.HONEYCOMB) public class MainActivity extends Activity { private Button exitbtn, aboutbtn, playbtn; private TextView tv1; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar(); actionBar.show(); ActionBar actionBar1 = getActionBar();// 获取ActionBar对象 actionBar1.setDisplayShowHomeEnabled(true);// 显示应用程序图标 actionBar1.setDisplayHomeAsUpEnabled(true);// 将应用程序图标转变为可点击图标,并添加一个返回箭头。 tv1 = (TextView) findViewById(R.id.textView1); Log.i("test", "333333"); final Intent intent = getIntent(); // 获取Intent对象 Bundle bundle = intent.getExtras(); // 获取传递的数据包 tv1.setText("你的名字是:" + bundle.getString("userName")); aboutbtn = (Button) findViewById(R.id.about_btn); exitbtn = (Button) findViewById(R.id.exit_btn); playbtn = (Button) findViewById(R.id.buttonPlayer); playbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, MyPlayer.class); startActivity(intent); } }); exitbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // startActivity(intent); setResult(0x1717, intent); Log.i("test", "444444"); finish(); } }); aboutbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent); // finish(); } }); } public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { /* * case android.R.id.home: // 创建启动MainActivity的Intent Intent intent = * new Intent(this, MainActivity.class); // * 添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); * startActivity(intent); break; */ case R.id.help: Intent intent1 = new Intent(this, Help.class); startActivity(intent1); // finish(); break; case R.id.about: Intent intent2 = new Intent(this, About.class); startActivity(intent2); // finish(); break; case R.id.newgame: Intent intent3 = new Intent(this, New_Game.class); startActivity(intent3); // finish(); break; default: break; } return super.onOptionsItemSelected(item); } /* * @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO * Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu); * return true; } * * @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO * Auto-generated method stub if (item.getItemId()==R.id.set) { Intent * intent=new Intent(); * intent.setClass(MainActivity.this,ShuduSettings.class); * startActivity(intent); finish(); * * } return true; } */ }
MyPlayer类
package com.example.sukodu; import java.io.File; import java.io.IOException; import java.net.URL; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MyPlayer extends Activity { // private MediaPlayer player; // MediaPlayer对象 private MediaPlayer OM; // MediaPlayer对象 private File file; // 要播放的音频文件 private TextView hint; // 声明显示提示信息的文本框 private MediaPlayer player = new MediaPlayer(); // private boolean sign; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button button1 = (Button) findViewById(R.id.PlayBtn); // 获取播放按钮 final Button button2 = (Button) findViewById(R.id.PauseBtn); // 获取“暂停/继续”按钮 final Button button3 = (Button) findViewById(R.id.StopBtn); // 获取“停止”按钮 final Button SrcBtn = (Button) findViewById(R.id.SrcBtn); // 获取播放按钮 final Button ServiceBtn = (Button) findViewById(R.id.serviveBtn); final Button OnlineBtn = (Button) findViewById(R.id.OnlineMusic); // 获取播放按钮 final Button VideoBtn = (Button) findViewById(R.id.VideoPlayer); // 获取“停止”按钮 hint = (TextView) findViewById(R.id.hint); // 获取用户显示提示信息的文本框 file = new File(Environment.getExternalStorageDirectory().getPath() + "/Who Let Me Red.mp3"); // 获取要播放的文件 /******************** 验证service ****************/ ServiceBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MyPlayer.this, PlayMusic.class); startActivity(intent); // 启动服务 } }); /******************** 播放视频 ****************/ VideoBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MyPlayer.this, VideoPlayer.class); startActivity(intent); } }); /******************** 播放网络上的的音乐 ****************/ OM = MediaPlayer .create(this, Uri.parse("http://play.baidu.com/?__m=mboxCtrl.playSong&__a=461315&__o=song/461315||playBtn&fr=-1||-1#")); OnlineBtn.setOnClickListener(new OnClickListener() { /*http://play.baidu.com/?_m=mBoxCtr.playSong&a=242078437&_o=song/242078437||playBt *//http://music.baidu.com/song/461315?pst=newindexsonglist * http://www/mingribook.com/sound/bg.mp3 */@Override public void onClick(View arg0) { // TODO Auto-generated method stub if (OM != null) { OM.start(); OM.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub hint.setText("已播放完毕"); } }); } else { hint.setText("网页打不开,播放路径改改吧!"); } } }); /******************** 播放源文件中的音乐 ****************/ SrcBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub player = MediaPlayer.create(MyPlayer.this, R.raw.nianlun); player.start(); hint.setText("正在播放源文件的音乐"); } }); /******************** 播放本地SD card 音乐 ****************/ file = new File(Environment.getExternalStorageDirectory().getPath() + "/Who Let Me Red.mp3"); if (file.exists()) { player = MediaPlayer .create(this, Uri.parse(file.getAbsolutePath())); } else { hint.setText("要播放的文件不存在!"); } button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub try { player.reset(); player.setDataSource(file.getAbsolutePath()); player.prepare(); player.start(); hint.setText("正在播放SD卡上的音乐"); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); // 为“播放”按钮添加单击事件监听器 // 为“暂停/继续”按钮添加单击事件监听器 button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (player.isPlaying()) { player.pause(); hint.setText("暂停播放音乐"); } else { player.start(); hint.setText("继续播放音乐"); } } }); // 为“停止”按钮添加单击事件监听器 button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { player.stop(); hint.setText("停止播放音乐"); } }); player.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub arg0.start(); } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub if (player.isPlaying()) { player.stop(); } player.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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. return super.onOptionsItemSelected(item); } }
PlayMusic类
package com.example.sukodu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class PlayMusic extends Activity { /** Called when the activity is first created. */ //Button BeginBtn = (Button) findViewById(R.id.begin); // 获取“开始”按钮 //Button FreezeBtn = (Button) findViewById(R.id.Freeze); // 获取“停止”按钮 //Button GoBtn = (Button) findViewById(R.id.GoButton); // 获取“跳转”按钮 Button BeginBtn,FreezeBtn,GoBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.service); findView(); BeginBtn.setOnClickListener(startlis); FreezeBtn.setOnClickListener(stoplis); Log.i("PlayMusic", "PlayMusic onCreate被运行"); GoBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // Intent intent=new Intent(Intent.ACTION_VIEW); // intent.setData(Uri.parse("http://www.baidu.com")); Intent intent=new Intent(PlayMusic.this, Surprise.class); startActivity(intent);; //finish(); } }); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i("PlayMusic", "PlayMusic onStart被运行"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.i("PlayMusic", "PlayMusic onRestart被运行"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i("PlayMusic", "PlayMusic onResume被运行"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i("PlayMusic", "PlayMusic onStop被运行"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i("PlayMusic", "PlayMusic onPause被运行"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i("PlayMusic", "PlayMusic onDestroy被运行"); } private OnClickListener startlis=new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub startService(new Intent(PlayMusic.this, MusicService.class)); //启动服务 } }; private OnClickListener stoplis=new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub stopService(new Intent(PlayMusic.this,MusicService.class)); //停止服务 } }; public void findView(){ BeginBtn=(Button)findViewById(R.id.begin); FreezeBtn=(Button)findViewById(R.id.Freeze); GoBtn=(Button)findViewById(R.id.GoButton); } }
MusicService类
package com.example.sukodu;
import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; public class MusicService extends Service { private MediaPlayer ms; private String TAG="Main"; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { //Service启动初始化 super.onCreate(); ms=MediaPlayer.create(this,R.raw.nianlun); ms.start(); // mp.setLooping(true); 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); } // @SuppressWarnings("deprecation") // @Override // public void onStart(Intent intent, int startId) { // super.onStart(intent, startId); // mp.start(); // } @Override public void onDestroy() { super.onDestroy(); ms.stop(); Log.i(TAG, "MusicService onDestroy被运行"); } }
VideoPlayer类
package com.example.sukodu; import java.io.File; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; public class VideoPlayer extends Activity { private VideoView video; // 声明VideoView对象 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.videoview); video = (VideoView) findViewById(R.id.video); // 获取VideoView组件 // video.setVideoPath("/data/mp4/happy.mp4"); //播放系统上的文件 File file = new File("/sdcard/Better When I'm Dancing.mp4"); // 获取SD卡上要播放的文件 MediaController mc = new MediaController(VideoPlayer.this); if (file.exists()) { // 判断要播放的视频文件是否存在 video.setVideoPath(file.getAbsolutePath()); // 指定要播放的视频 video.setMediaController(mc); // 设置VideoView与MediaController相关联 video.requestFocus(); // 让VideoView获得焦点 try { video.start(); // 开始播放视频 } catch (Exception e) { e.printStackTrace(); // 输出异常信息 } // 为VideoView添加完成事件监听器 video.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Toast.makeText(VideoPlayer.this, "视频播放完毕!", Toast.LENGTH_SHORT).show(); // 弹出消息提示框显示播放完毕 } }); } else { Toast.makeText(this, "要播放的视频文件不存在", Toast.LENGTH_SHORT).show(); // 弹出消息提示框提示文件不存在 } } }
Surprise类
package com.example.sukodu; import android.app.Activity; import android.os.Bundle; public class Surprise extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.suprise); } }
布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="单击“开始”按钮播放音频" /> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/PlayBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放本地文件系统中的音乐" /> </LinearLayout> <Button android:id="@+id/SrcBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放源文件的音乐" /> <Button android:id="@+id/OnlineMusic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放网络上的音乐" /> <Button android:id="@+id/VideoPlayer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放视频" /> <Button android:id="@+id/serviveBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证Service" /> <Button android:id="@+id/PauseBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="想暂停or继续就点我吧!" /> <Button android:id="@+id/StopBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout>
Service.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/begin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" /> <Button android:id="@+id/Freeze" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> <Button android:id="@+id/GoButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转有惊喜!" /> </LinearLayout>
surprise.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/surprise" /> </LinearLayout>
videoview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <VideoView android:id="@+id/video" android:background="@drawable/bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sukodu" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sukodu.MainActivity" android:label="@string/shudu" > </activity> <activity android:name="com.example.sukodu.PlayMusic" android:label="@string/Servicepm" > </activity> <activity android:name="com.example.sukodu.VideoPlayer" android:label="@string/VideoPlayer" > </activity> <activity android:name="com.example.sukodu.ShuduSettings" android:label="@string/shezhi" > </activity> <activity android:name="com.example.sukodu.LoginActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.sukodu.SecondActivity" android:label="@string/app_name" > </activity> <activity android:name="com.example.sukodu.About" android:label="@string/shudu" > </activity> <activity android:name="com.example.sukodu.Help" android:label="@string/shudu" > </activity> <activity android:name="com.example.sukodu.New_Game" android:label="@string/shudu" > </activity> <activity android:name="com.example.sukodu.MyPlayer" android:label="@string/Media" > </activity> <activity android:name="com.example.sukodu.Surprise" android:label="@string/Media" > </activity> <activity android:name="com.example.sukodu.MusicService" android:label="@string/Media" > </activity> <service android:name=".MusicService" > </service> </application> </manifest>
5) 完成设计后,界面运行如下
媒体播放器界面:
点击“媒体播放器”按钮后跳转到如下页面:
播放本地文件系统上(SD卡)的音乐:
播放源文件的音乐:
暂停播放音乐:
继续播放音乐:
停止播放音乐:
点击“播放网络上的音乐”按钮:(因为网址找不到所以访问不了)
点击“播放视频”按钮后视图如下:
点击“验证Service”按钮跳转到:
点击“跳转有惊喜”按钮跳到下面所示界面:
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
||
这次实验耗费了比较多的心血,通过不断修正和改进代码,无数次的运行让我觉得学到了很多相关的编程思想和相关知识。 好像对这门Android开发的课更有兴趣和信心学好。期间遇到了很多困难:譬如mediaplayer类如何使用,对象的初始化, 方法的定义以及调用方法,一些权限的设置,是在这次实验当中遇到的小困难并且通过不断调试和查找资料解决的。可能 因为权限的问题,暂时找不到合适的网址,导致播放网上的音乐被禁止,但是代码是没有错的;视频有音乐但没有画面, 因为模拟机不允许播放吧,但是在真机上可以。还有一种可能就是视频的频帧太高,高清,导致模拟机解析不出画面。 因此需要更换成低频帧的视频文件。这次作业学到了很多,我感觉到自己有进步,但是这个程序还有很多地方需要完善, 希望继续跟着老师,深入的学习,以及自己课外查找相关资料,继续改进程序,继续解决bugs的问题!
|
||
实验评语 |
|
|
实验成绩 |
|
指导教师签名: 年 月 日 |