android开发音乐盒的实验报告

《开发一个安卓软件报告》报告

设   计   题  目:  用安卓开发单机音乐盒       

                                                                                                                                                

学   院  名   称:    电子与信息工程学院      

专            业:     计算机科学与技术       

班            级:         计科091            

姓            名:***    学  号 03      

指   导   教  师:        ***              

定稿日期: 20##年12月 26日

一、    设计任务与要求

1、设计目标:

(1)了解Java编程的基础知识和数据库知识

(2) 掌握用android开发应用软件的的一般步骤和方法。

(3) 能够利用所学的基本知识,设计一个完整的音乐播放盒,还可以实现后台播放声音,并且在播放过程中可以通过前台的按钮控制声音的播放、暂停与关闭。

(4)在以上基础上再加一个界面,能够实现跳转,出现一个歌词界面。

2、课程设计的要求:

1、 用Service组件、BroadcastReceiver组件、Intent、菜单对话框来完成音乐的播放等操作。

2、 提交设计报告一份(需求分析、设计的基本思路、设计的步骤及主要代码、心得体会)。

二、系统分析

1需求分析

随着android手机的普及和一些小游戏的流行,android开发也日益high了。现在,大家也都是用手机听音乐了,一个绚丽的、酷酷的音乐盒就可以成为我们炫耀的资本了。为了符合更多人的喜欢,设计的界面要清晰、美观、大方、并且操作方便。所以我设计的界面也一定要清晰、美观、大方、并且操作方便。

2、模块分析

(1)在layout文件中增加一个歌词的界面songwords.xml并画好界面,还要在mail.xml中画好界面。

(2)在raw文件中放入歌曲。

<string name="myTextView1">我爱他</string>

 <string name="myTextView2">叮当</string>

 <string name="swords">歌词</string>   

 <string name="words">他的轻狂留在 某一节车厢   地下铁里的风 比回忆还重 

   ···省部分歌词··· 

若那一刻重来 我不哭   让他知道我可以 很好</string>  

(3)在values文件中的string中添加所要用到的字符串。

(4)在src文件中的文件中的Sample_9_3.java中定义一些按钮和转换的方法,在MyService.java中实现了按钮,在songwords.java中实现了歌词那个界面的功能。

三、系统设置

1、功能设计

这个播放器可以实现播放,暂停,停止和歌词界面跳转功能。在刚打开音乐盒的时候,是播放状态,按一下暂停按钮,音乐停止播放,且暂停按钮的图像变为播放按钮的图像;此时再按一下播放按钮,音乐继续;无论音乐在播放状态还是暂停状态,只要按一下停止按钮,音乐就会被关闭;当音乐在播放时,按歌词按钮,就会跳转到当前播放的歌的歌词界面,且音乐继续在播放;如果退出音乐盒这个界面,音乐也还是再继续的。而且在还没有跳转的时候,按菜单栏就会出现退出前台的按钮了。

2、界面设计

第一个界面总体分为上、下两层,第一层主要分三个部分,把播放按钮和暂停按钮重叠的放在第一部分,把停止按钮放在第二部分,第三部分又划分成上、下两个部分,上面放歌曲的名称,下面放歌曲的演唱者;第二层只有一个按钮,跳转到歌词界面的功能。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content">

    <ImageButton

    android:id="@+id/start"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:src="@drawable/png2"/>

    <ImageButton

    android:id="@+id/stop"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:src="@drawable/png1"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

        <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="25px"

        android:textColor="#ffffff"

        android:ellipsize="marquee"

        android:layout_weight="1"

        android:marqueeRepeatLimit="marquee_forever"

        android:text="@string/myTextView1"/>

        <TextView

        android:id="@+id/textView2"

        android:textSize="15px"

        android:gravity="center_vertical"

        android:layout_weight="1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/myTextView2"/>      

    </LinearLayout>

    </LinearLayout>

   <Button

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="@string/swords"   

       android:id="@+id/swords"></Button>

</LinearLayout>

第二个界面也分为上、下两层,第一层就是用来放歌词的;第二层有一个返回按钮,可以返回到第一个界面。

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

<TextView

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="@string/words"

  android:textSize="15px"

  android:textColor="#FF00FF"

  android:id="@+id/words">

  </TextView>

