Skip to content

discuz论坛中发帖之音乐自动播放方法

2010 三月 3
by 荒野无灯

今天寝室的小则童鞋问我他在论坛发帖如何让音乐自动播放,于是有了此文。
我看了下,他所在的论坛用的是国内比较流行的discuz论坛系统,虽然以前我也办过论坛,不过那时候我用的是6.*版和7.0版。现在的7.1和7.2版的discuz添加媒体或者音乐时没有自动播放选项的。不过其实它是支持自动播放的。
实现方法我这里有两种:
一、用discuz 代码media实现:
如:

1
[media=mp3,250,30,1]http://www.changheng.net/iblog/images/jpg/dielian.mp3[/media]

那四个参数分别是文件类型(mp3、wma 等) 、长、宽、是否自动播放(自动播放填1就可以了,其实填什么无所谓,不自动播放则不填)
中间是mp3地址。

在include/discuzcode.php文件中的discuzcode函数中可以看到如下代码:

1
2
3
4
5
6
        if(strpos($msglower, '[/media]') !== FALSE) {
            $message = preg_replace("/\[media=([\w,]+)\]\s*([^\[\<\r\n]+?)\s*\[\/media\]/ies", $allowmediacode ? "parsemedia('\\1', '\\2')" : "bbcodeurl('\\2', '<a href=\"%s\" target=\"_blank\">%s</a>')", $message);
        }
        if($allowmediacode && strpos($msglower, '[/audio]') !== FALSE) {
            $message = preg_replace("/\[audio\]\s*([^\[\<\r\n]+?)\s*\[\/audio\]/ies", "parseaudio('\\1')", $message);
        }

可见对于media标签是用parsemedia解析的,对于audio标签是用parseaudio解析的,但是media标签也可以处理音乐,从代码可以看出parsemedia函数对于音乐地址是通过调用parseaudio来解析的。从代码还可以看出audio标签是不支持自动播放的。因此要实现自动播放就得用media标签了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function parseaudio($url, $width = 400, $autostart = 0) {
    $ext = strtolower(substr(strrchr($url, '.'), 1, 5));
    switch($ext) {
        case 'mp3':
        case 'wma':
        case 'mid':
        case 'wav':
            return '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'.$width.'" height="64"><param name="invokeURLs" value="0"><param name="autostart" value="'.$autostart.'" /><param name="url" value="'.$url.'" /><embed src="'.$url.'" autostart="'.$autostart.'" type="application/x-mplayer2" width="'.$width.'" height="64"></embed></object>';
        case 'ra':
        case 'rm':
        case 'ram':
            $mediaid = 'media_'.random(3);
            return '<object classid="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="'.$width.'" height="32"><param name="autostart" value="'.$autostart.'" /><param name="src" value="'.$url.'" /><param name="controls" value="controlpanel" /><param name="console" value="'.$mediaid.'_" /><embed src="'.$url.'" type="audio/x-pn-realaudio-plugin" controls="ControlPanel" console="'.$mediaid.'_" width="'.$width.'" height="32"></embed></object>';
    }
}

