码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-22 01:08:45      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

实验报告

课程名称

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

实验日期

2016年4月21日

实验项目名称

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

实验地点

S3010

实验类型

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

学  时

2

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

  1. 实现在应用程序中处理音频和视频。
  2. 实现播放音频,音频播放控制;
  3. 实现播放视频,视频播放控制;
  4. 使用Service服务播放项目源文件中的音乐。

 

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

(1)PC机

(2)操作系统:Windows XP

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

三、实验内容及步骤

1)新建工程;

2)修改布局文件main.xml;

3)完善Activity类。

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

代码:

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.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);
        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/bluebird.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;
    }
    
}

Main 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: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_centerHorizontal="true"
        android:text="mediaplayertest"
        android:textSize="35px" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="源文件音乐" />

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

    <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:text="网络音乐" />

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

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

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

</RelativeLayout>

运行结果:(截图)

技术分享

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

通过这次实验知道了如何往模拟器中的sd卡放入文件以及如何从sd卡里使用。

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

标签:

原文地址:http://www.cnblogs.com/zzen/p/5419517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!