实验报告

实验报告

课程名称

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

实验日期

2016年4月15日

实验项目名称

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

实验地点

S30010

实验类型

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

学    时

 

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

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

2、实现播放音频,音频播放控制;实现播放视频,视频播放控制;

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

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

(1)PC机

(2)操作系统:Windows XP

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

三、实验内容及步骤

1、在Sudoku项目中修改布局文件activity_main.xml,添加必要的控件。在项目中新建raw文件夹,把.mp3类型的音频文件复制到raw文件夹下。通过DDMS导入到SD卡。

2、定义MediaPlayer对象,findViewById()方法为各种按钮建立关联。

3、为各种按钮添加事件监听

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

MainActivity

package com.example.mediaplayer;

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

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

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://y.qq.com/#type=song&mid=003e0OzZ0wFBRX&tpl=yqq_song_detail";
     private VideoView videoView;
     MediaController mc;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ma);
        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);
        videoView=(VideoView)findViewById(R.id.videoView1);
        File file=new File("/sdcard/bluevird.mp4");
        mc=new MediaController(MainActivity.this);
        
        source.setOnClickListener(new OnClickListener(){
    
            public void onClick(View arg0) {
                // TODO Auto-generated method   stub
                mediaplayer=MediaPlayer.create(MainActivity.this,   R.raw.what);
                mediaplayer.start();
                Toast.makeText(MainActivity.this,"playing the music",Toast.LENGTH_SHORT).show();
            }

         });

         stop.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                // TODO Auto-generated method   stub
                if(mediaplayer.isPlaying()){
                mediaplayer.stop();
                Toast.makeText(MainActivity.this,"stop",Toast.LENGTH_SHORT).show();
                }
            }
         });
         local.setOnClickListener(new OnClickListener() {
             
             @SuppressLint("SdCardPath")   @Override
             public void onClick(View arg0) {
                 // TODO Auto-generated method   stub
                 try {
                    mediaplayer.reset();
                    mediaplayer.setDataSource("/sdcard/Louis Armstrong - What A Wonderful World.mp3");
                    mediaplayer.prepare();
                    mediaplayer.start();
                    Toast.makeText(MainActivity.this,"playing the sdcard music",Toast.LENGTH_SHORT).show();
                 } 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();
             }
             Toast.makeText(MainActivity.this,"network music",Toast.LENGTH_SHORT).show();
             Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(uri));
             startActivity(intent);
          }
             
          });
          video.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                File file=new File("/sdcard/ccc.mp4");
                videoView.setVideoPath(file.getAbsolutePath());
                videoView.setMediaController(mc);
                try{
                    videoView.start();
                } catch (Exception e){
                    e.printStackTrace();
                }
                Toast.makeText(MainActivity.this,"playing the video",Toast.LENGTH_SHORT).show();
            }
        });
         
    }


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

MA

<?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:padding="30px"
    android:background="@drawable/jx"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="shipinyinyuebofang"
        android:textSize="35px" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="本地音乐" />

        <Button
            android:id="@+id/button1"
            android:layout_width="107dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_weight="0.32"
            android:text="源文件音乐"
            android:textSize="18sp" />
    </LinearLayout>

    <Button
        android:id="@+id/button4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button5"
        android:layout_below="@+id/button3"
        android:text="停止" />

    <Button
        android:id="@+id/button5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:text="视频" />

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="match_parent"
        android:layout_height="212dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button5" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0px"
        android:layout_height="0px" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>

运行结果:

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

    在制作视频播放的过程中,能够听到视频声音以及看到视频播放的滚动条,但是无法正常观看视频的界面。

posted @ 2016-04-22 09:46  09王绍俊  阅读(126)  评论(0编辑  收藏  举报