function parsemedia($params, $url) {
    $params = explode(',', $params);
    $width = intval($params[1]) > 800 ? 800 : intval($params[1]);
    $height = intval($params[2]) > 600 ? 600 : intval($params[2]);
    $autostart = !empty($params[3]) ? 1 : 0;
    if($flv = parseflv($url, $width, $height)) {
        return $flv;
    }
    if(in_array(count($params), array(3, 4))) {
        $type = $params[0];
        $url = str_replace(array('<', '>'), '', str_replace('\\"', '\"', $url));
        switch($type) {
            case 'mp3':
            case 'wma':
            case 'ra':
            case 'ram':
            case 'wav':
            case 'mid':
                return parseaudio($url, $width, $autostart);
            case 'rm':
            case 'rmvb':
            case 'rtsp':
                $mediaid = 'media_'.random(3);
                return '<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="'.$width.'" height="'.$height.'"><param name="autostart" value="'.$autostart.'" /><param name="src" value="'.$url.'" /><param name="controls" value="imagewindow" /><param name="console" value="'.$mediaid.'_" /><embed src="'.$url.'" type="audio/x-pn-realaudio-plugin" controls="imagewindow" console="'.$mediaid.'_" width="'.$width.'" height="'.$height.'"></embed></object><br /><object classid="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="'.$width.'" height="32"><param name="src" value="'.$url.'" /><param name="controls" value="controlpanel" /><param name="console" value="'.$mediaid.'_" /><embed src="'.$url.'" type="audio/x-pn-realaudio-plugin" controls="controlpanel" console="'.$mediaid.'_" width="'.$width.'" height="32"'.($autostart ? ' autostart="true"' : '').'></embed></object>';
            case 'flv':
                return '<script type="text/javascript" reload="1">document.write(AC_FL_RunContent(\'width\', \''.$width.'\', \'height\', \''.$height.'\', \'allowNetworking\', \'internal\', \'allowScriptAccess\', \'never\', \'src\', \'images/common/flvplayer.swf\', \'flashvars\', \'file='.rawurlencode($url).'\', \'quality\', \'high\', \'wmode\', \'transparent\', \'allowfullscreen\', \'true\'));</script>';
            case 'swf':
                return '<script type="text/javascript" reload="1">document.write(AC_FL_RunContent(\'width\', \''.$width.'\', \'height\', \''.$height.'\', \'allowNetworking\', \'internal\', \'allowScriptAccess\', \'never\', \'src\', \''.$url.'\', \'quality\', \'high\', \'bgcolor\', \'#ffffff\', \'wmode\', \'transparent\', \'allowfullscreen\', \'true\'));</script>';
            case 'asf':
            case 'asx':
            case 'wmv':
            case 'mms':
            case 'avi':
            case 'mpg':
            case 'mpeg':
                return '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'.$width.'" height="'.$height.'"><param name="invokeURLs" value="0"><param name="autostart" value="'.$autostart.'" /><param name="url" value="'.$url.'" /><embed src="'.$url.'" autostart="'.$autostart.'" type="application/x-mplayer2" width="'.$width.'" height="'.$height.'"></embed></object>';
            case 'mov':
                return '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="'.$width.'" height="'.$height.'"><param name="autostart" value="'.($autostart ? '' : 'false').'" /><param name="src" value="'.$url.'" /><embed src="'.$url.'" autostart="'.($autostart ? 'true' : 'false').'" type="video/quicktime" controller="true" width="'.$width.'" height="'.$height.'"></embed></object>';
            default:
                return '<a href="'.$url.'" target="_blank">'.$url.'</a>';
        }
    }
    return;
}

从代码可以看出media标签只要存在第四个参数就可以使自动播放的条件为真,再调用 parseaudio解析时就会设置成自动播放。

二、第二种方法是用flash标签实现

1
2
3
4
5
6
7
8
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=25&songVolume=100&backColor=ffffff&frontColor=ffffff&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]   白色播放器
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=150&songVolume=100&backColor=ff80c0&frontColor=ffffff&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]    粉色播放器
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=150&songVolume=100&backColor=000000&frontColor=ffffff&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]   黑色播放器
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=150&songVolume=100&backColor=8080ff&frontColor=ffffff&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]  紫色播放器
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=150&songVolume=100&backColor=ff0080&frontColor=ffffff&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]  深粉色播放器
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=150&songVolume=100&backColor=ff8080&frontColor=ffffff&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]  橙色播放器
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=150&songVolume=100&backColor=408080&frontColor=ffffff&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]  绿色播放器
[flash]http://www.51119.com/play/swf/2.swf?file=歌曲地址&width=25&songVolume=100&backColor=0080c0&frontColor=7bb5e3&autoStart=true&repeatPlay=true&showDownload=false&2.swf[/flash]  深蓝色播放器

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:,欢迎点击这里捐赠以支持荒野无灯转播到腾讯微博 转播到腾讯微博

作者:荒野无灯
出处:Hacklog【Hacklog】

声明: 本站遵循 署名-非商业性使用-相同方式共享 3.0 共享协议. 转载请注明转自Hacklog【荒野无灯weblog】

本文链接: http://ihacklog.com/?p=3495

3 Responses Post a comment
  1. 七月 23, 2010

    不错!看来你很了解DZ哦

  2. 四月 17, 2010

    好东西,有用 :grin:

  3. 三月 29, 2010

    测试有效

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <q cite=""> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS