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


    实验报告

    课程名称

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

    实验日期

    4月15日

    实验项目名称

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

    实验地点

    S3002

    实验类型

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

    学  时

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

    实现在应用程序中处理音频和视频。

    【要求】

    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服务播放项目源文件中的音乐,实现后台继续能播放音频。

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

    代码:

    1.MainActivity

    package com.example.mediapiayer;

    import java.io.File;

    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.content.Intent;

    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 {

    public MediaPlayer myPlayer1 = new MediaPlayer();

    private Button bt1, bt2, bt3, bt4, bt5, bt6;

    private TextView mTextView1;

    private String uri = "http://bd.kuwo.cn/yinyue/2324240";

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mTextView1 = (TextView) findViewById(R.id.textView1);

    bt1 = (Button) findViewById(R.id.button1);

    bt2 = (Button) findViewById(R.id.button2);

    bt3 = (Button) findViewById(R.id.button3);

    bt4 = (Button) findViewById(R.id.button4);

    bt5 = (Button) findViewById(R.id.button5);

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

    bt1.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

    // TODO Auto-generated method stub

    try {

    if (myPlayer1.isPlaying() == true) {

    myPlayer1.reset();

    }

    myPlayer1 = MediaPlayer.create(MainActivity.this,

    R.raw.song);

    myPlayer1.start();

    mTextView1.setText(R.string.str_start);

    } catch (IllegalStateException e) {

    mTextView1.setText(R.string.str_stop);

    e.printStackTrace();

    }

    }

    });

    bt2.setOnClickListener(new OnClickListener() {

    String sdCard = Environment.getExternalStorageDirectory().getPath();

    @Override

    public void onClick(View arg0) {

    // TODO Auto-generated method stub

    try {

    if (myPlayer1.isPlaying() == true) {

    myPlayer1.reset();

    }

    myPlayer1.setDataSource(sdCard + File.separator

    + "F.I.R. - yueyawan.mp3");

    myPlayer1.prepare();

    myPlayer1.start();

    mTextView1.setText(R.string.str_tishi);

    } catch (IllegalStateException e) {

    mTextView1.setText(R.string.str_stop);

    e.printStackTrace();

    } catch (IllegalArgumentException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (SecurityException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    });

    bt3.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

    // TODO Auto-generated method stub

    myPlayer1.reset();

    try {

    myPlayer1.setDataSource(uri);

    }

    catch (IllegalArgumentException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (SecurityException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

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

    startActivity(intent);

    mTextView1.setText(R.string.str_wangluo);

    }

    });

    bt4.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

    // TODO Auto-generated method stub

    myPlayer1.stop();

    mTextView1.setText(R.string.str_finished);

    }

    });

    bt5.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

    // TODO Auto-generated method stub

    }

    });

    bt6.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

    // TODO Auto-generated method stub

    myPlayer1.stop();

    mTextView1.setText(R.string.str_jieshu);

    }

    });

    }

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

    }

    }

    2.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"

        android:background="@drawable/u311593540665946679fm15gp0"

        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:text="测试多媒体播放" />

        <Button

            android:id="@+id/button1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignLeft="@+id/button2"

            android:layout_alignRight="@+id/button2"

            android:layout_below="@+id/textView1"

            android:layout_marginTop="26dp"

            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/button2"

            android:layout_below="@+id/button2"

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

        <Button

            android:id="@+id/button2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_below="@+id/button1"

            android:layout_centerHorizontal="true"

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

        <Button

            android:id="@+id/button4"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignBaseline="@+id/button5"

            android:layout_alignBottom="@+id/button5"

            android:layout_alignLeft="@+id/button3"

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

        <Button

            android:id="@+id/button5"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_below="@+id/button3"

            android:layout_toRightOf="@+id/button4"

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

        <Button

            android:id="@+id/button6"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_below="@+id/button3"

            android:layout_toRightOf="@+id/button5"

            android:text="退出" />

    </RelativeLayout>

    运行结果:(截图)

    1.

     

    2.

     

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

    通过实验学会了Android通过调用Andriod.media.MediaPlayer包里面的函数来控制媒体文件的播放,实现选文件、本地SD卡和网络上进行音频的播放。加深了对安卓开发的理解。

    实验评语

     

    实验成绩

     

    指导教师签名:                    

  • 相关阅读:
    【BZOJ3190】[JLOI2013]赛车 单调栈+几何
    【BZOJ2738】矩阵乘法 整体二分
    PR 批量导入
    JAVA 水果机游戏及编码
    sap 图标查看
    ABAP 给动态变量赋值
    abap 数字移动小游戏
    FI模块与SD、MM的接口配置方法
    信用控制增强
    SAP-财务知识点
  • 原文地址:https://www.cnblogs.com/badgood/p/5419345.html
Copyright © 2020-2023  润新知