Android多媒体播放功能的代码分析

 更新时间:2016年9月20日 20:00  点击:1902
我们曾经在其他文章中通过对录音以及录像的实现方法具体讲解过有Android多媒体录制功能的实现方式介绍 的相关操作技巧。在这里我们将会为大家详细介绍一下Android多媒体播放的应用方式,以帮助大家对这方面的应用知识有一个深刻的印象。


Android多媒体播放代码:

 代码如下 复制代码


import android.app.Activity; 
import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener; 
import android.widget.Button;
  import android.widget.VideoView; 
public class VideoPlayer extends Activity   {
  /**   Called when the activity is first created.
  */
@Override
  public void onCreate(Bundle icicle)   {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  final VideoView w =(VideoView)findViewById(R.id.vdoplayer);
  Button cmdload = (Button)this.findViewById(R.id.cmd_load);   cmdload.setOnClickListener(new OnClickListener()  { 
public void onClick(View arg0)   {  
// TODO Auto-generated method stub
  w.setVideoPath("/sdcard/android/kongfu.mp4");
  } 

); 
Button cmdplay = (Button)this.findViewById(R.id.cmd_play); 
cmdplay.setOnClickListener(new OnClickListener()  {  
public void onClick(View arg0)   { 
// TODO Auto-generated method stub 
w.start(); 
}
  } 
);  
Button cmdpause = (Button)this.findViewById(R.id.cmd_pause);   cmdpause.setOnClickListener(new OnClickListener()  {
  public void onClick(View arg0)   {
  // TODO Auto-generated method stub
  w.pause(); 
}
  }
  );
  }

Android多媒体播放实现代码:
xml文件:

 代码如下 复制代码


< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
< Button android:id="@+id/cmd_load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="load" />
< Button android:id="@+id/cmd_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play" />
< Button android:id="@+id/cmd_pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pause" />
< VideoView android:id="@+id/vdoplayer"
android:layout_width="fill_parent"
android:layout_height="300px" />
< /LinearLayout>

本文章为安卓开发者入门者介绍关于Android Debug Bridge (ADB)的使用方法,有需要学习的朋友可进入参考。

 

Android系统调试工具——ADB

Android Debug Bridge是个多功能的工具,可以管理设备上的执行状况。
即ADB是管理模拟器的一个工具。

包括下列三个部分:

客户端程序(Client):在开发环境上运行,也可以通过命令行模式shell接口执行adb命令来操作客户端程序。其他Android工具如开发工具ADT或调试监控系统DDMS都可以创建一个adb客户端。

服务器程序(Server):在开发环境的后台运行,服务器程序负责管理介于客户端程序和守护进程程序的通信沟通。
守护进程程序(Daemon):在实际硬件设备或虚拟设备的后台运行。
adb命令位于安装路径下的platform-tools路径中,把这个路径加在环境变量PATH中就可以在其他目录下使用adb命令。

adb命令的标准格式

adb [-d|-e|-s <serialNumber>] <command>
这是adb命令的标准格式,当你正在执行的仿真器有多个时,要加上仿真器序号来区别:

使用“-s <serialNumber>”选项参数;
使用“-d”选项参数,adb命令只会送到连接USB的实际硬设备;
使用“-e”选项参数,adb命令只会送到Android仿真器。

后面<command>是必要的命令。

安装与卸载应用程序apk文件

安装:
adb [-s <serialNumber>] install <path_to_apk>

范例:安装helloWorld.apk到Android仿真器序列号是5556的虚拟设备上:

 adb –s emulator-5556 install helloWorld.apk
 adb install helloWorld.apk

使用adb install安装apk组件程序时,安装在Android仿真器上的apk组件会被放在Android系统目录的/data/app下,这个目录下的应用程序文件名就是写程序时设置的Package name

卸载:

 adb [-s <serialNumber>] uninstall <package>
范例:

adb –s emulator-5556 uninstall com.example.android.helloWorld.apk
adb uninstall com.example.android.helloWorld.apk

注意安装是接apk文件路径名称,而移除是接package名称。

手动删除:

  adb shell
  cd data/app
  rm app.apk

Android操作系统命令行模式,使用shell命令

  执行

  adb shell

  可以进入Android操作系统命令行模式,如此一来你就可以管理和查询Android操作系统的目录和执行相关的命令。

  因为Android操作系统是Linux操作系统的一种,所以shell命令和Linux操作系统是相同的,例如ls命令显示文件目录,cd命令更改文件目录,mkdir命令创建目录,rmdir命令删除目录,rm命令删除文件,mv命令移动文件。

  进入Android操作系统命令行模式后,要离开回到控制台窗口,可以使用exit命令。

上传文件到/sdcard或自/sdcard下载文件(复制文件)

  上传,即从系统复制文件到设备:
  adb push <local> <remote>
  如:adb push d:test.txt /sdcard/
  下载,即从设备复制文件到系统:
  adb pull <remote> <local>
  如:adb pull /sdcard/test.txt d:/

发布端口

  可以设置任意的端口号,作为主机向模拟器或设备的请求端口。如:
  adb forward tcp:5555 tcp:8000

搜索/等待模拟器、设备实例

  取得当前运行的模拟器,设备的实例列表及每个实例的状态|等待正在运行的设备。
  adb devices
  adb wait-for-device

 

查看Bug报告

  adb bugreport

记录无线通讯日志

  adb shell
  logcat –b radio


获取设备ID和序列号

  adb get-product

  adb get-serialno

访问数据库SQLite3

  adb shell
  sqlite3

本文章来介绍在android开发中,我们通过button按钮来动态改变字体大小、字体颜色、背景颜色代码,有需要了解的朋友可参考参考。

实现的逻辑:通过遍历View的方式,判断View是否是TextView、EditText和Button类型,如果是的话,就修改。

代码如下:
1、xml布局文件,文件名:test4.xml,内容如下:

 代码如下 复制代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:id="@+id/mainLayout">
    
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <EditText android:id="@+id/fontSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="30"
        android:hint="请输入数字"/>
         
        <Button android:id="@+id/ChangeSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变字体"/>    
         
    </LinearLayout>
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <EditText android:id="@+id/fontColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="#ffffff"
        android:hint="请输入字体颜色"/>
         
        <Button android:id="@+id/ChangeColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变字体颜色" />    
         
    </LinearLayout>
     
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         
        <EditText android:id="@+id/bgColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="#ff0000"
        android:hint="请输入背景颜色"/>
         
        <Button android:id="@+id/ChangeBgColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改变背景颜色"/>    
         
    </LinearLayout>
     
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
      
    
   <TextView android:id="@+id/TextView01"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="top"
                   android:gravity="top"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView02"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="bottom"
                   android:gravity="bottom"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView03"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="left"
                   android:gravity="left"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView04"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="right"
                   android:gravity="right"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView05"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="center_vertical"
                   android:gravity="center_vertical"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView06"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="fill_vertical"
                   android:gravity="fill_vertical"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView07"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="center_horizontal"
                   android:gravity="center_horizontal"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView08"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="fill_horizontal"
                   android:gravity="fill_horizontal"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>                                                                                                                                     
 
   <TextView android:id="@+id/TextView09"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="center"
                   android:gravity="center"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView10"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="fill"
                   android:gravity="fill"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView11"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="clip_vertical"
                   android:gravity="clip_vertical"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
                    
   <TextView android:id="@+id/TextView12"
                   android:layout_width="fill_parent"
                   android:layout_height="50dp"
                   android:text="clip_horizontal"
                   android:gravity="clip_horizontal"
                   android:textColor="#ffffff"
                   android:background="#00ff00"
                   android:layout_margin="2px"/>
    </LinearLayout>                                                                                               
</LinearLayout>

2、实现的代码文件:MainActivity.java,代码如下:

 代码如下 复制代码

package org.shuxiang.test;
 
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity{
         
        private LinearLayout mainLayout;
        private Button changeSize, changeColor, changeBgColor;
        private EditText fontSize, fontColor, bgColor;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test4);
         
        mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
        changeSize = (Button) findViewById(R.id.ChangeSize);
        fontSize = (EditText) findViewById(R.id.fontSize);
         
        changeSize.setOnClickListener(new OnClickListener()
        {
                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                setFontSize(mainLayout, Float.parseFloat(fontSize.getText().toString()));
                        }                
        });
         