<Button

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="返回"

  android:id="@+id/back"

  android:onClick="@string/swords">

  </Button>

最终的效果是:

四、个人总结

这个音乐盒虽然比较简单,但是是我自己用这个学期所学到的知识来完成的,有我自己的心血,让自己觉得自己还不错。在做实验的过程也遇到了问题,毕竟所学的知识不是很深,问了在暑假所过的同学很多问题,终于完成了。

在做音乐盒的过程中,先前想得很简单,只要它能播放、暂停、停止就可以了。后来想想要是可以看到歌词就好了,所以才加的歌词那个界,还把字体设置成了自己喜欢的枚红色。本来还想在歌词按钮的下面再加一个列表,来显示所有的歌曲名及其演唱者,但是自己的知识还是太少了,没能把它实现。不过还是会再接再砺的。

五、附录:代码

4、修改Music.java源代码:

publicclass Music extends Activity implements OnClickListener{

    ImageButton start;//播放、暂停按钮

    ImageButton stop;//停止按钮

    private Button swords;

    ActivityReceiver activityReceiver;

    int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停

    /** Called when the activity is first created. */

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {//重写的onCreate方法

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);//设置当前的用户界面

       start = (ImageButton) this.findViewById(R.id.start);//得到start的引用

        stop = (ImageButton) this.findViewById(R.id.stop);//得到stop按钮的引用

        swords = (Button) this.findViewById(R.id.swords);

        start.setOnClickListener(this);//为按钮添加监听

        stop.setOnClickListener(this);//为按钮添加监听

        swords.setOnClickListener(this);        activityReceiver = new ActivityReceiver();//创建BroadcastReceiver

        IntentFilter filter = new IntentFilter();//创建IntentFilter过滤器

        filter.addAction("wyf.ytl.update");//添加Action

        registerReceiver(activityReceiver, filter);//注册监听

        Intent intent = new Intent(this, MyService.class);//创建Intent

        startService(intent);//启动后台Service

    }

    publicclass ActivityReceiver extends BroadcastReceiver{//自定义的BroadcastReceiver

       @Override

       publicvoid onReceive(Context context, Intent intent) {//重写的onReceive方法

           // TODO Auto-generated method stub

           int update = intent.getIntExtra("update", -1);//得到intent中的数据

           switch(update){//分支判断

           case 1://没有声音播放

              status = 1; //设置当前状态

              break;

           case 2://正在播放声音

              start.setImageResource(R.drawable.png3);//更换图片

              status = 2; //设置当前状态

              break;

           case 3://暂停中

              start.setImageResource(R.drawable.png2);//更换图片

              status = 3; //设置当前状态

              break;

           }

       }

    }

    @Override

    publicvoid onClick(View v) {//接口中的方法

       // TODO Auto-generated method stub

       Intent intent = new Intent("wyf.ytl.control");//创建Intent

       switch(v.getId()){//分支判断

       case R.id.start://按下播放、暂停按钮

           intent.putExtra("ACTION", 1);//存放数据

           sendBroadcast(intent);//发送广播

           break;

       case R.id.stop://按下停止按钮

           intent.putExtra("ACTION", 2);//存放数据

           sendBroadcast(intent);//发送广播

           break;

case R.id.swords:

           Intent intent2=new Intent();

           intent2.setClass(this, Songwords.class);

           startActivity(intent2);

       }

    }

    @Override

    protectedvoid onDestroy() {//释放时被调用

       // TODO Auto-generated method stub

       super.onDestroy();

        Intent intent = new Intent(this, MyService.class);//创建Intent

        stopService(intent);//停止后台的Service

    }

    @Override

    publicboolean onCreateOptionsMenu(Menu menu){//弹出菜单

       menu.add(0,Menu.FIRST,0,"退出")

           .setIcon(R.drawable.ic_menu_delete);//设置图标

       returntrue;

    }

    @Override

    publicboolean onOptionsItemSelected(MenuItem item){//选择的菜单项

       switch(item.getItemId()){//分支判断

       case Menu.FIRST:

           showDialog(1);//显示对话框

           break;

       }

       //将来可在此进行扩展

       returnfalse;

    }

    @Override

    protected Dialog onCreateDialog(int id){//创建对话框

       switch(id){//判断

       case 1:

           returnnew AlertDialog.Builder(this)

              .setTitle("您确定退出?")

              .setPositiveButton("确定", new android.content.DialogInterface.OnClickListener(){

                  @Override

                  publicvoid onClick(DialogInterface dialog, int which) {

                     // TODO Auto-generated method stub

                     System.exit(0);//直接退出

                  }

              })

              .setNegativeButton("取消", null)//取消按钮

              .create();

       default:

           returnnull;

       }

    }

}

