【Android】播放視頻的簡易播放器源碼

來源:互聯網
上載者:User

一,MainActivity.java源碼

import android.app.Activity;import android.graphics.PixelFormat;import android.media.AudioManager;import android.media.MediaPlayer;import android.os.Bundle;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.widget.ImageButton;import android.widget.TextView;import android.widget.Toast;public class EX07_14 extends Activityimplements SurfaceHolder.Callback{  private TextView mTextView01;  private static final String TAG = "HIPPO_MediaPlayer"; //列印日誌的標誌    private MediaPlayer mMediaPlayer01;    private SurfaceView mSurfaceView01;    private SurfaceHolder mSurfaceHolder01;    private ImageButton mPlay;  private ImageButton mPause;  private ImageButton mReset;  private ImageButton mStop;    private boolean bIsPaused = false;  private boolean bIsReleased = false;  private String strVideoPath = "";    @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);        if(!checkSDCard()) //如果沒有SD卡    {      mMakeTextToast      (        getResources().getText(R.string.str_err_nosd).toString(),        true      );    }        mTextView01 = (TextView)findViewById(R.id.myTextView1);        getWindow().setFormat(PixelFormat.UNKNOWN);        mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1); //顯示動畫用的容器        mSurfaceHolder01 = mSurfaceView01.getHolder();    mSurfaceHolder01.addCallback(this);    mSurfaceHolder01.setFixedSize(176,144);    mSurfaceHolder01.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        mPlay = (ImageButton) findViewById(R.id.play);     mPause = (ImageButton) findViewById(R.id.pause);     mReset = (ImageButton) findViewById(R.id.reset);     mStop = (ImageButton) findViewById(R.id.stop);        strVideoPath = "/sdcard/a.3gp";        mPlay.setOnClickListener(new ImageButton.OnClickListener()    {       public void onClick(View view)      {        if(checkSDCard())        {          playVideo(strVideoPath);        }      }    });        mPause.setOnClickListener(new ImageButton.OnClickListener()    {      public void onClick(View view)      {        if(checkSDCard())        {          if (mMediaPlayer01 != null)          {            if(bIsReleased == false)            {              if(bIsPaused==false)              {                mMediaPlayer01.pause();                bIsPaused = true;                mTextView01.setText(R.string.str_pause);              }              else if(bIsPaused==true)              {                mMediaPlayer01.start();                bIsPaused = false;                mTextView01.setText(R.string.str_play);              }            }          }        }      }    });        mReset.setOnClickListener(new ImageButton.OnClickListener()    {       public void onClick(View view)      {        if(checkSDCard())        {          if(bIsReleased == false)          {            if (mMediaPlayer01 != null)            {               mMediaPlayer01.seekTo(0);            }          }        }      }     });        mStop.setOnClickListener(new ImageButton.OnClickListener()    {       public void onClick(View view)      {        if(checkSDCard())        {          if (mMediaPlayer01 != null)          {            if(bIsReleased==false)            {              mMediaPlayer01.stop();              mMediaPlayer01.release();              bIsReleased = true;              mTextView01.setText(R.string.str_stop);            }                    }        }      }    });  }    private void playVideo(String strPath)  {     mMediaPlayer01 = new MediaPlayer();    mMediaPlayer01.setAudioStreamType(AudioManager.STREAM_MUSIC);        mMediaPlayer01.setDisplay(mSurfaceHolder01);        try    {       mMediaPlayer01.setDataSource(strPath);    }    catch (Exception e)    {       // TODO Auto-generated catch block      mTextView01.setText("setDataSource Exceeption:"+e.toString());    }        try    {       mMediaPlayer01.prepare();    }    catch (Exception e)    {       // TODO Auto-generated catch block      mTextView01.setText("prepare Exceeption:"+e.toString());    }    mMediaPlayer01.start();    bIsReleased = false;    mTextView01.setText(R.string.str_play);        mMediaPlayer01.setOnCompletionListener    (new MediaPlayer.OnCompletionListener()    {      @Override      public void onCompletion(MediaPlayer arg0)      {        // TODO Auto-generated method stub        mTextView01.setText(R.string.str_stop);      }    });  }    private boolean checkSDCard()  {    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))    {      return true;    }    else    {      return false;    }  }    public void mMakeTextToast(String str, boolean isLong)  {    if(isLong==true)    {      Toast.makeText(EX07_14.this, str, Toast.LENGTH_LONG).show();    }    else    {      Toast.makeText(EX07_14.this, str, Toast.LENGTH_SHORT).show();    }  }    @Override  public void surfaceChanged  (SurfaceHolder surfaceholder, int format, int w, int h)  {    // TODO Auto-generated method stub    Log.i(TAG, "Surface Changed");  }    @Override  public void surfaceCreated(SurfaceHolder surfaceholder)  {    // TODO Auto-generated method stub    Log.i(TAG, "Surface Changed");  }    @Override  public void surfaceDestroyed(SurfaceHolder surfaceholder)  {    // TODO Auto-generated method stub    Log.i(TAG, "Surface Destroyed");  }}

二,main.xml 源碼

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:background="@drawable/white"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <TextView    android:id="@+id/myTextView1"    android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:textColor="@drawable/blue"     android:text="@string/hello"  />  <SurfaceView    android:id="@+id/mSurfaceView1"     android:layout_width="100px"     android:layout_height="100px">   </SurfaceView>    <SeekBar android:id="@+id/seekBar" android:layout_height="wrap_content" android:layout_width="fill_parent" />       <LinearLayout     android:orientation="horizontal"     android:layout_height="wrap_content"     android:layout_width="fill_parent"     android:padding="10dip"   >  <ImageButton android:id="@+id/play"     android:layout_height="wrap_content"     android:layout_width="wrap_content"    android:src="@drawable/play"  />   <ImageButton android:id="@+id/pause"    android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:src="@drawable/pause"  />   <ImageButton android:id="@+id/reset"     android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:src="@drawable/reset"  />   <ImageButton android:id="@+id/stop"     android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:src="@drawable/stop"  />   </LinearLayout> </LinearLayout>

三,源碼中所需其餘圖片等 ,自備就可以。經測試通過可以播放,前提需要sdcard中已經傳入播放檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.