• 播放视频Android提高第一篇之MediaPlayer


    文章结束给大家来个程序员笑话:[M]

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

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

        

        main.xml的源码:

        

    1. [xhtml] view plaincopyprint?
    2. <?xml version="1.0" encoding="utf-8"?>  
    3. <LinearLayout android:id="@+id/LinearLayout01"  
    4.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
    5.     xmlns:android="http://schemas.android.com/apk/res/android"  
    6.     android:orientation="vertical">  
    7.     <SeekBar android:id="@+id/SeekBar01" android:layout_height="wrap_content"  
    8.         android:layout_width="fill_parent"></SeekBar>  
    9.     <LinearLayout android:id="@+id/LinearLayout02"  
    10.         android:layout_width="wrap_content" android:layout_height="wrap_content">  
    11.         <Button android:id="@+id/Button01" android:layout_width="wrap_content"  
    12.             android:layout_height="wrap_content" android:text="播放音频"></Button>  
    13.         <Button android:id="@+id/Button02" android:layout_width="wrap_content"  
    14.             android:layout_height="wrap_content" android:text="停止播放"></Button>  
    15.     </LinearLayout>  
    16.     <SeekBar android:id="@+id/SeekBar02" android:layout_height="wrap_content"  
    17.         android:layout_width="fill_parent"></SeekBar>  
    18.   
    19.     <SurfaceView android:id="@+id/SurfaceView01"  
    20.         android:layout_width="fill_parent" android:layout_height="250px"></SurfaceView>  
    21.     <LinearLayout android:id="@+id/LinearLayout02"  
    22.         android:layout_width="wrap_content" android:layout_height="wrap_content">  
    23.         <Button android:layout_width="wrap_content"  
    24.             android:layout_height="wrap_content" android:id="@+id/Button03"  
    25.             android:text="播放视频"></Button>  
    26.         <Button android:layout_width="wrap_content"  
    27.             android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/Button04"></Button>  
    28.     </LinearLayout>  
    29. </LinearLayout>  
    复制代码

        



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

        每日一道理
    生活中受伤难免,失败跌倒并不可怕,可怕的是因此而一蹶不振,失去了对人生的追求与远大的理想。没有一个人的前进道路是平平稳稳的,就算是河中穿梭航行的船只也难免颠簸,生活中所遇上的坎坷磨难不是偶尔给予的为难,而是必然所经受的磨练。

        

    1. [java] view plaincopyprint?
    2. package com.testMedia;  
    3.   
    4. import java.io.IOException;   
    5. import java.util.Timer;  
    6. import java.util.TimerTask;  
    7. import android.app.Activity;   
    8. import android.media.AudioManager;  
    9. import android.media.MediaPlayer;  
    10. import android.os.Bundle;   
    11. import android.view.SurfaceHolder;  
    12. import android.view.SurfaceView;  
    13. import android.view.View;   
    14. import android.widget.Button;   
    15. import android.widget.SeekBar;  
    16. import android.widget.Toast;   
    17.   
    18.   
    19. public class testMedia extends Activity {  
    20.       /** Called when the activity is first created. */   
    21.   
    22.     private SeekBar skb_audio=null;  
    23.     private Button btn_start_audio = null;   
    24.     private Button btn_stop_audio = null;  
    25.   
    26.     private SeekBar skb_video=null;  
    27.     private Button btn_start_video = null;   
    28.     private Button btn_stop_video = null;  
    29.     private SurfaceView surfaceView;   
    30.     private SurfaceHolder surfaceHolder;   
    31.       
    32.     private MediaPlayer m = null;   
    33.     private Timer mTimer;  
    34.     private TimerTask mTimerTask;  
    35.       
    36.     private boolean isChanging=false;//互斥变量,避免定时器与SeekBar拖动时进度冲突  
    37.      @Override   
    38.     public void onCreate(Bundle savedInstanceState) {   
    39.         super.onCreate(savedInstanceState);   
    40.         setContentView(R.layout.main);   
    41.          
    42.         //----------Media控件设置---------//  
    43.         m=new MediaPlayer();  
    44.          
    45.         //播放结束之后弹出提示  
    46.         m.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){  
    47.             @Override  
    48.             public void onCompletion(MediaPlayer arg0) {  
    49.                 Toast.makeText(testMedia.this, "结束", 1000).show();  
    50.                 m.release();  
    51.             }  
    52.         });  
    53.          
    54.       //----------定时器记录播放进度---------//  
    55.         mTimer = new Timer();  
    56.         mTimerTask = new TimerTask() {  
    57.             @Override  
    58.             public void run() {   
    59.                 if(isChanging==true)  
    60.                     return;  
    61.                   
    62.                 if(m.getVideoHeight()==0)  
    63.                     skb_audio.setProgress(m.getCurrentPosition());  
    64.                 else   
    65.                     skb_video.setProgress(m.getCurrentPosition());  
    66.             }  
    67.         };  
    68.   
    69.         mTimer.schedule(mTimerTask, 0, 10);  
    70.          
    71.         btn_start_audio = (Button) this.findViewById(R.id.Button01);   
    72.         btn_stop_audio = (Button) this.findViewById(R.id.Button02);   
    73.         btn_start_audio.setOnClickListener(new ClickEvent());  
    74.         btn_stop_audio.setOnClickListener(new ClickEvent());  
    75.         skb_audio=(SeekBar)this.findViewById(R.id.SeekBar01);  
    76.         skb_audio.setOnSeekBarChangeListener(new SeekBarChangeEvent());  
    77.          
    78.         btn_start_video = (Button) this.findViewById(R.id.Button03);   
    79.         btn_stop_video = (Button) this.findViewById(R.id.Button04);   
    80.         btn_start_video.setOnClickListener(new ClickEvent());  
    81.         btn_stop_video.setOnClickListener(new ClickEvent());  
    82.         skb_video=(SeekBar)this.findViewById(R.id.SeekBar02);  
    83.         skb_video.setOnSeekBarChangeListener(new SeekBarChangeEvent());  
    84.         surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);  
    85.         surfaceHolder = surfaceView.getHolder();  
    86.         surfaceHolder.setFixedSize(100, 100);  
    87.         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
    88.     }   
    89.       
    90.   /*
    91.    * 按键事件处理
    92.    */  
    93.   class ClickEvent implements View.OnClickListener{  
    94.     @Override  
    95.     public void onClick(View v) {  
    96.         if(v==btn_start_audio)  
    97.         {  
    98.             m.reset();//恢复到未初始化的状态  
    99.             m=MediaPlayer.create(testMedia.this, R.raw.big);//读取音频  
    100.             skb_audio.setMax(m.getDuration());//设置SeekBar的长度  
    101.             try {                     
    102.                 m.prepare();    //准备  
    103.             } catch (IllegalStateException e) {           
    104.                 // TODO Auto-generated catch block               
    105.                 e.printStackTrace();                  
    106.             } catch (IOException e) {            
    107.                 // TODO Auto-generated catch block               
    108.                 e.printStackTrace();                  
    109.             }         
    110.             m.start();  //播放  
    111.         }  
    112.         else if(v==btn_stop_audio || v==btn_stop_video)  
    113.         {  
    114.             m.stop();  
    115.         }  
    116.         else if(v==btn_start_video)  
    117.         {  
    118.             m.reset();//恢复到未初始化的状态  
    119.             m=MediaPlayer.create(testMedia.this, R.raw.test);//读取视频  
    120.             skb_video.setMax(m.getDuration());//设置SeekBar的长度  
    121.             m.setAudioStreamType(AudioManager.STREAM_MUSIC);  
    122.             m.setDisplay(surfaceHolder);//设置屏幕  
    123.               
    124.             try {  
    125.                 m.prepare();  
    126.                   
    127.             } catch (IllegalArgumentException e) {  
    128.                 // TODO Auto-generated catch block  
    129.                 e.printStackTrace();  
    130.             } catch (IllegalStateException e) {  
    131.                 // TODO Auto-generated catch block  
    132.                 e.printStackTrace();  
    133.             } catch (IOException e) {  
    134.                 // TODO Auto-generated catch block  
    135.                 e.printStackTrace();  
    136.             }  
    137.             m.start();  
    138.         }  
    139.     }  
    140.   }  
    141.    
    142.   /*
    143.    * SeekBar进度转变事件
    144.    */  
    145.   class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{  
    146.   
    147.     @Override  
    148.     public void onProgressChanged(SeekBar seekBar, int progress,  
    149.             boolean fromUser) {  
    150.         // TODO Auto-generated method stub  
    151.          
    152.     }  
    153.   
    154.     @Override  
    155.     public void onStartTrackingTouch(SeekBar seekBar) {  
    156.         isChanging=true;  
    157.     }  
    158.   
    159.     @Override  
    160.     public void onStopTrackingTouch(SeekBar seekBar) {  
    161.         m.seekTo(seekBar.getProgress());  
    162.         isChanging=false;     
    163.     }  
    164.         
    165.   }  
    166.   
    167. }

    文章结束给大家分享下程序员的一些笑话语录: 一条狗在街上闲逛,看见橱窗里一张告示:「招聘程序员。会编程,有团队精神,至少精通两种语言。均等机会。」
      那条狗就进去申请,但是被拒绝了。
      「我不能雇一条狗在公司里做事。」经理说。
      狗不服气,指着告示上「均等机会」几字抗议。
      经理没法,叹了口气,不屑地问道:「你会编程吗?」
      那条狗默默地走到电脑前,编了个程序,运作准确。
      「你有团队精神吗?」经理问。
      那条狗掉头看了看门外,一大群野狗在外面虎视耽耽。
      「我真的不能雇狗做这份工作。」经理气急败坏地说。
      「就算会编程、有团队精神,但是我需要的雇员至少要能精通两种语言。」
      那条狗抬头看着经理说:「喵-噢。」

  • 相关阅读:
    Ubuntu 14.04上架IPSec+L2TP的方法
    Windows Server 2008 R2 FTP无法从外部访问的解决方法
    在Windows Server 2008 R2上打开ping的方法
    全站导航
    拉勾网招聘信息分析
    pandas之DataFrame
    pandas之Series
    matplolib学习
    numpy学习
    scrapy框架【爬虫的暂停和启动】
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3078457.html
Copyright © 2020-2023  润新知