Android学习笔记_25_多媒体之在线播放器

一、布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#FFFFFF"

    android:orientation="vertical" >



    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/filename" />



    <EditText

        android:id="@+id/filename"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="11.RMVB" />



    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >



        <ImageButton

            android:id="@+id/play"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/play" />



        <ImageButton

            android:id="@+id/pause"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/pause" />



        <ImageButton

            android:id="@+id/reset"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/reset" />



        <ImageButton

            android:id="@+id/stop"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/stop" />

    </LinearLayout>



    <SurfaceView

        android:id="@+id/surfaceView"

        android:layout_width="fill_parent"

        android:layout_height="240dip" />



</LinearLayout>
View Code

二、代码实现:

package com.example.video;



import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;



import android.R.integer;

import android.app.Activity;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.media.MediaPlayer.OnPreparedListener;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.widget.EditText;

import android.widget.ImageButton;

import android.widget.Toast;



public class MainActivity extends Activity {

    private static final String TAG = "VideoActivity";

    private EditText filenameText;

    private SurfaceView surfaceView;

    private MediaPlayer mediaPlayer;

    private String filename;// 当前播放文件的名称

    private int position;// 记录播放位置

    private boolean pause=false;//是否被暂停

    private  File file;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        this.mediaPlayer = new MediaPlayer();

        this.filenameText = (EditText) this.findViewById(R.id.filename);

        this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);

        ImageButton playButton = (ImageButton) this.findViewById(R.id.play);

        ImageButton pauseButton = (ImageButton) this.findViewById(R.id.pause);

        ImageButton resetButton = (ImageButton) this.findViewById(R.id.reset);

        ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop);



        ButtonClickListener listener = new ButtonClickListener();

        playButton.setOnClickListener(listener);

        pauseButton.setOnClickListener(listener);

        resetButton.setOnClickListener(listener);

        stopButton.setOnClickListener(listener);



        /* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */

        this.surfaceView.getHolder().setType(

                SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        this.surfaceView.getHolder().setFixedSize(176, 144);// 设置分辨率

        //让屏幕高亮,不要锁频

        this.surfaceView.getHolder().setKeepScreenOn(true);

        this.surfaceView.getHolder().addCallback(new SurfaceListener());

    }



    private class ButtonClickListener implements View.OnClickListener {

        @Override

        public void onClick(View v) {

            try {

                switch (v.getId()) {

                case R.id.play:// 来自播放按钮

                    filename = filenameText.getText().toString();

                    file=new File("/storage/sdcard0/Music",filename);

                    if (file.exists()) {                        

                        play(0);

                    }else {

                        Toast.makeText(getApplicationContext(), "文件不存在", Toast.LENGTH_LONG).show();

                    }

                    break;



                case R.id.pause:// 来自暂停按钮

                    if (mediaPlayer.isPlaying()) {

                        mediaPlayer.pause();

                        pause=true;

                    } else {

                        if (pause) {

                            mediaPlayer.start();

                            pause=false;

                        }

                    }

                    break;



                case R.id.reset:// 来自重新播放按钮

                    if (mediaPlayer.isPlaying()) {

                        mediaPlayer.seekTo(0);

                    }else {

                        if(file!=null){

                            play(0);

                        }

                    }

                    break;

                case R.id.stop:// 来自停止按钮

                    if (mediaPlayer.isPlaying())

                        mediaPlayer.stop();

                    break;

                }

            } catch (Exception e) {

                Log.e(TAG, e.toString());

            }

        }

    }



    /**

     * 播放视频

     */

    private void play(int position) throws IOException {

        mediaPlayer.reset();

        //mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mediaPlayer.setDataSource(file.getAbsolutePath());// 设置需要播放的视频

        mediaPlayer.setDisplay(surfaceView.getHolder());// 把视频画面输出到SurfaceView

        mediaPlayer.prepare();//进行缓冲处理,完成之后播放

        mediaPlayer.setOnPreparedListener(new PrepareListener(position));

        

    }

    private final class PrepareListener implements OnPreparedListener{

        private int position;

        public PrepareListener(int position) {

            this.position=position;

        }



        //当缓冲完成之后就会调用onPrepared方法

        @Override

        public void onPrepared(MediaPlayer mp) {

            mediaPlayer.start();//播放

            if (position>0) {

                mediaPlayer.seekTo(position);

            }

        }

    }



    private class SurfaceListener implements SurfaceHolder.Callback {

        @Override

        public void surfaceChanged(SurfaceHolder holder, int format, int width,

                int height) {

        }



        //当画面再次回到前台时调用

        @Override

        public void surfaceCreated(SurfaceHolder holder) {// 方法在onResume()后被调用

            Log.i(TAG, "surfaceCreated()");

            if (position > 0 && filename != null) {

                try {

                    play(position);

                    position = 0;

                } catch (Exception e) {

                    Log.e(TAG, e.toString());

                }

            }

        }



        @Override

        public void surfaceDestroyed(SurfaceHolder holder) {

            if (mediaPlayer.isPlaying()) {

                position = mediaPlayer.getCurrentPosition();// 得到播放位置

                mediaPlayer.stop();

            }

            Log.i(TAG, "surfaceDestroyed()");

        }

    }



    @Override

    protected void onDestroy() {

        if (mediaPlayer.isPlaying())

            mediaPlayer.stop();

        mediaPlayer.release();

        super.onDestroy();

    }

    

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }



}

 

你可能感兴趣的:(Android学习)