Android利用Mediapalyer播放本地资源文件声音

  1. 首先在res下创建raw,然后将mp3音乐拷贝到raw下
  2. 直接贴代码吧
//开始播放声音
public class PlayVoice {
    private static MediaPlayer mediaPlayer;

    public static void playVoice(Context context){
        try {
            mediaPlayer= MediaPlayer.create(context, R.raw.opening);
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //停止播放声音
    public  static void stopVoice(){
        if(null!=mediaPlayer) {
            mediaPlayer.stop();
        }
    }
}

 

posted @ 2018-11-07 11:41  云中雀  阅读(855)  评论(0编辑  收藏  举报