实验六 在应用程序中播放音频和视频

实验报告

课程名称

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

实验日期

2016年4月15日

实验项目名称

在应用程序中播放音频和视频

实验地点

S30010

实验类型

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

学    时

2

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

1.目的:实现在应用程序中处理音频和视频。

2.要求:实现播放音频,音频播放控制;

实现播放视频,视频播放控制;

使用Service服务播放项目源文件中的音乐。

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

(1)PC机

(2)操作系统:Windows XP

(3)软件: Eclipse, JDK1.6,Android SDK,ADT

三、实验内容及步骤

1)新建工程

2)修改布局文件main.xml

3)完善Activity类

4)新建Service类,使用Service服务播放项目源文件中的音乐,实现后台继续能播放音频。

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

代码:

musicplayer的activity_main.xml的代码:

<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_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="14dp"

        android:text="@string/hello_world"

        android:textSize="50px"   />

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button2"

        android:layout_alignRight="@+id/textView1"

        android:layout_below="@+id/textView1"

        android:layout_marginTop="30dp"

        android:text="播放源文件中的音乐" />

 

    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button4"

        android:layout_alignRight="@+id/textView1"

        android:layout_below="@+id/button1"

        android:layout_marginTop="25dp"

        android:text="播放本地文件系统中的音乐" />

 

    <Button

        android:id="@+id/button3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button2"

        android:layout_alignRight="@+id/button1"

        android:layout_below="@+id/button2"

        android:layout_marginTop="20dp"

        android:text="播放网络上的音乐" />

 

    <Button

        android:id="@+id/button6"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBottom="@+id/button5"

        android:layout_alignParentRight="true"

        android:layout_marginRight="18dp"

        android:text="退出" />

 

    <Button

        android:id="@+id/button4"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/button3"

        android:layout_marginTop="20dp"

        android:layout_toLeftOf="@+id/button5"

        android:text="停止播放" />

 

    <Button

        android:id="@+id/button5"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBaseline="@+id/button4"

        android:layout_alignBottom="@+id/button4"

        android:layout_toLeftOf="@+id/button6"

        android:text="播放视频" />

 

    <TextView

        android:id="@+id/textView2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView1"

        android:layout_below="@+id/button4"

        android:layout_marginTop="42dp"

        android:text="TextView"   />

 

</RelativeLayout>

musicplayer的MainActivity.java的部分代码:

package   com.example.musicplayer;

import   java.io.File;

import   java.io.IOException;

import   com.example.musicplayer.MainActivity;

import   android.media.MediaPlayer;

import   android.net.Uri;

import   android.os.Bundle;

import   android.os.Environment;

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.TextView;

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://play.baidu.com/?__m=mboxCtrl.playSong&__a=73923261&__o=/song/104261||yyr_singleSong||yyr&fr=-1||music.baidu.com||yyr#";

   @Override

   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);

       exit=(Button)findViewById(R.id.button6);

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

//     if (file.exists()) {

//        mediaplayer=MediaPlayer.create(MainActivity.this,   Uri.parse(file.getAbsolutePath()));

//       

//     }else {

//        tv.setText("要播放的文件不存在!");

//     }

       source.setOnClickListener(new   OnClickListener(){

          @Override

          public void onClick(View arg0) {

              // TODO Auto-generated method   stub

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

              mediaplayer.start();

              tv.setText("正在播放音乐");

          }

         

       });

       stop.setOnClickListener(new   OnClickListener(){

          @Override

          public void onClick(View arg0) {

              // TODO Auto-generated method   stub

              if(mediaplayer.isPlaying()){

              mediaplayer.stop();

              tv.setText("停止播放音乐");

              }

          }

         

       });

       local.setOnClickListener(new   OnClickListener() {

         

          @SuppressLint("SdCardPath")   @Override

          public void onClick(View arg0) {

              // TODO Auto-generated method   stub

              try {

                 mediaplayer.reset();

                 Log.i("abBFKhkrhwu",   "vlgdangjdije");

                 mediaplayer.setDataSource("/sdcard/model.mp3");

                 //mediaplayer.create(MainActivity.this,   Uri.parse(file.getAbsolutePath()));

                 mediaplayer.prepare();

                 mediaplayer.start();

                 Log.i("addddddddddddddddddddddddddddddd",   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");

                 tv.setText("正在播放SD卡上的音乐");

              } 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();

          }

          tv.setText("正在播放网络上的音乐");

          Intent intent=new   Intent(Intent.ACTION_VIEW,Uri.parse(uri));

          startActivity(intent);

       }

          

       });

       exit.setOnClickListener(new   OnClickListener(){

       @Override

       public void onClick(View arg0) {

          // TODO Auto-generated method stub

          System.exit(0);

       }

          

       });

       video.setOnClickListener(new   OnClickListener(){

       @Override

       public void onClick(View arg0) {

          // TODO Auto-generated method stub

          Intent vi=new Intent();

          vi.setClass(MainActivity.this,   video.class);

          startActivity(vi);

          finish();

       }

          

       });

     

   }

    protected void onDestroy() {

          // TODO Auto-generated method stub

        if(mediaplayer!=null){

           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;

   }

}

运行结果:(截图)

1. musicplayer项目效果图:

                                                                            

           

    

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

通过这一次的实验,我基本掌握了如何去设置一个播放器的基本功能,在实验过程中遇到很多不理解和不懂得问题,通过书本的例题,我慢慢地解决一个又一个的问题,而且我也学到了不同路径的音乐,所用的方法也是不一样的,并且在实验过程中忘记了权限的问题,所以sd卡的内容播放不了,所以做实验也需要细心。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           
原文地址:https://www.cnblogs.com/xy1015/p/5415686.html