实验6 测试多媒体播放

实验报告

课程名称

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

实验日期

2016.4.15

实验项目名称

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

实验地点

S30010

实验类型

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

学  时

2

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

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

实验要求:

1.实现播放音频,音频播放控制;

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

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

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

(1)PC机

(2)操作系统:Windows XP

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

三、实验内容及步骤

1)新建工程

 2)修改布局文件main.xml

 

3)完善Activity类

 

/***

 * 这个类主要用来测试调用Andriod.media.MediaPlayer包里面的函数实现音乐播放的功能,

 * 分别播放了工程中资源文件中的音乐文件、本地文件系统中的文件,以及网络上的音乐;

 * 视频播放功能,使用了VideoView控件;

 * 代码中指定了本地文件系统中音频和视频文件的路径,在测试本程序前请按照代码中的路径和音频、视频文件的名称在手机中添加文件;

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

代码:

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:background="@drawable/yu"
    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="18dp"
        android:text="@string/title"
        android:textColor="@color/textcolor"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:textColor="@color/textcolor"
        android:text="@string/btn1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="14dp"
        android:textColor="@color/textcolor"
        android:text="@string/btn2" />

    <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:textColor="@color/textcolor"
        android:layout_marginTop="22dp"
        android:text="@string/btn3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="22dp"
        android:textColor="@color/textcolor"
        android:text="@string/btn4" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button5"
        android:layout_alignBottom="@+id/button5"
        android:layout_alignParentRight="true"
        android:textColor="@color/textcolor"
        android:text="@string/btn6" />

    <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_marginRight="16dp"
        android:layout_toLeftOf="@+id/button6"
        android:textColor="@color/textcolor"
        android:text="@string/btn5" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:textColor="@color/textcolor"
        android:text="@string/tv" />

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="550dp"
        android:layout_height="680dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/button4"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Mainactivity:

package com.example.mymediaplayer07;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
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.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {
private Button btn1,btn2,btn3,btn4,btn5,btn6;
private MediaPlayer mediaPlayer;
private File file,file2;
private TextView tv;
private VideoView video;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);
        btn3=(Button)findViewById(R.id.button3);
        btn4=(Button)findViewById(R.id.button4);
        btn5=(Button)findViewById(R.id.button5);
        btn6=(Button)findViewById(R.id.button6);
        tv=(TextView)findViewById(R.id.textView2);
        video =(VideoView)findViewById(R.id.videoView1);
        file2=new File("/sdcard/test.mp4");
        MediaController mc=new MediaController(MainActivity.this);
        file=new File(Environment .getExternalStorageDirectory().getPath()+"/yu.mp3");
        if(file.exists())
        {
        mediaPlayer=MediaPlayer.create(MainActivity.this,Uri.parse(file.getAbsolutePath()));
    
        }else {
            tv.setText("您要播放的音乐不存在");
        }
        btn1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mediaPlayer=MediaPlayer.create(MainActivity.this, R.raw .yu);
                mediaPlayer.start();
                tv.setText("正在播放音乐");
            }
        });
        btn2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource("sdcard/yu.mp3");
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    tv.setText("正在播放SD卡上的音乐");
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        });
        btn4.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mediaPlayer .stop();
                tv.setText("停止播放音乐");
            }
        });
        btn5.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                File file=new File("/sdcard/test.mp4");
                video.setVideoPath(file.getAbsolutePath());
                
                try{
                    video.start();
                } catch (Exception e){
                    e.printStackTrace();
                }
                tv.setText("播放视频中");
            }
        });
            
            
 
    }
    
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
    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;
    }

}

   运行结果:(截图)

 

 

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

    通过此次实验,我学会了如何实现播放项目的音频以及如何播放sd卡里面的音频和播放视频,加强了我对安卓编程的信心,收益良多。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           
原文地址:https://www.cnblogs.com/xkb07/p/5420048.html