        changeColor = (Button) findViewById(R.id.ChangeColor);
        fontColor = (EditText) findViewById(R.id.fontColor);
         
        changeColor.setOnClickListener(new OnClickListener()
        {
                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                int color = Integer.parseInt(fontColor.getText().toString().replace("#", ""), 16);
                                int red = (color & 0xff0000) >> 16; 
                            int green = (color & 0x00ff00) >> 8; 
                            int blue = (color & 0x0000ff); 
                             
                            setFontColor(mainLayout, Color.rgb(red, green, blue));                                
                        }                
        });
         
        changeBgColor = (Button) findViewById(R.id.ChangeBgColor);
        bgColor = (EditText) findViewById(R.id.bgColor);
         
        changeBgColor.setOnClickListener(new OnClickListener()
        {
                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                int color = Integer.parseInt(bgColor.getText().toString().replace("#", ""), 16);
                                int red = (color & 0xff0000) >> 16; 
                            int green = (color & 0x00ff00) >> 8; 
                            int blue = (color & 0x0000ff);
                             
                                setBgColor(mainLayout, Color.rgb(red, green, blue));
                        }                
        });        
 
        
    }
     
    /**
     * 改变字体
     * @param v
     * @param fontSize
     */
    public void setFontSize(View v, float fontSizeValue)
    {
            if(v instanceof TextView)
                {
                        ((TextView) v).setTextSize(fontSizeValue);
                }
                else if(v instanceof EditText)
                {
                        ((EditText) v).setTextSize(fontSizeValue);
                }
                else if(v instanceof Button)
                {
                        ((Button) v).setTextSize(fontSizeValue);
                }
                else
                {
                        int vChildCount = ((ViewGroup) v).getChildCount();
                    for(int i=0; i<vChildCount; i++)
                    {
                            View v1 = ((ViewGroup) v).getChildAt(i);
                            setFontSize(v1, fontSizeValue);
                    }
                }        
    }
     
    /**
     * 改变字体颜色
     * @param v
     * @param fontSize
     */
    public void setFontColor(View v, int fontColorValue)
    {
            if(v instanceof TextView)
                {
                        ((TextView) v).setTextColor(fontColorValue);
                }
                else if(v instanceof EditText)
                {
                        ((EditText) v).setTextColor(fontColorValue);
                }
                else if(v instanceof Button)
                {
                        ((Button) v).setTextColor(fontColorValue);
                }
                else
                {
                        int vChildCount = ((ViewGroup) v).getChildCount();
                    for(int i=0; i<vChildCount; i++)
                    {
                            View v1 = ((ViewGroup) v).getChildAt(i);
                            setFontColor(v1, fontColorValue);
                    }
                }        
    }
     
    /**
     * 改变背景字体
     * @param v
     * @param fontSize
     */
    public void setBgColor(View v, int bgColorValue)
    {
            if(v instanceof TextView)
                {
                        ((TextView) v).setBackgroundColor(bgColorValue);
                }
                else if(v instanceof EditText)
                {
                        ((EditText) v).setBackgroundColor(bgColorValue);
                }
                else if(v instanceof Button)
                {
                        ((Button) v).setBackgroundColor(bgColorValue);
                }
                else
                {
                        int vChildCount = ((ViewGroup) v).getChildCount();
                    for(int i=0; i<vChildCount; i++)
                    {
                            View v1 = ((ViewGroup) v).getChildAt(i);
                            setBgColor(v1, bgColorValue);
                    }
                }        
    }    
 
}