5、修改Myservice.java源代码:

package wyf.ytl;

publicclass MyService extends Service{

    MediaPlayer mp;

    ServiceReceiver serviceReceiver;

    int status = 1;//当前的状态,1没有声音播放 ,2 正在播放声音,3暂停

    @Override

    public IBinder onBind(Intent intent) {//重写的onBind方法

       // TODO Auto-generated method stub

       returnnull;

    }

    @Override

    publicvoid onCreate() {//重写的onCreate方法,会在Service创建时被调用

       // TODO Auto-generated method stub    

       status = 1;

       serviceReceiver = new ServiceReceiver();//创建BroadcastReceiver

       IntentFilter filter = new IntentFilter();//创建过滤器

       filter.addAction("wyf.ytl.control");//添加Action

       registerReceiver(serviceReceiver, filter);//注册BroadcastReceiver

       super.onCreate();

    }

    @Override

    publicvoid onDestroy() {//重写的onDestroy方法

       // TODO Auto-generated method stub

       unregisterReceiver(serviceReceiver);//取消注册

       super.onDestroy();

    }

    publicclass ServiceReceiver extends BroadcastReceiver{//自定义BroadcastReceiver

       @Override

       publicvoid onReceive(Context context, Intent intent) {//重写的响应方法

           // TODO Auto-generated method stub

           int action = intent.getIntExtra("ACTION", -1);//得带需要的数据

           switch(action){

           case 1://播放或暂停声音

              if(status == 1){//当前没有声音播放

                  mp = MediaPlayer.create(context, R.raw.nx);

                  status = 2;

                  Intent sendIntent = new Intent("wyf.ytl.update");

                  sendIntent.putExtra("update", 2);

                  sendBroadcast(sendIntent);

                  mp.start();

              }

              elseif(status == 2){//正在播放声音

                  mp.pause();   //停止

                  status = 3;//改变状态

                  Intent sendIntent = new Intent("wyf.ytl.update");

                  sendIntent.putExtra("update", 3);//存放数据

                  sendBroadcast(sendIntent);//发送广播

              }

              elseif(status == 3){//暂停中

                  mp.start();//播放声音

                  status = 2;//改变状态

                  Intent sendIntent = new Intent("wyf.ytl.update");

                  sendIntent.putExtra("update", 2);//存放数据

                  sendBroadcast(sendIntent);//发送广播

              }

              break;

           case 2://停止声音

              if(status == 2 || status == 3){//播放中或暂停中

                  mp.stop();//停止播放

                  status = 1;//改变状态

                  Intent sendIntent = new Intent("wyf.ytl.update");

                  sendIntent.putExtra("update", 1);//存放数据

                  sendBroadcast(sendIntent);//发送广播

              }

           }

       }

    }

}

6、增加Songwords.java的源代码:

publicclass Songwords extends Activity implements OnClickListener{

    private TextView  view_words;

    private Button back;

    privatevoid findViews(){

        back = (Button) findViewById(R.id.back);

    view_words = (TextView) findViewById(R.id.words);

    }

    @Override

    protectedvoid onCreate(Bundle savedInstanceState) {

       // TODO Auto-generated method stub

       super.onCreate(savedInstanceState);

       setContentView(R.layout.songwords);

       findViews();

       showSongwords();

       Intent intent =getIntent();

    }

    privatevoid showSongwords(){

       Bundle receive = this.getIntent().getExtras();

       view_words.setText(R.string.words);

       back.setOnClickListener(this);

      

    }

    @Override

    publicvoid onClick(View v) {

       Intent intent=new Intent();

       intent.setClass(this, Sample_9_3.class);

       startActivity(intent);

    } 

}

相关推荐