频道栏目
首页 > 资讯 > Android > 正文

Android提高第一篇之MediaPlayer

11-01-10        来源:[db:作者]  
收藏   我要投稿

前面写了十四篇关于界面的入门文章,大家都看完和跟着练习之后,对于常用的Layout和View都会有一定的了解了,接下来的文章就不再强调介绍界面了,而是针对具体的常见功能而展开。

        本文介绍MediaPlayer的使用。MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。

         先贴出本文程序运行结果的截图,上面是播放/停止音频,可用SeekBar来调进度,下面是播放/停止视频,也是用SeekBar来调进度:

 

 

main.xml的源码:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android
    android:orientation="vertical"> 
    <SeekBar android:id="@+id/SeekBar01" android:layout_height="wrap_content" 
        android:layout_width="fill_parent"></SeekBar> 
    <LinearLayout android:id="@+id/LinearLayout02" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        <Button android:id="@+id/Button01" android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="播放音频"></Button> 
        <Button android:id="@+id/Button02" android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="停止播放"></Button> 
    </LinearLayout> 
    <SeekBar android:id="@+id/SeekBar02" android:layout_height="wrap_content" 
        android:layout_width="fill_parent"></SeekBar> 
 
    <SurfaceView android:id="@+id/SurfaceView01" 
        android:layout_width="fill_parent" android:layout_height="250px"></SurfaceView> 
    <LinearLayout android:id="@+id/LinearLayout02" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:id="@+id/Button03" 
            android:text="播放视频"></Button> 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/Button04"></Button
    </LinearLayout> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical">
 <SeekBar android:id="@+id/SeekBar01" android:layout_height="wrap_content"
  android:layout_width="fill_parent"></SeekBar>
 <LinearLayout android:id="@+id/LinearLayout02"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
  <Button android:id="@+id/Button01" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="播放音频"></Button>
  <Button android:id="@+id/Button02" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="停止播放"></Button>
 </LinearLayout>
 <SeekBar android:id="@+id/SeekBar02" android:layout_height="wrap_content"
  android:layout_width="fill_parent"></SeekBar>

 <SurfaceView android:id="@+id/SurfaceView01"
  android:layout_width="fill_parent" android:layout_height="250px"></SurfaceView>
 <LinearLayout android:id="@+id/LinearLayout02"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
  <Button android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:id="@+id/Button03"
   android:text="播放视频"></Button>
  <Button android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/Button04"></Button>
 </LinearLayout>
</LinearLayout>
 

 

本文程序的源码,有点长:

view plaincopy to clipboardprint?
package com.testMedia;  
 
import java.io.IOException;    
import java.util.Timer;  
import java.util.TimerTask;  
import android.app.Activity;    
import android.media.AudioManager;  
import android.media.MediaPlayer;  
import android.os.Bundle;    
import android.view.SurfaceHolder;  
import android.view.SurfaceView;  
import android.view.View;    
import android.widget.Button;    
import android.widget.SeekBar;  
import android.widget.Toast;    
 
 
public class testMedia extends Activity {  
      /** Called when the activity is first created. */   
 
    private SeekBar skb_audio=null;  
    private Button btn_start_audio = null;    
    private Button btn_stop_audio = null;  
 
    private SeekBar skb_video=null;  
    private Button btn_start_video = null;    
    private Button btn_stop_video = null;  
    private SurfaceView surfaceView;   
    private SurfaceHolder surfaceHolder;   
      
    private MediaPlayer m = null;    
    private Timer mTimer;  
    private TimerTask mTimerTask;  
      
    private boolean isChanging=false;//互斥变量,防止

相关TAG标签
上一篇:Android提高第二篇之SurfaceView(上)
下一篇:在Android上使用ZXing识别条码/二维码
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站