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


    【目的】

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

    【要求】

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

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

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

    【原来】

       Android多媒体处理机制。

    【过程】

       1.新建工程Mediaplayer;

       2.修改布局文件activity_main,添加videoview.xml文件;

       3.修改MainActivity.java,新建VideoActivity.java;

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

       5.代码

       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="#bbbbbb"
        tools:context=".MainActivity" 
        >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button4"
            android:layout_alignRight="@+id/button6"
            android:gravity="center"
            android:text="测试多媒体播放"
            android:textSize="45px" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignRight="@+id/button6"
            android:layout_below="@+id/textView1"
            android:text="播放源文件中的音乐" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignRight="@+id/button6"
            android:layout_below="@+id/button1"
            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/button6"
            android:layout_below="@+id/button2"
            android:text="播放网络上的音乐" />
    
        <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_toRightOf="@+id/button5"
            android:text="退出" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button3"
            android:layout_toLeftOf="@+id/button5"
            android:text="停止播放" />
    
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button3"
            android:layout_centerHorizontal="true"
            android:text="播放视屏" />
    
    </RelativeLayout>

    videoview.xml

    <?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:background="#bbbbbb"
        android:orientation="vertical" >
    
        <VideoView
            android:id="@+id/videoView1"
            android:layout_width="match_parent"
            android:layout_height="500px" />
        
    </LinearLayout>

    MainActivity.java代码

    package com.example.mediaplayer;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    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 {
        private TextView tv;
        private Button source,local,net,stopbtn,startbtn,exitbtn;
        private MediaPlayer soutceMP = new MediaPlayer();
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            source=(Button)findViewById(R.id.button1);
            local=(Button)findViewById(R.id.button2);
            net=(Button)findViewById(R.id.button3);
            stopbtn=(Button)findViewById(R.id.button4);
            startbtn=(Button)findViewById(R.id.button5);
            exitbtn=(Button)findViewById(R.id.button6);
            tv=(TextView)findViewById(R.id.textView1);
            final Intent startsv  = new Intent();
            
            source.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    startsv.setClass(MainActivity.this, MusicService.class);
                }
            });
            local.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                }
            });
            net.setOnClickListener(new OnClickListener() {    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                }
            });
            stopbtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub    
                }
            });
            startbtn.setOnClickListener(new OnClickListener() {        
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub    
                }
            });
            exitbtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                }
            });
        }
        @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;
        }
    }

    【运行结果】

    【心得体会】

         通过这次实验,学会了如何实现在应用程序中处理音频和视频。

  • 相关阅读:
    IE浏览器下ajax缓存导致数据不更新的解决方法
    .NET C# Tostring format 格式化字符串
    解决jquery.validate.js的验证bug
    jquery中attr和prop的区别
    Jquery操作下拉框(DropDownList)实现取值赋值
    【MVC】自定义Scaffold Template
    【HTML5+MVC4】xhEditor网页编辑器图片上传
    回归与梯度下降法及实现原理
    浅谈强化学习的方法及学习路线
    【OpenCV学习笔记之一】图像加载,修改及保存
  • 原文地址:https://www.cnblogs.com/jun-28blog/p/5420083.html
Copyright © 2020-2023  润新知