android 播放声音、视频应用
2017-02-07 22:09
423 查看
这是入口
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>
相关文章推荐
- 【bug整理】Android声音并发问题:播放视频的时候,用第三方应用播放音乐,存在音视频声音并发问题
- android 用MyTV等第三方应用播放直播视频,只有声音没有画面
- JNI在Android开发中的应用之--用AudioRecord录音,用AudioTrack播放声音.
- android Mediaplayer 播放视频 只有声音没有画面的问题
- Android 多媒体应用:视频播放之VideoView与SurfaceView
- Android WebView页面结束后视频播放还有声音问题处理
- android之视频播放控件VideoView简单应用
- Android 使用intent打开手机自带应用播放视频,音频,文档,还有打开应用市场
- 【Android开发】多媒体应用开发-使用VideoView播放视频
- Android WebView 播放视频的问题(只有声音没有图像)
- android之视频播放控件VideoView简单应用
- android播放html5视频,只有声音没有图像视频
- Android应用开发学习笔记之播放视频
- Android 多媒体应用——MediaPlayer与SurfaceView播放视频
- Android声音和视频播放
- 我的Android进阶之旅------>android视频播放只有声音无画面的解决办法
- Android 多媒体应用:视频播放之VideoView与SurfaceView
- 播放android应用工作中的视频文件
- android之视频播放系统VideoView和自定义VideoView控件的应用
- android hwcomposer 在视频播放中的应用