微信打开页面自动播放默认音乐

try {
(function() {
    var musicResources = 'http://ministe-d.qiniudn.com/alwayshere_wintersun_pazzaInter2.mp3';
    var ICON_playing = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADoAAAA6CAYAAADhu0ooAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA4JpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDY3IDc5LjE1Nzc0NywgMjAxNS8wMy8zMC0yMzo0MDo0MiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDowMzgwMTE3NDA3MjA2ODExODIyQURENjIwMzQ0NjY3MCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDo5NTg1NjA3QTVEMjgxMUU2ODNGMURCODRCREY0RkNBQyIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDo5NTg1NjA3OTVEMjgxMUU2ODNGMURCODRCREY0RkNBQyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3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==';
    var browserPrefix = prefix();
    var style_text = '@' + browserPrefix + 'keyframes __music__playing_icon{0% {' + browserPrefix + 'transform: rotate(0)}to {' + browserPrefix + 'transform: rotate(1turn)}}';

    var STATE_playing = 1;
    var STATE_stopped = 0;
    var music = {};

    initmusic();
    addMusic();

    function initmusic() {
        music.res = musicResources;
        music.icon = document.createElement('img');
        music.state = STATE_stopped;
        music.audio = document.createElement('audio');
        music.style = document.createElement('link');
        music.style.setAttribute('rel', 'stylesheet');
        music.style.href = getObjectURL(new Blob([style_text]));

        music.audio.src = music.res;
        music.audio.loop = 'loop';

        music.icon.onclick = playOstop;
        music.icon.setAttribute('style', 'position:absolute;top: 0;margin: 4%;right: 0;width: 7.7%;z-index: 100;' + browserPrefix + 'animation:__music__playing_icon 3s linear infinite;' + browserPrefix + 'transform-origin: 50% 50%;'+browserPrefix + 'animation-play-state: paused');
        music.icon.src = ICON_stopped;
    }

    function addMusic() {
        document.body.appendChild(music.icon);
        document.head.appendChild(music.style);
    }

    function playOstop() {
        if (music.audio.paused) {
            // 暂停中, 启动播放
            music.audio.play();
            // 是否播放成功
            if (!music.audio.paused) {
            	music.state = STATE_playing;
	            music.icon.src = ICON_playing;
	            music.icon.style[browserPrefix + 'animation-play-state'] = 'running';
            }
        } else {
            // 播放中, 启动暂停
            music.audio.pause();
            if (music.audio.paused) {
            	music.state = STATE_stopped;
	            music.icon.src = ICON_stopped;
	            music.icon.style[browserPrefix + 'animation-play-state'] = 'paused';
            }
        }
    }

    function autoPlay(){
    	music.audio.play();
    }

    function prefix() {
        var div = document.createElement('div');
        var cssText = '-webkit-transition:all .1s; -moz-transition:all .1s; -o-transition:all .1s; -ms-transition:all .1s; transition:all .1s;';
        div.style.cssText = cssText;
        var style = div.style;
        if (style.webkitTransition) {
            return '-webkit-';
        }
        if (style.MozTransition) {
            return '-moz-';
        }
        if (style.oTransition) {
            return '-o-';
        }
        if (style.msTransition) {
            return '-ms-';
        }
        return '';
    }

    function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL !== undefined) { // basic
            url = window.createObjectURL(file);
        } else if (window.URL !== undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL !== undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }

	wx && wx.ready(function(){
		playOstop();
    });
}());
} catch (e) {console && console.log(e);}

实现微信自动播放音乐的关键代码wx && wx.ready(function(){playOstop();});,必须要放wx.ready里面执行audio.play()

微信音乐播放简易版

<audio id="mp3" src="bgm.mp3" autoplay="autoplay" loop></audio>
var audio = document.getElementById('mp3');
    if (window.WeixinJSBridge) {
        WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
            audio.play();
        }, false);
    } else {
        document.addEventListener("WeixinJSBridgeReady", function() {
            WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
                audio.play();
            });
        }, false);
    }

简易版的实现方式主要是自动监听微信准备就绪事件
不是微信页面,让音乐自动播放

posted @ 2016-10-17 17:52  Silababy  阅读(2134)  评论(0编辑  收藏  举报