Android MediaPlayer+SurfaceView实现视频播放

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText 
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edt"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    >
        <Button 
            android:id="@+id/btnPlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放"/>

        <Button 
            android:id="@+id/btnPause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暂停"/>
            <Button 
            android:id="@+id/btnStop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"/>
    </LinearLayout>

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="240dp"/>
</LinearLayout>

MainActivity

package com.sun.activity;

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

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
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.EditText;

public class MainActivity extends Activity implements OnClickListener{
    MediaPlayer mediaPlayer;
    SurfaceHolder surfaceHolder;
    EditText edt;
    String path;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonPause = (Button) findViewById(R.id.btnPause);
        Button buttonPlay = (Button) findViewById(R.id.btnPlay);
        Button buttonStop = (Button) findViewById(R.id.btnStop);
        buttonPause.setOnClickListener(this);
        buttonPlay.setOnClickListener(this);
        buttonStop.setOnClickListener(this);

         edt=(EditText) findViewById(R.id.edt);

         mediaPlayer=   new MediaPlayer();

        SurfaceView surfaceView= (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder=surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.setFixedSize(176, 144);
        surfaceHolder.setKeepScreenOn(true);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
        case R.id.btnPlay:
            try {
                File file=new File(Environment.getExternalStorageDirectory(),edt.getText().toString());
                Log.v("aaaaa", file.getPath());
                if(file.exists())
                {

                     path=file.getAbsolutePath();
                     play();
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            break;
        }

    }

    class Prepared implements OnPreparedListener{

        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();

        }

    }

    private void play() {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.setDisplay(surfaceHolder);
            mediaPlayer.prepare();
            mediaPlayer.setOnPreparedListener(new Prepared());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日有所思的暖树

对您有帮助给点鼓励吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值