1. 程式人生 > >android中的Mediaplayer------可以同時開啟多個視訊播放--------------

android中的Mediaplayer------可以同時開啟多個視訊播放--------------

package com.example.testmultimediaplayer;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;


import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
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.TextView;

public class MainActivity extends Activity {
	private SurfaceView surfaceView;
	private MediaPlayer mediaPlayer;
	private String FilePath;

	private SurfaceView surfaceView2;
	private MediaPlayer mediaPlayer2;
	private String FilePath2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
         initData1();
         initData2();
	}

	private void initData1() {
//		FilePath="/sdcard/video/sishui.avi";
		FilePath="/sdcard/talshow.mp4";
		surfaceView = (SurfaceView) findViewById(R.id.sv);
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//設定視訊流型別
	
		mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mediaPlayer.start();
		         Log.i("sno","start mediaplayer1----------------");
			}
		});
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					mediaPlayer.setDisplay(surfaceView.getHolder());
					mediaPlayer.setDataSource(FilePath);
					mediaPlayer.prepareAsync();
				} catch (Exception e) {   ///在這裡增加播放失敗.
					mediaPlayer.release();
					if(mediaPlayer!=null)
						Log.i("sno","eeeeeeeeeeeeerrormediaPlayer!=null");
					e.printStackTrace();
				}
			}
		}, 200);
	}
	
	private void initData2() {
        FilePath2="/sdcard/video/sishui.avi";
		surfaceView2 = (SurfaceView) findViewById(R.id.sv2);
		mediaPlayer2 = new MediaPlayer();
		mediaPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);//設定視訊流型別
	
		mediaPlayer2.setOnPreparedListener(new OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mediaPlayer2.start();
		         Log.i("sno","start mediaPlayer2----------------");
			}
		});
		
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					mediaPlayer2.setDisplay(surfaceView2.getHolder());
					mediaPlayer2.setDataSource(FilePath2);
					mediaPlayer2.prepareAsync();
				} catch (Exception e) {   ///在這裡增加播放失敗.
					mediaPlayer2.release();
					if(mediaPlayer2!=null)
						Log.i("sno","eeeeeeeeeeeeerrormediaPlayer!=null");
					e.printStackTrace();
				}
			}
		}, 200);
	}
	
}
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <SurfaceView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>

     <SurfaceView
        android:id="@+id/sv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1" />
         <SurfaceView
        android:id="@+id/sv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"  />
	

</LinearLayout>