多媒体(Multimedia)是多种媒体的综合,一般包括文本,声音和图像等多种媒体形式。在计算机系统中,多媒体指组合两种或两种以上媒体的一种人机交互式信息交流和传播媒体。使用的媒体包括文字、图片、照片、声音 、动画和影片,以及程式所提供的互动功能。
- AsyncPlayer
- AsyncPlayer是一个异步播放类,它可以不在主UI上播放音频,使用到的方法是play()、stop();
-
public void play(Context context, Uri uri, boolean looping, int stream) 分别需要传入上下文对象、媒体的地址、是否循环、以音频流类型,下面我们来一个放出实例代码:
-
AsyncPlayer mAsync = new AsyncPlayer(TAG); mAsync.play(this, Uri.parse("file://" + Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_RINGTONES) + "/perry_ringtone.mp3"), false, AudioManager.STREAM_MUSIC);
- MediaStore实现拍照的功能
-
ContentValues values = new ContentValues();//创建一个contentValues对象 values.put(Media.TITLE, "My demo image");//put图片的标题与描述 values.put(Media.DESCRIPTION, "Image Captured by Camera via an Intent"); //创建一个图像保存路径 myPicture = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//new一个对象 i.putExtra(MediaStore.EXTRA_OUTPUT, myPicture);//put出保存路径 //如果我们需要指定保存到其他地方,可以使用以下的方法,创建一个文件. //myImageFile = new File(Environment.getExternalStorageDirectory()+"/test.jpg"); //myImageFile.delete(); //myPicture = Uri.fromFile(myImageFile); //i.putExtra(MediaStore.EXTRA_OUTPUT, myPicture); startActivityForResult(i, 0);//启动activity
-
@Override//在拍照完成的时候我们需要在这个方法中取出相片的URI protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==0 && resultCode==Activity.RESULT_OK) { Intent inn = new Intent(Intent.ACTION_VIEW); Log.v(TAG, "myPicture is " + myPicture.toString()); inn.setData(myPicture); startActivity(inn); } }
-
- SoundPool它可以同时播放多个音频
-
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, SRC_QUALITY);//申明一个soundpool对象;第一个参数是最大播放数量;第二个是音频类型;第三个是品质,这里传入的是0.
-
soundPool.setOnLoadCompleteListener(this);//接着我们设置一个监听器
-
sid_background = soundPool.load(this, R.raw.crickets, PRIORITY);//加载一个音频文件 sid_chimp = soundPool.load(this, R.raw.chimp, PRIORITY);//加载第二个音频文件,以此类推 sid_rooster = soundPool.load(this, R.raw.rooster, PRIORITY); sid_roar = soundPool.load(this, R.raw.roar, PRIORITY); try { AssetFileDescriptor afd = this.getAssets().openFd("dogbark.mp3");//这里加载了一个非APK内的一个音频文件 sid_bark = soundPool.load(afd.getFileDescriptor(), 0, afd.getLength(), PRIORITY); afd.close(); } catch (IOException e) { e.printStackTrace(); }
-
@Override//然后我们在加载完成后的监听方法中进行音频播放 public void onLoadComplete(SoundPool sPool, int sid, int status) { Log.v("soundPool", "sid " + sid + " loaded with status " + status); final float currentVolume = ((float)aMgr.getStreamVolume(AudioManager.STREAM_MUSIC)) / ((float)aMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); if(status != 0) return; if(sid == sid_background) { if(sPool.play(sid, currentVolume, currentVolume, PRIORITY, -1, 1.0f) == 0) Log.v("soundPool", "Failed to start sound"); } else if(sid == sid_chimp) { queueSound(sid, 5000, currentVolume);//为了演示效果,这里使用队列,对其他音频进行队列播放,下面会给出队列的代码 } else if(sid == sid_rooster) { queueSound(sid, 6000, currentVolume); } else if(sid == sid_roar) { queueSound(sid, 12000, currentVolume); } else if(sid == sid_bark) { queueSound(sid, 7000, currentVolume); } }
- paly中参数详解:
Parameters
soundID a soundID returned by the load() function 声音资源 leftVolume left volume value (range = 0.0 to 1.0) 左音量值 rightVolume right volume value (range = 0.0 to 1.0) 右音量值 priority stream priority (0 = lowest priority) 优先流的优先级(0 =最低优先级) loop loop mode (0 = no loop, -1 = loop forever) 循环循环模式(0 =无环路,-1 =无限循环) rate playback rate (1.0 = normal playback, range 0.5 to 2.0) 率播放速率(1.0=正常播放,范围0.5〜2.0) Returns
- non-zero streamID if successful, zero if failed 非零流ID如果成功,零,如果失败
-
private void queueSound(final int sid, final long delay, final float volume) { new Handler().postDelayed(new Runnable() { @Override public void run() { if(soundPool == null) return; if(soundPool.play(sid, volume, volume, PRIORITY, 0, 1.0f) == 0) Log.v("soundPool", "Failed to start sound (" + sid + ")"); queueSound(sid, delay, volume); }}, delay);//延时启动线程 }
- 在程序挂起的时候我们记得释放资源:
@Override protected void onPause() { soundPool.release(); soundPool = null; super.onPause(); }
-
-
mediaPlayer
- 使用mediaPlayer来播放音乐:
-
mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(AUDIO_PATH);//资源可以是网络音频,也可以是本地音频 mediaPlayer.setOnPreparedListener(this);//设置一个监听器 mediaPlayer.prepareAsync();//异步编译
static final String AUDIO_PATH = "http://lanxinbase.com/KUHF.m3u"; // "http://lanxinbase.com/3389/play.mp3"; // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/music_file.mp3"; // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/movie.mp4";
public void onPrepared(MediaPlayer mp) { mp.start();//编译完成后进行播放 }