首页 > 系统 > Android > 正文

android 播放声音、视频应用

2019-11-09 17:30:16
字体:
来源:转载
供稿:网友
这是入口
package com.example.soundpooldemo;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.media.AudioManager;import android.media.MediaPlayer;import android.media.SoundPool;import android.os.Bundle;import android.view.View;import android.view.View.MeasureSpec;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {	PRivate Button btn_playSoundpool;	private SoundPool sp;	private int sountID;	private Button btn_mediaplayer;	private MediaPlayer mp;	private Button btn_player;	private Button btn_videoView;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);				btn_playSoundpool=(Button) findViewById(R.id.btn_playSoundpool);		btn_mediaplayer=(Button) findViewById(R.id.btn_mediaplayer);		btn_videoView=(Button) findViewById(R.id.btn_videoView);		btn_player=(Button) findViewById(R.id.btn_player);		sp=new SoundPool(1, AudioManager.STREAM_MUSIC, 0);		sountID=sp.load(this, R.raw.like, 1);		btn_playSoundpool.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				//SoundPool只能播放短声音				sp.play(sountID, 1, 1, 0, 0, 1);			}		});		btn_mediaplayer.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				if(mp!=null){					mp.start();				}							}		});		btn_player.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				Intent video=new Intent();				video.setClass(MainActivity.this, PlayVideo.class);				startActivity(video);							}		});		btn_videoView.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				Intent video=new Intent();				video.setClass(MainActivity.this, VideoViewPlay.class);				startActivity(video);							}		});	}	@Override	protected void onResume() {		// TODO Auto-generated method stub		mp=MediaPlayer.create(this, R.raw.like);		try {			mp.prepare();		} catch (IllegalStateException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		super.onResume();	}	@Override	protected void onPause() {		// TODO Auto-generated method stub		if(mp!=null){			mp.release();		}		super.onPause();	}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btn_playSoundpool"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="SoundPool播放短声音" />    <Button	    android:id="@+id/btn_mediaplayer"	    android:layout_width="fill_parent"	    android:layout_height="wrap_content"	    android:text="MediaPlayer播放长长声音" />    <Button	    android:id="@+id/btn_player"	    android:layout_width="fill_parent"	    android:layout_height="wrap_content"	    android:text="MediaPlayer播放mp4视频" />     <Button	    android:id="@+id/btn_videoView"	    android:layout_width="fill_parent"	    android:layout_height="wrap_content"	    android:text="btn_videoView播放mp4视频" /></LinearLayout>

这是PlayVideo播放视频的

package com.example.soundpooldemo;import java.io.IOException;import android.app.Activity;import android.media.MediaPlayer;import android.os.Bundle;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;public class PlayVideo extends Activity {	private SurfaceView sv;	private SurfaceHolder holder;	private Callback callBalk=new Callback() {		private MediaPlayer mp;				@Override		public void surfaceDestroyed(SurfaceHolder holder) {			// TODO Auto-generated method stub			mp.release();		}				@Override		public void surfaceCreated(SurfaceHolder holder) {			// TODO Auto-generated method stub			mp=MediaPlayer.create(PlayVideo.this, R.raw.ren);			try {					mp.prepare();			} catch (IllegalStateException e) {				// TODO Auto-generated catch block				e.printStackTrace();			} catch (IOException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}			mp.setDisplay(holder);			mp.start();		}				@Override		public void surfaceChanged(SurfaceHolder holder, int format, int width,				int height) {			// TODO Auto-generated method stub					}	};	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		sv=new SurfaceView(this);		holder=sv.getHolder();		holder.addCallback(callBalk);		setContentView(sv);	}	}这是VideoView播放视频

package com.example.soundpooldemo;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.widget.MediaController;import android.widget.Toast;import android.widget.VideoView;public class VideoViewPlay extends Activity {	private VideoView videoView;	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.video);			 //本地的视频 需要在手机SD卡根目录添加一个 fl1234.mp4 视频	    String videoUrl1 = Environment.getExternalStorageDirectory().getPath()+"/fl1234.mp4" ;	 	    //网络视频	    String videoUrl2 = Utils.videoUrl ;	 	    Uri uri = Uri.parse( videoUrl2 );	 	    videoView = (VideoView) findViewById(R.id.videoView);	 	    //设置视频控制器	    videoView.setMediaController(new MediaController(this));	 	    //播放完成回调	    videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());	 	    //设置视频路径	    videoView.setVideoURI(uri);	    //设置隐藏播放视频进度条、播放、暂停、上一个、下一个	    MediaController mc = new MediaController(this);	    mc.setVisibility(View.INVISIBLE);	    videoView.setMediaController(mc);	    	    //开始播放视频	    videoView.start();	  }	 	  class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {	 	    @Override	    public void onCompletion(MediaPlayer mp) {	      Toast.makeText( VideoViewPlay.this, "播放完成了", Toast.LENGTH_SHORT).show();	    }	  }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <VideoView      android:id="@+id/videoView"      android:layout_width="match_parent"      android:layout_height="match_parent"      /> </RelativeLayout>
package com.example.soundpooldemo;public class Utils {	public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ;}最后是xml配制文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.soundpooldemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="17"        android:targetSdkVersion="17" />    <uses-permission android:name="android.permission.INTERNET"></uses-permission>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/APPTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name="com.example.soundpooldemo.PlayVideo"           android:screenOrientation="landscape"           android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" ></activity>       <activity android:name="com.example.soundpooldemo.VideoView"           android:screenOrientation="landscape"           android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" ></activity>       <activity android:name="com.example.soundpooldemo.VideoViewPlay"           android:screenOrientation="landscape"           android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" ></activity>    </application></manifest>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表