Android 播放音乐文件与视频文件

jopen 11年前

Android 播放音乐文件与视频文件

package cn.m15.test;    import android.app.Activity;  import android.media.MediaPlayer;  import android.net.Uri;  import android.os.Bundle;  import android.os.Environment;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.MediaController;  import android.widget.TextView;  import android.widget.VideoView;    public class TestActivity extends Activity {     Button mPlayMusic = null;   Button mStopMusic = null;   Button mPlayVido = null;   Button mStopVido = null;     TextView mTextView = null;   VideoView mVideoView;   Uri mUri;     MediaPlayer mMediaMusic = null;   MediaPlayer mMediaVido = null;     @Override   public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    mPlayMusic = (Button) findViewById(R.id.btn_01);    mStopMusic = (Button) findViewById(R.id.btn_02);    mPlayVido = (Button) findViewById(R.id.btn_03);    mStopVido = (Button) findViewById(R.id.btn_04);      // 创建MediaPlayer 这里用的音频格式是mp3    mMediaMusic = MediaPlayer.create(this, R.raw.music);      // 创建视频播放视图    mVideoView = (VideoView) findViewById(R.id.vidoView);      // sd卡下文件的路径(这是我录的一段汤姆猫的录像)    mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/TalkingFriends/TalkingTom.mp4");      // 设置MediaController    mVideoView.setMediaController( new MediaController(this));      // 设置音频循环播放    mMediaMusic.setLooping(true);      mTextView = (TextView) findViewById(R.id.textView);      // 播放声音    mPlayMusic.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {      mTextView.setText("开始播放音频");      if (!mMediaMusic.isPlaying()) {       mMediaMusic.start();      }       }    });    // 关闭声音    mStopMusic.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {      mTextView.setText("停止播放音频");      if (mMediaMusic.isPlaying()) {       mMediaMusic.stop();      }     }    });      // 播放视频    mPlayVido.setOnClickListener(new OnClickListener() {       @Override     public void onClick(View v) {      mTextView.setText("开始播放视频");      mVideoView.setVideoURI(mUri);      mVideoView.start();     }    });      // 关闭视频    mStopVido.setOnClickListener(new OnClickListener() {       @Override     public void onClick(View v) {      mTextView.setText("停止播放视频");      mVideoView.stopPlayback();     }    });   }  }
<?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"      >  <TextView        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:id="@+id/textView"      />      <Button android:id="@+id/btn_01" android:layout_height="50dip"     android:text="播放音频" android:layout_width="150dip"/>   <Button android:id="@+id/btn_02" android:layout_height="50dip"     android:text="关闭音频" android:layout_width="150dip"/>   <Button android:id="@+id/btn_03" android:layout_height="50dip"     android:text="播放视频" android:layout_width="150dip"/>   <Button android:id="@+id/btn_04" android:layout_height="50dip"     android:text="关闭视频" android:layout_width="150dip"/>   <VideoView   android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:id="@+id/vidoView"   />  </LinearLayout>

</span>