当前位置:操作系统 > 安卓/Android >>

Android提高第一篇之MediaPlayer

前面写了十四篇关于界面的入门文章,大家都看完和跟着练习之后,对于常用的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;//互斥变量,防止

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,