本文章介绍一段超张简单的关于android如何判断是用户还机器代码,有需要的朋友可参考。

我们写程序的时候可能需要判断用户到底使用的是模拟器还是真机,可以使用如下的代码

 代码如下 复制代码
private boolean isEmulator() {
    return (Build.MODEL.equals("sdk")) || (Build.MODEL.equals("google_sdk"));
}
本文章介绍一个入门型号的教程,关于android 如何判断当前设备是模拟器还是真机和设置当前的Activity的屏幕亮度实例有需要的朋友可参考。

/** 判断是否模拟器。如果返回TRUE,则当前是模拟器
     * @param context
     * @return
*/ 

 代码如下 复制代码

public static boolean isEmulator(Context context){ 
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
        String imei = tm.getDeviceId(); 
        if (imei == null || imei.equals("000000000000000")){ 
            return true; 
        } 
        return false; 
}

这个方法没大量测试过,应该是WORK的,一般真机都有IMEI的,不过也见过工程机的IMEI是000000000000000还是0。



设置当前的Activity的屏幕亮度,而不是设置系统的屏幕亮度,退出当前的Activity后恢复系统的亮度。 直接看代码好了

 代码如下 复制代码

WindowManager.LayoutParams lp = getWindow().getAttributes(); 

lp.screenBrightness = 0.5f; 
getWindow().setAttributes(lp); 

  screenBrightness的值范围是0到1。 注意不要设成0,屏幕会黑掉,完全看不到。

[!--infotagslink--]

相关文章

  • Android子控件超出父控件的范围显示出来方法

    下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • Android模拟器上模拟来电和短信配置

    如果我们的项目需要做来电及短信的功能,那么我们就得在Android模拟器开发这些功能,本来就来告诉我们如何在Android模拟器上模拟来电及来短信的功能。 在Android模拟...2016-09-20
  • 夜神android模拟器设置代理的方法

    夜神android模拟器如何设置代理呢?对于这个问题其实操作起来是非常的简单,下面小编来为各位详细介绍夜神android模拟器设置代理的方法,希望例子能够帮助到各位。 app...2016-09-20
  • android自定义动态设置Button样式【很常用】

    为了增强android应用的用户体验,我们可以在一些Button按钮上自定义动态的设置一些样式,比如交互时改变字体、颜色、背景图等。 今天来看一个通过重写Button来动态实...2016-09-20
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • Android WebView加载html5页面实例教程

    如果我们要在Android应用APP中加载html5页面,我们可以使用WebView,本文我们分享两个WebView加载html5页面实例应用。 实例一:WebView加载html5实现炫酷引导页面大多...2016-09-20
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • 深入理解Android中View和ViewGroup

    深入理解Android中View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,View需要继承ViewGroup,但实际上不是这样的。View是基类,ViewGroup是它的子类。本教程我们深...2016-09-20
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • Android自定义WebView网络视频播放控件例子

    下面我们来看一篇关于Android自定义WebView网络视频播放控件开发例子,这个文章写得非常的不错下面给各位共享一下吧。 因为业务需要,以下代码均以Youtube网站在线视...2016-10-02