指定src =""在< audio>中是没有.mp3扩展名的mp3文件? [英] Specify that src="" in <audio> is a mp3 file without .mp3 extension?

查看:332
本文介绍了指定src =""在< audio>中是没有.mp3扩展名的mp3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来指定类似

I need a way to specify that something like

<audio src="http://somesite.com/track/377374"></audio>

有一个mp3文件作为来源。我无法在url的末尾使用.mp3扩展名,所以我需要其他方法来实现这一点。我正在使用自定义播放器jQuery插件,如果它无法确定音频标签中指定的文件格式,则会回退到旧的浏览器版本。我有办法实现这个目标吗?

Has a mp3 file as a source. I am unable to use .mp3 extension at the end of the url, so I need other way to achieve this. I'm using custom player jQuery plugin which falls back to old browser version if it can't determine what file format is specified in audio tag. I's there a way to achieve this?

推荐答案

有一个检查文件扩展名并尝试匹配的函数

There is a function that check the file extension and tries to match

    canPlayType   = function( file )
    {
        var audioElement = document.createElement( 'audio' );
        return !!( audioElement.canPlayType && 
                   audioElement.canPlayType( 'audio/' + file.split( '.' ).pop().toLowerCase() + ';' ).replace( /no/, '' ) );
    };

如果我们说这个函数是返回true或者更改为:

if let's say this function was to return true or be changed to something like:

    canPlayType   = function( type )// type = mp3
    {
        var audioElement = document.createElement( 'audio' );
        return !!( audioElement.canPlayType && 
                   audioElement.canPlayType( 'audio/' + type.toLowerCase() + ';' ).replace( /no/, '' ) );
    };

然后插件行75的更新代码将假设文件是​​mp3并且在使用时这样做src ...源元素仍然可以按传统方式使用。

Then the updated code of the plugin line 75 would assume the files are mp3 and do this like when using the src... the source element could still be used the traditional way.

else if( canPlayType( 'mp3' ) ) isSupport = true; // here change this

对于要检测的源类型(如@ruakh建议的那样代码会需要编辑(再次):

For the source type to be detected (like suggested by @ruakh the code would need to be edited (again):

if( typeof audioFile === 'undefined' )
{
    $this.find( 'source' ).each( function()
    {
        audioFile = $( this ).attr( 'src' );
        var sourceType = $( this ).attr( 'type' );
        if( typeof audioFile !== 'undefined' && 
                        typeof sourceType !== 'undefined' && 
                        canPlayType( sourceType.split( '/' ).pop().toLowerCase()  ) )
        {
            isSupport = true;
            return false;
        }
    });
}

这篇关于指定src =&quot;&quot;在&lt; audio&gt;中是没有.mp3扩展名的mp3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