欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

21天学习android开发教程之MediaPlayer

程序员文章站 2024-02-22 17:48:55
本文介绍mediaplayer的使用。mediaplayer可以播放音频和视频,另外也可以通过videoview来播放视频,虽然videoview比mediaplayer简...

本文介绍mediaplayer的使用。mediaplayer可以播放音频和视频,另外也可以通过videoview来播放视频,虽然videoview比mediaplayer简单易用,但定制性不如用mediaplayer,要视情况选择了。mediaplayer播放音频比较简单,但是要播放视频就需要surfaceview。surfaceview比普通的自定义view更有绘图上的优势,它支持完全的opengl es库。
先贴出本文程序运行结果的截图,上面是播放/停止音频,可用seekbar来调进度,下面是播放/停止视频,也是用seekbar来调进度:

21天学习android开发教程之MediaPlayer

main.xml的源码:

<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">
  <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 android:id="@+id/button02" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="停止播放">
  
  <seekbar android:id="@+id/seekbar02" android:layout_height="wrap_content" 
    android:layout_width="fill_parent">

  <surfaceview android:id="@+id/surfaceview01" 
    android:layout_width="fill_parent" android:layout_height="250px">
  <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 android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/button04">

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

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;//互斥变量,防止定时器与seekbar拖动时进度冲突
  @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  
  //----------media控件设置---------//
  m=new mediaplayer();
  
  //播放结束之后弹出提示
  m.setoncompletionlistener(new mediaplayer.oncompletionlistener(){
      @override
      public void oncompletion(mediaplayer arg0) {
        toast.maketext(testmedia.this, "结束", 1000).show();
        m.release();
      }
  });
  
  //----------定时器记录播放进度---------//
  mtimer = new timer();
  mtimertask = new timertask() {
   @override
   public void run() {  
     if(ischanging==true)
       return;
     
     if(m.getvideoheight()==0)
       skb_audio.setprogress(m.getcurrentposition());
     else 
       skb_video.setprogress(m.getcurrentposition());
   }
  };

  mtimer.schedule(mtimertask, 0, 10);
    
  btn_start_audio = (button) this.findviewbyid(r.id.button01); 
  btn_stop_audio = (button) this.findviewbyid(r.id.button02); 
  btn_start_audio.setonclicklistener(new clickevent());
  btn_stop_audio.setonclicklistener(new clickevent());
  skb_audio=(seekbar)this.findviewbyid(r.id.seekbar01);
  skb_audio.setonseekbarchangelistener(new seekbarchangeevent());
  
  btn_start_video = (button) this.findviewbyid(r.id.button03); 
  btn_stop_video = (button) this.findviewbyid(r.id.button04); 
  btn_start_video.setonclicklistener(new clickevent());
  btn_stop_video.setonclicklistener(new clickevent());
  skb_video=(seekbar)this.findviewbyid(r.id.seekbar02);
  skb_video.setonseekbarchangelistener(new seekbarchangeevent());
  surfaceview = (surfaceview) findviewbyid(r.id.surfaceview01);
  surfaceholder = surfaceview.getholder();
  surfaceholder.setfixedsize(100, 100);
  surfaceholder.settype(surfaceholder.surface_type_push_buffers);
 } 
  
 /*
 * 按键事件处理
 */
 class clickevent implements view.onclicklistener{
  @override
  public void onclick(view v) {
    if(v==btn_start_audio)
    {
      m.reset();//恢复到未初始化的状态
      m=mediaplayer.create(testmedia.this, r.raw.big);//读取音频
      skb_audio.setmax(m.getduration());//设置seekbar的长度
      try {          
        m.prepare();  //准备
      } catch (illegalstateexception e) {      
        // todo auto-generated catch block        
        e.printstacktrace();        
      } catch (ioexception e) {      
        // todo auto-generated catch block        
        e.printstacktrace();        
      }    
      m.start();  //播放
    }
    else if(v==btn_stop_audio || v==btn_stop_video)
    {
      m.stop();
    }
    else if(v==btn_start_video)
    {
      m.reset();//恢复到未初始化的状态
      m=mediaplayer.create(testmedia.this, r.raw.test);//读取视频
      skb_video.setmax(m.getduration());//设置seekbar的长度
      m.setaudiostreamtype(audiomanager.stream_music);
      m.setdisplay(surfaceholder);//设置屏幕
      
      try {
       m.prepare();
       
      } catch (illegalargumentexception 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();
      }
      m.start();
    }
  }
 }
 
 /*
 * seekbar进度改变事件
 */
 class seekbarchangeevent implements seekbar.onseekbarchangelistener{

  @override
  public void onprogresschanged(seekbar seekbar, int progress,
      boolean fromuser) {
    // todo auto-generated method stub
    
  }

  @override
  public void onstarttrackingtouch(seekbar seekbar) {
   ischanging=true;
  }

  @override
  public void onstoptrackingtouch(seekbar seekbar) {
    m.seekto(seekbar.getprogress());
   ischanging=false;  
  }
   
 }

}

以上就是21天学习android开发教程第一篇关于mediaplayer的相关介绍,希望对大家的学习有所帮助。