设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 667|回复: 3

How to play video and audio on Android

  [复制链接]
发表于 2010-11-17 15:19:45 | 显示全部楼层 |阅读模式
                                                    SDK Version:
                    M3        
        

There is more than one way, to play media files on an Android phone, let me show you two of them.
Audio:
MediaPlayer is the easier way, if you just want to play an audio file in the background, somewhere in an appliaction. There are no ui controls here, but of course you can use MediaPlayer.stop(), play(), seekTo() ,etc. Just bind the needed functions to a button, gesture, or event. As you can see, it also throws a lot of exceptions, which you need to catch.
  • public void audioPlayer(String path, String fileName){
  •     //set up MediaPlayer   
  •     MediaPlayer mp = new MediaPlayer();

  •     try {
  •         mp.setDataSource(path+"/"+fileName);
  •     } catch (IllegalArgumentException e) {
  •         // TODO Auto-generated catch block
  •         e.printStackTrace();
  •     } catch (IllegalStateException e) {
  •         // TODO Auto-generated catch block
  •         e.printStackTrace();
  •     } catch (IOException e) {
  •         // TODO Auto-generated catch block
  •         e.printStackTrace();
  •     }
  •     try {
  •         mp.prepare();
  •     } catch (IllegalStateException e) {
  •         // TODO Auto-generated catch block
  •         e.printStackTrace();
  •     } catch (IOException e) {
  •         // TODO Auto-generated catch block
  •         e.printStackTrace();
  •     }
  •     mp.start();
  • }



Video:
In this example, I open a video file, and decide if I want it to autoplay after it is loaded.
You propably want to store your video files, on the sd card. You can get the path to the sd card via: Environment.getExternalStorageDirectory().
  • public void videoPlayer(String path, String fileName, boolean autoplay){
  •     //get current window information, and set format, set it up differently, if you need some special effects
  •     getWindow().setFormat(PixelFormat.TRANSLUCENT);
  •     //the VideoView will hold the video
  •     VideoView videoHolder = new VideoView(this);
  •     //MediaController is the ui control howering above the video (just like in the default youtube player).
  •     videoHolder.setMediaController(new MediaController(this));
  •     //assing a video file to the video holder
  •     videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
  •     //get focus, before playing the video.
  •     videoHolder.requestFocus();
  •     if(autoplay){
  •         videoHolder.start();
  •     }

  • }


In this part, I'm going to focus on MediaPlayer. I'm going to show you, how to stop it properly, and how to create a media player more easily.
The default behavior is the same, with every activity, after onPause and onStop, if the activity is not destroyed, it will continue to run in the background, until it is killed so the system can free up the memory it is using. So until the activity is destroyed, the mediaplayer will continue to occupy space in the phones memory.
  • private MediaPlayer mp;

  • public void onCreate(Bundle savedInstanceState) {
  •     super.onCreate(savedInstanceState);
  •     mp=new MediaPlayer();
  •     audioPlayer(context,uri);
  • }

  • public void audioPlayer(Context context,Uri uri){
  •     mp= MediaPlayer.create(context, uri);
  •     mp.start();
  • }



From the official docs:
"public static MediaPlayer create (Context context, Uri uri)
Convenience method to create a MediaPlayer for a given Uri. On success, prepare() will already have been called and must not be called again.
When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception."
I have yet to get an exception from mediaplayer, but to make sure it won't happen, don't forget to realese(), at least in onDestroy()!
  • @Override
  • protected void onPause() {
  •     super.onPause();
  •     if(mp.isPlaying()){ //you need to check if it is playing first, or you might get a null pointer exception!
  •         mp.stop();
  •     }
  • }



Depending on your needs, you can put the mp.stop() in the onPause part, so if the activity goes to the background for whatever reason, it will only pause the playback, and it can continue it in onResume(). Just put mp.play() in onResume().
  • @Override
  • public void onDestroy(){
  • super.onDestroy();
  •     mp.release();
  • }



If you don't release the media player in the onDestroy part, it will continue to be in the memory, after the activity is stopped. The OS will handle this issue for you if it runs out of memory, but it's nicer, if You do it, so the OS will run out of memory later (much later in case of huge files).
As an alternative, you could set up an onCompletionListener, so when the media playback is done, it will release the media player.
  • mp.setOnCompletionListener(new OnCompletionListener(){
  •     @Override
  •     public void onCompletion(MediaPlayer mp) {
  •         mp.release();
  •     }
  • });






发表于 2011-6-7 01:50:32 | 显示全部楼层
楼主的这个帖子不错哈

千年遇好帖,遇到必须顶

顶一下看看


Millasha,Millasha,Millasha,Millasha官网地址,Millasha
发表于 2011-6-16 05:44:42 | 显示全部楼层
楼主的这个帖子不错哈

千年遇好帖,遇到必须顶

顶一下看看


Millasha,Millasha,Millasha,Millasha官网地址,Millasha
发表于 2011-10-29 13:11:59 | 显示全部楼层
大家顶啊,这么好的帖子,楼主辛苦了
发表于 2011-12-18 05:55:30
不错!不错!不错!













支持 反对

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 13:52 , Processed in 0.015712 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表