您的位置:首页 > 其它

移动终端设计实验 实验6:多媒体处理实验

2016-12-02 10:31 471 查看

实验6:多媒体处理实验

一、     实验目的

掌握使用MediaPlayer组件播放音频、VideoView播放视频以及MediaRecorder组件实现录音的方法。

 

二、     实验环境

Windows7  Eclipse MARS.2  AVD虚拟机

三、实验内容及结果

设计一个能播放音乐和视频的播放器。界面如右图所示,具体要求如下:

(1)       点击“播放工程中原始文件”可以播放存放在raw文件夹下的音乐文件;

(2)       点击“播放SD卡文件”可以播放存放在SD卡上的音乐文件;

(3)       点击“播放网络文件”可以播放网络上存储的音乐文件;(略)

(4)       点击“播放视频”文件,可以播放视频;

(5)       播放音乐时,可通过“停止”和“暂停”按钮控制播放。

代码实现:

MainActivity.java

package com.example.pplayer;
 
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public
class
MainActivity extends Activity {
    Button gongcheng,sdcard,stop,pause;
    MediaPlayer mp;
    @Override
    protected
void
onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gongcheng=(Button)findViewById(R.id.gongcheng);
        sdcard=(Button)findViewById(R.id.sdcard);
        stop=(Button)findViewById(R.id.stop);
        pause=(Button)findViewById(R.id.pause);
       
        gongcheng.setOnClickListener(new OnClickListener(){
 
            @Override
            public
void
onClick(View v) {
                // TODO Auto-generated method stub
                mp=MediaPlayer.create(MainActivity.this,
 R.raw.goon);
                mp.start();
                Toast.makeText(MainActivity.this,

"raw播放成功goon", 1).show();
            }
           
        });
       
        sdcard.setOnClickListener(new OnClickListener(){
 
            @Override
            public
void
onClick(View arg0) {
                // TODO Auto-generated method stub
                mp=new MediaPlayer();
                try {
                    mp.setDataSource("/sdcard/firework.mp3");
                    mp.prepare();
                   
                } catch (IllegalArgumentException
e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException
e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException
e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException
e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mp.start();
                Toast.makeText(MainActivity.this,

"sdcard播放成功firework", 1).show();
            }
           
        });
       
        stop.setOnClickListener(new

OnClickListener(){
 
            @Override
            public
void
onClick(View arg0) {
                // TODO Auto-generated method stub
                mp.stop();
                Toast.makeText(MainActivity.this,
 "歌曲已停止", 1).show();
            }
        });
       
        pause.setOnClickListener(new

OnClickListener(){
 
            @Override
            public
void
onClick(View arg0) {
                // TODO Auto-generated method stub
                mp.pause();
                Toast.makeText(MainActivity.this,

"歌曲已经暂停", 1).show();
            }
           
        });
    }
}
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="com.example.pplayer.MainActivity"
>
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎来到多媒体播放歌曲界面"
/>
 
    <Button
        android:id="@+id/gongcheng"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="播放工程中的原始文件"
/>
 
    <Button
        android:id="@+id/sdcard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gongcheng"
        android:layout_below="@+id/gongcheng"
        android:text="播放SD卡文件"
/>
 
    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/sdcard"
        android:layout_below="@+id/sdcard"
        android:text="停止"
/>
 
    <Button
        android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/stop"
        android:layout_below="@+id/stop"
        android:text="暂停"
/>
</RelativeLayout>

而且一定不要忘记在AndroidManifest.xml中添加此设置,否则sdcard上的歌曲不能播放出来

测试结果:

视频播放代码实现:

MainActivity.java

package com.example.psurfaceview;
 
import java.io.IOException;
 
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public
class
MainActivity extends Activity {
    Button start,pause,stop;
    MediaPlayer mp;
    SurfaceView surface;
    @Override
    protected
void
onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        start=(Button)findViewById(R.id.start);
        pause=(Button)findViewById(R.id.pause);
        stop=(Button)findViewById(R.id.stop);
        surface=(SurfaceView)findViewById(R.id.surface);
       
        start.setOnClickListener(new OnClickListener(){
 
            @Override
            public
void
onClick(View arg0) {
                // TODO Auto-generated method stub
                mp=new MediaPlayer();
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
               
                try {
                    mp.setDataSource("/sdcard/aa.mp4");
                    mp.prepare();
                   
                } catch (IllegalStateException
e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException
e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mp.setDisplay(surface.getHolder());
                mp.start();
                Toast.makeText(MainActivity.this,
"播放视频成功", 1).show();
            }
        });
       
       
        pause.setOnClickListener(new OnClickListener(){
 
            @Override
            public
void
onClick(View arg0) {
                // TODO Auto-generated method stub
                if(mp.isPlaying()){
                    pause.setText("继续");
                    mp.pause();
                    Toast.makeText(MainActivity.this,
"视频暂停", 1).show();
                }
                else {
                    pause.setText("暂停");
                    mp.start();
                    Toast.makeText(MainActivity.this,
"继续播放", 1).show();
                }
            }
        });
       
        stop.setOnClickListener(new OnClickListener(){
 
            @Override
            public
void
onClick(View arg0) {
                // TODO Auto-generated method stub
                if(mp!=null){
                    mp.stop();
                    Toast.makeText(MainActivity.this,
"视频已经停止,无法继续操作", 1).show();
                }
            }
           
        });
    }
}
 

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="com.example.psurfaceview.MainActivity"
>
 
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="欢迎观看视频"
/>
    <SurfaceView

        android:id="@+id/surface"
         android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@id/text1"
        />
    <Button

        android:id="@+id/start"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/surface"
        android:text="播放"
        />
     <Button

        android:id="@+id/pause"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/surface"
        android:layout_toRightOf="@id/start"
        android:text="暂停"
        />
      <Button

        android:id="@+id/stop"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/surface"
        android:layout_toRightOf="@id/pause"
        android:text="停止"
        />
   
</RelativeLayout>
一定不要忘记在清单文件中添加许可,否则不能访问sdcard

四、实验过程分析与讨论
    此次实验相对较简单,只是多媒体开发组件的应用,但是在做有关数据库的资源时,一定不要忘了在Manifest.xml文件中添加许可,否则,将不能使用sdcard上的数据。通过学习,知道了Android系统能够录制,播放各种不同形式的本地和流式多媒体文件,为Android设备多媒体的开发和应用提供了非常好的学习平台。

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: