重新學了Android,寫了一個mediaplayer播放的程序實現音樂的播放和視頻的播放,程序效果圖如圖:
完成的基本功能有:1,讀取音樂文件 2.播放音樂文件 3,讀取視頻文件4,播放視頻文件
主要程序源代碼:
- package com.huangcheng.media;
-
- 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.media.MediaPlayer.OnCompletionListener;
- import android.os.Bundle;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.Toast;
-
- public class MediaDemoActivity extends Activity {
- //控制音樂
- private SeekBar seekbar_music;
- private Button start_music_btn;
- private Button stop_music_btn;
- //控制視頻
- private SeekBar seekbar_video;
- private Button start_video_btn;
- private Button stop_video_btn;
- private SurfaceView surfaceview_video;
- private SurfaceHolder surfaceview_holder;
-
- //播放器
- private MediaPlayer media=null;
- private MediaPlayer media_video=null;
- private Timer timer;
- private TimerTask timertask_music;
- private TimerTask timertask_video;
-
- //互斥量,seekbar拖動和Timer
- private volatile boolean ischanging=false;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- //控制media
- media=new MediaPlayer();
- media_video=new MediaPlayer();
- media.setOnCompletionListener(new OnCompletionListener() {
-
- @Override
- public void onCompletion(MediaPlayer arg0) {
- // TODO Auto-generated method stub
- Toast.makeText(MediaDemoActivity.this, "播放結束", Toast.LENGTH_SHORT).show();
- media.release();
- }
- });
- media_video.setOnCompletionListener(new OnCompletionListener() {
-
- @Override
- public void onCompletion(MediaPlayer arg0) {
- // TODO Auto-generated method stub
- Toast.makeText(MediaDemoActivity.this, "播放結束", Toast.LENGTH_SHORT).show();
- media_video.release();
- }
- });
- //Timer記錄播放時刻
- timer=new Timer();
- timertask_music=new TimerTask() {
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- //正在拖動seekbar,停止Timer
- if(ischanging==true){
- return;
- }
- else{
- seekbar_music.setProgress(media.getCurrentPosition());
- }
- }
- };
- timertask_video=new TimerTask() {
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- //正在拖動seekbar,停止Timer
- if(ischanging==true){
- return;
- }
- else{
- seekbar_video.setProgress(media_video.getCurrentPosition());
- }
- }
- };
-
- timer.schedule(timertask_music, 0, 10);//開始記錄,每10ms記錄一次
- timer.schedule(timertask_video, 0, 10);//開始記錄,每10ms記錄一次
- //更新控件
- seekbar_music=(SeekBar)findViewById(R.id.seekbar_music);
- seekbar_video=(SeekBar)findViewById(R.id.seekbar_video);
- start_music_btn=(Button)findViewById(R.id.start_music_btn);
- stop_music_btn=(Button)findViewById(R.id.stop_music_btn);
- start_video_btn=(Button)findViewById(R.id.start_video_btn);
- stop_video_btn=(Button)findViewById(R.id.stop_video_btn);
- surfaceview_video=(SurfaceView)findViewById(R.id.surfaceview_video);
- surfaceview_holder=surfaceview_video.getHolder();
- surfaceview_holder.setFixedSize(100, 100);
- surfaceview_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-
- seekbar_music.setEnabled(false);
- seekbar_video.setEnabled(false);
- // 添加處理seekbar事件
- seekbar_music.setOnSeekBarChangeListener(new SeekbarmusicEvent());
- seekbar_video.setOnSeekBarChangeListener(new SeekbarvideoEvent());
- //添加music按鍵處理
- start_music_btn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- media.reset();
- media=MediaPlayer.create(MediaDemoActivity.this, R.raw.m);
- seekbar_music.setMax(media.getDuration());
- try {
- media.prepare();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- media.start();
- seekbar_music.setEnabled(true);
- }
- });
- stop_music_btn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- media.stop();
- seekbar_music.setEnabled(false);
- }
- });
- start_video_btn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- media_video.reset();
- media_video=MediaPlayer.create(MediaDemoActivity.this, R.raw.v);
- seekbar_video.setMax(media_video.getDuration());
- media_video.setAudioStreamType(AudioManager.STREAM_MUSIC);
- media_video.setDisplay(surfaceview_holder);
- try {
- media_video.prepare();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- media_video.start();
- seekbar_video.setEnabled(true);
- }
- });
- stop_video_btn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- media_video.stop();
- seekbar_video.setEnabled(false);
- }
- });
-
- }
- //處理拖動seekbarmusic事件
- class SeekbarmusicEvent implements SeekBar.OnSeekBarChangeListener{
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- ischanging=true;//停止timer
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- media.seekTo(seekBar.getProgress());
- ischanging=false;//開啟timer
- }
- }
- class SeekbarvideoEvent implements SeekBar.OnSeekBarChangeListener{
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- ischanging=true;//停止timer
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- media_video.seekTo(seekBar.getProgress());
- ischanging=false;//開啟timer
- }
- }
- }