歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android音樂和視頻播放【附源碼】

重新學了Android,寫了一個mediaplayer播放的程序實現音樂的播放和視頻的播放,程序效果圖如圖:


完成的基本功能有:1,讀取音樂文件 2.播放音樂文件 3,讀取視頻文件4,播放視頻文件

主要程序源代碼:

  1. package com.huangcheng.media;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Timer;  
  5. import java.util.TimerTask;  
  6.   
  7. import android.app.Activity;  
  8. import android.media.AudioManager;  
  9. import android.media.MediaPlayer;  
  10. import android.media.MediaPlayer.OnCompletionListener;  
  11. import android.os.Bundle;  
  12. import android.view.SurfaceHolder;  
  13. import android.view.SurfaceView;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.SeekBar;  
  18. import android.widget.SeekBar.OnSeekBarChangeListener;  
  19. import android.widget.Toast;  
  20.   
  21. public class MediaDemoActivity extends Activity {  
  22.     //控制音樂   
  23.     private SeekBar seekbar_music;  
  24.     private Button start_music_btn;  
  25.     private Button stop_music_btn;  
  26.     //控制視頻   
  27.     private SeekBar seekbar_video;  
  28.     private Button start_video_btn;  
  29.     private Button stop_video_btn;  
  30.     private SurfaceView surfaceview_video;  
  31.     private SurfaceHolder surfaceview_holder;  
  32.       
  33.     //播放器   
  34.     private MediaPlayer media=null;  
  35.     private MediaPlayer media_video=null;  
  36.     private Timer timer;  
  37.     private TimerTask timertask_music;  
  38.     private TimerTask timertask_video;  
  39.       
  40.     //互斥量,seekbar拖動和Timer   
  41.     private volatile boolean ischanging=false;  
  42.       
  43.     /** Called when the activity is first created. */  
  44.     @Override  
  45.     public void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.         setContentView(R.layout.main);  
  48.           
  49.         //控制media   
  50.         media=new MediaPlayer();  
  51.         media_video=new MediaPlayer();  
  52.         media.setOnCompletionListener(new OnCompletionListener() {  
  53.               
  54.             @Override  
  55.             public void onCompletion(MediaPlayer arg0) {  
  56.                 // TODO Auto-generated method stub   
  57.                 Toast.makeText(MediaDemoActivity.this"播放結束", Toast.LENGTH_SHORT).show();  
  58.                 media.release();  
  59.             }  
  60.         });  
  61.         media_video.setOnCompletionListener(new OnCompletionListener() {  
  62.               
  63.             @Override  
  64.             public void onCompletion(MediaPlayer arg0) {  
  65.                 // TODO Auto-generated method stub   
  66.                 Toast.makeText(MediaDemoActivity.this"播放結束", Toast.LENGTH_SHORT).show();  
  67.                 media_video.release();  
  68.             }  
  69.         });  
  70.         //Timer記錄播放時刻   
  71.         timer=new Timer();  
  72.         timertask_music=new TimerTask() {  
  73.               
  74.             @Override  
  75.             public void run() {  
  76.                 // TODO Auto-generated method stub   
  77.                 //正在拖動seekbar,停止Timer   
  78.                 if(ischanging==true){  
  79.                     return;  
  80.                 }  
  81.                 else{  
  82.                         seekbar_music.setProgress(media.getCurrentPosition());  
  83.                 }  
  84.             }  
  85.         };  
  86.         timertask_video=new TimerTask() {  
  87.               
  88.             @Override  
  89.             public void run() {  
  90.                 // TODO Auto-generated method stub   
  91.                 //正在拖動seekbar,停止Timer   
  92.                 if(ischanging==true){  
  93.                     return;  
  94.                 }  
  95.                 else{  
  96.                         seekbar_video.setProgress(media_video.getCurrentPosition());  
  97.                 }  
  98.             }  
  99.         };  
  100.           
  101.         timer.schedule(timertask_music, 010);//開始記錄,每10ms記錄一次   
  102.         timer.schedule(timertask_video, 010);//開始記錄,每10ms記錄一次   
  103.         //更新控件   
  104.         seekbar_music=(SeekBar)findViewById(R.id.seekbar_music);  
  105.         seekbar_video=(SeekBar)findViewById(R.id.seekbar_video);  
  106.         start_music_btn=(Button)findViewById(R.id.start_music_btn);  
  107.         stop_music_btn=(Button)findViewById(R.id.stop_music_btn);  
  108.         start_video_btn=(Button)findViewById(R.id.start_video_btn);  
  109.         stop_video_btn=(Button)findViewById(R.id.stop_video_btn);  
  110.         surfaceview_video=(SurfaceView)findViewById(R.id.surfaceview_video);  
  111.         surfaceview_holder=surfaceview_video.getHolder();  
  112.         surfaceview_holder.setFixedSize(100100);  
  113.         surfaceview_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  114.           
  115.         seekbar_music.setEnabled(false);  
  116.         seekbar_video.setEnabled(false);  
  117.         // 添加處理seekbar事件   
  118.         seekbar_music.setOnSeekBarChangeListener(new SeekbarmusicEvent());  
  119.         seekbar_video.setOnSeekBarChangeListener(new SeekbarvideoEvent());  
  120.         //添加music按鍵處理   
  121.         start_music_btn.setOnClickListener(new OnClickListener() {  
  122.               
  123.             @Override  
  124.             public void onClick(View arg0) {  
  125.                 // TODO Auto-generated method stub   
  126.                 media.reset();  
  127.                 media=MediaPlayer.create(MediaDemoActivity.this, R.raw.m);  
  128.                 seekbar_music.setMax(media.getDuration());  
  129.                 try {  
  130.                     media.prepare();  
  131.                 } catch (IllegalStateException e) {  
  132.                     // TODO Auto-generated catch block   
  133.                     e.printStackTrace();  
  134.                 } catch (IOException e) {  
  135.                     // TODO Auto-generated catch block   
  136.                     e.printStackTrace();  
  137.                 }  
  138.                 media.start();  
  139.                 seekbar_music.setEnabled(true);  
  140.             }  
  141.         });  
  142.         stop_music_btn.setOnClickListener(new OnClickListener() {  
  143.               
  144.             @Override  
  145.             public void onClick(View arg0) {  
  146.                 // TODO Auto-generated method stub   
  147.                 media.stop();  
  148.                 seekbar_music.setEnabled(false);  
  149.             }  
  150.         });  
  151.         start_video_btn.setOnClickListener(new OnClickListener() {  
  152.               
  153.             @Override  
  154.             public void onClick(View arg0) {  
  155.                 // TODO Auto-generated method stub   
  156.                 media_video.reset();  
  157.                 media_video=MediaPlayer.create(MediaDemoActivity.this, R.raw.v);  
  158.                 seekbar_video.setMax(media_video.getDuration());  
  159.                 media_video.setAudioStreamType(AudioManager.STREAM_MUSIC);  
  160.                 media_video.setDisplay(surfaceview_holder);  
  161.                 try {  
  162.                     media_video.prepare();  
  163.                 } catch (IllegalStateException e) {  
  164.                     // TODO Auto-generated catch block   
  165.                     e.printStackTrace();  
  166.                 } catch (IOException e) {  
  167.                     // TODO Auto-generated catch block   
  168.                     e.printStackTrace();  
  169.                 }  
  170.                 media_video.start();  
  171.                 seekbar_video.setEnabled(true);  
  172.             }  
  173.         });  
  174.         stop_video_btn.setOnClickListener(new OnClickListener() {  
  175.               
  176.             @Override  
  177.             public void onClick(View arg0) {  
  178.                 // TODO Auto-generated method stub   
  179.                 media_video.stop();  
  180.                 seekbar_video.setEnabled(false);  
  181.             }  
  182.         });  
  183.           
  184.     }  
  185.     //處理拖動seekbarmusic事件   
  186.     class SeekbarmusicEvent implements SeekBar.OnSeekBarChangeListener{  
  187.         @Override  
  188.         public void onProgressChanged(SeekBar seekBar, int progress,  
  189.                 boolean fromUser) {  
  190.             // TODO Auto-generated method stub   
  191.               
  192.         }  
  193.         @Override  
  194.         public void onStartTrackingTouch(SeekBar seekBar) {  
  195.             // TODO Auto-generated method stub   
  196.             ischanging=true;//停止timer   
  197.         }  
  198.         @Override  
  199.         public void onStopTrackingTouch(SeekBar seekBar) {  
  200.             // TODO Auto-generated method stub   
  201.             media.seekTo(seekBar.getProgress());  
  202.             ischanging=false;//開啟timer   
  203.         }  
  204.     }  
  205.     class SeekbarvideoEvent implements SeekBar.OnSeekBarChangeListener{  
  206.         @Override  
  207.         public void onProgressChanged(SeekBar seekBar, int progress,  
  208.                 boolean fromUser) {  
  209.             // TODO Auto-generated method stub   
  210.               
  211.         }  
  212.         @Override  
  213.         public void onStartTrackingTouch(SeekBar seekBar) {  
  214.             // TODO Auto-generated method stub   
  215.             ischanging=true;//停止timer   
  216.         }  
  217.         @Override  
  218.         public void onStopTrackingTouch(SeekBar seekBar) {  
  219.             // TODO Auto-generated method stub   
  220.             media_video.seekTo(seekBar.getProgress());  
  221.             ischanging=false;//開啟timer   
  222.         }  
  223.     }  
  224. }  
Copyright © Linux教程網 All Rights Reserved