Android下用Media Player实现一个最简单的视频播放器

1. 首先建立一个空的Android工程;

2. 编辑¨res -> layout -> fragment_main.xml¨文件,添加如下控件:

视频显示窗口:

开始按钮:


停止按钮:


3. 编辑¨MainActivity.java¨:

1)创建MediaPlayer

private static MediaPlayer mp = new MediaPlayer();

2)在PlaceholderFragment类onCreateView方法中,获取上述添加的三个控件,并为开始和结束按钮添加消息处理方法。

其中,开始按钮的消息处理方法中包括

mp.reset();
mp.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/Movies/big_buck_bunny.mp4");
mp.setDisplay(surfaceHolder);
mp.prepare();
mp.start();
(此处为了尽量简单,文件路径是写死的,需要预先将视频文件保存到指定路径。这里的路径为/storage/emulated/0/Movies/big_buck_bunny.mp4)

结束按钮的消息处理方法中包括

mp.stop();
4. 编辑¨AndroidMainfest.xml¨文件,添加如下代码:

上面这句很重要,否则程序将无法访问保存在sd卡上的文件。


完整的代码和配置文件如下:

AndroidManifest.xml




    

    
        
            
                

                
            
        
    
	

fragment_main.xlm



    

    

    
MainActivity.java

package com.example.mediaplayerjavademo;

import java.io.File;
import java.io.IOException;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
	private static Button btplay, btstop;
	private static MediaPlayer mp = new MediaPlayer();
	private static SurfaceView surfaceView;
	private static SurfaceHolder surfaceHolder; 
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            
            surfaceView = (SurfaceView) rootView.findViewById(R.id.surfaceView1);
            surfaceHolder = surfaceView.getHolder();
            btplay = (Button)rootView.findViewById(R.id.play);
            btstop = (Button)rootView.findViewById(R.id.stop);
            
            btplay.setOnClickListener(new OnClickListener(){
            	@Override
                public void onClick(View v) {
            		try {
            			mp.reset();
                        mp.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/Movies/big_buck_bunny.mp4");
                        mp.setDisplay(surfaceHolder);
                        mp.prepare();
                        mp.start();
                     } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                     } catch (IllegalStateException e) {
                        e.printStackTrace();
                     } catch (IOException e) {
                        e.printStackTrace();
                     }
            	}
            });
            
            btstop.setOnClickListener(new OnClickListener(){
            	@Override
                public void onClick(View v) {
            		mp.stop();
            	}
            });
            
            return rootView;
        }
    }
}

运行效果

Android下用Media Player实现一个最简单的视频播放器_第1张图片

你可能感兴趣的:(Android)