• Android 中MediaPlayer播放音频


    1、播放/res/raw/目录下的音频资源

    public class MainActivity extends Activity {
    
        MediaPlayer mediaPlayer;
        int position = 0;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
                
        }
    
        public void start(View v) {
            try {
                mediaPlayer = MediaPlayer.create(this, R.raw.mus);
                if (mediaPlayer != null ) {
                    mediaPlayer.stop(); 
                    position = 0;
                }
                mediaPlayer.prepare();
                if (position > 0) mediaPlayer.seekTo(position);
                mediaPlayer.start();        
            }catch (Exception e) {
                e.printStackTrace();
            }
            
        }
        
        public void stop(View v) {
            mediaPlayer.stop();
        }
    
        public void pause(View v) {
            position = mediaPlayer.getCurrentPosition();
            mediaPlayer.pause();
        }
        
        @Override
        protected void onDestroy() {
            mediaPlayer.release();
            super.onDestroy();
        }
    
    }

    2、播放 assets目录下的音频资源

    package com.test.mediaplayer;
    
    import java.io.IOException;
    
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.AssetManager;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        MediaPlayer mediaPlayer;
        int position = 0;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        
        }
    
        public void start(View v) {        
            try {
                mediaPlayer = new MediaPlayer();
                AssetManager am = getAssets();
                AssetFileDescriptor afd = am.openFd("mus.mp3");
                mediaPlayer.setDataSource(afd.getFileDescriptor());
                mediaPlayer.prepare();
                if (position > 0) mediaPlayer.seekTo(position);
                mediaPlayer.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        public void stop(View v) {
            mediaPlayer.stop();
        }
    
        public void pause(View v) {
            position = mediaPlayer.getCurrentPosition();
            mediaPlayer.pause();
        }
        
        @Override
        protected void onDestroy() {
            mediaPlayer.release();
            super.onDestroy();
        }
    
    }

    3、播放sdCard上的音频资源

    package com.test.mediaplayer;
    
    import java.io.IOException;
    
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.AssetManager;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        MediaPlayer mediaPlayer;
        int position = 0;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        
        }
    
        public void start(View v) {        
            try {
                mediaPlayer = new MediaPlayer();
                mediaPlayer.setDataSource("/mnt/sdcard/mus.mp3");
                mediaPlayer.prepare();
                if (position > 0) mediaPlayer.seekTo(position);
                mediaPlayer.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        public void stop(View v) {
            mediaPlayer.stop();
        }
    
        public void pause(View v) {
            position = mediaPlayer.getCurrentPosition();
            mediaPlayer.pause();
        }
        
        @Override
        protected void onDestroy() {
            mediaPlayer.release();
            super.onDestroy();
        }
    
    }

    4、播放网络音频资源使用 MediaPlayer的setDataSource(Context context, Uri uri)方法,Uri为音频文件的网络路径。

    使用SoundPool来播放密集、短促的音效。MediaPlayer的缺点是:资源占用量高、延迟时间长,不支持多个音频同时播放,SoundPool虽然可以一次性加载多个声音,但是由于内存限制,因此应该避免使用SoundPool来播放歌曲或者做游戏背景。

  • 相关阅读:
    十八、分页查询
    十七、oracle的权限和角色
    十六、同义词(synonyms)
    十五、序列
    十四、禁用与启动约束
    十三、修改表时添加约束
    十二、约束
    十一、修改表格
    十、事务(Transaction)
    Windows 远程连接后,自动断开,所有程序都自动关闭(待验证,待更新)
  • 原文地址:https://www.cnblogs.com/heml/p/3593762.html
Copyright © 2020-2023  润新知