【发布时间】:2018-04-20 16:38:04
【问题描述】:
在 iOS 11 更新后,我客户的所有播客都在 iTunes 应用程序中的 20 到 40 秒左右重新开始。 iOS 10 播放器不显示此问题。没有其他玩家可以证明这个问题,一旦剧集重新开始,它就会毫无问题地播放到最后。在任何给定的剧集中,重新启动都保持在同一点,但我怀疑它在较长的剧集中发生得更远,就好像它可能在持续时间的某个百分比处一样。
FF 和 RW 功能,包括拖动播放头都可以工作。
无论是否允许该应用下载剧集文件,iTunes 播客应用都会重新启动。
我们几周来一直在与 Apple 支持合作,但我想我也会在这里提出这个问题。
Apple 支持人员检查了 mp3 文件和 RSS 提要,没有发现任何问题。
<enclosure url="http://example.com/audio/episodes/2018_04_18_1458_some-show.mp3" length="63773956" type="audio/mpeg"/>
<pubDate>Wed, 18 Apr 2018 00:00:00 -0500</pubDate>
<itunes:duration>1:06:25</itunes:duration>
客户的生死取决于 Google Analytics 统计数据,因此我们使用 htaccess 中的 mod_rewrite 路由 MP3 的入站请求,就像这样
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteRule ^audio/episodes/([^/\.]+).mp3$ /audio/episodes/google_analytics_mp3.php?mp3=$1&redirected=1 [L,QSA]
并且测试表明,当路由被删除时,在以前未播放的剧集中不会出现问题。之前播放过的剧集继续出现此问题。
google_analytics_mp3.php:
<?php
include_once($_SERVER['DOCUMENT_ROOT']."/classDBI/classDBI.php");
include_once($_SERVER['DOCUMENT_ROOT']."/classDBI/google_analytics_api.php");
$e = new Episode;
if (is_numeric($_REQUEST['mp3'])) {
$id = intval($_REQUEST['mp3']);
list($ep) = $e->retrieve("id = $id");
if ($ep) {
$outcome = ga_send_pageview(basename($ep->audio_link), 'Site streaming: ' . $ep->google_title());
}
} else if ($_REQUEST['redirected'] == 1) {
$fileName = $_GET['mp3'] . '.mp3';
$fileLocation = $_SERVER['DOCUMENT_ROOT'].'/audio/episodes/'.$fileName;
$etag = md5_file($fileName);
$fileRedirect = '/audio/episodes/'.$fileName;
if (file_exists($fileLocation)) {
list($ep) = $e->retrieve("audio_link = 'audio/episodes/$fileName'");
$pageName = 'Direct access';
if ($ep) {
$pageName .= ': ' . $ep->google_title();
}
$size = filesize($fileLocation);
$time = date('r', filemtime($fileLocation));
$fm = @fopen($fileLocation, 'rb');
if (!$fm) {
header ("HTTP/1.1 505 Internal server error");
return;
}
$begin = 0;
$end = $size - 1;
if (isset($_SERVER['HTTP_RANGE'])) {
if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
$begin = intval($matches[1]);
if (!empty($matches[2])) {
$end = intval($matches[2]);
}
}
}
if (isset($_SERVER['HTTP_RANGE'])) {
header('HTTP/1.1 206 Partial Content');
if ($begin == 0) {
$outcome = ga_send_pageview($fileName, 'Streaming: ' . $pageName);
}
} else{
header('HTTP/1.1 200 OK');
$outcome = ga_send_pageview($fileName, $pageName);
}
header("Content-Transfer-Encoding: binary");
header('Content-Type: audio/mpeg');
header("Etag: $etag");
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
header('Content-Length:' . (($end - $begin) + 1));
header('Content-Disposition: inline; filename="'.$fileName.'"');
// 2018.01.14 changes
if (isset($_SERVER['HTTP_RANGE'])) {
header("Content-Range: bytes $begin-$end/$size");
}
// END 2018.01.14 changes
readfile($fileLocation);
header("Last-Modified: $time");
$cur = $begin;
fseek($fm, $begin, 0);
while(!feof($fm) && $cur <= $end && (connection_status() == 0)) {
print fread($fm, min(1024 * 16, ($end - $cur) + 1));
$cur += 1024 * 16;
}
exit();
}
}
我很确定我处理标头的方式导致了重新启动问题,但我找不到错误。
以下是来自 Chrome 开发面板的关于 mp3 请求的标头。 IP 地址更改为文件名:
General
Request URL: http://example.com/audio/episodes/2018_04_18_1458_some-show.mp3
Request Method: GET
Status Code: 200 OK
Remote Address: 205.196.xxx.xxx:80
Referrer Policy: no-referrer-when-downgrade
Response Headers
Accept-Ranges: bytes
Cache-Control: public, must-revalidate, max-age=0
Connection: Keep-Alive
Content-Disposition: inline; filename="2018_04_18_1458_some-show.mp3"
Content-Length: 63773956
Content-Transfer-Encoding: binary
Content-Type: audio/mpeg
Date: Fri, 20 Apr 2018 16:27:57 GMT
Etag: 7fe7b0375cd99ec4d928b1a8885bee81
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=2, max=100
Pragma: no-cache
Server: Apache
Request Headers
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Cookie: PHPSESSID=TYDy9IZXJwTCBlmXVmkHn0; _ga=GA1.2.904913110.1515511103; _gid=GA1.2.666994959.1523989189; __unam=739f578-160db803df3-1cf2ee83-70
Host: www.example.com
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36
如果有帮助的话,这里是 google_analytics_api.php:
<?php
define('GOOGLEACCOUNT', 'UA-xxxxxxxx-1');
define('GOOGLEDOMAIN', $_SERVER['HTTP_HOST']);
function gaParseCookie() {
if (isset($_COOKIE['_ga'])) {
list($version, $domainDepth, $cid1, $cid2) = explode('.', $_COOKIE["_ga"], 4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1 . '.' . $cid2);
$cid = $contents['cid'];
} else {
$cid = gaGenerateUUID();
}
return $cid;
}
function gaGenerateUUID() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
function gaSendData($data) {
$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET",
CURLOPT_POST =>false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
$ch = curl_init( $getString );
curl_setopt_array( $ch, $options );
$result = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
if ($err) {
mail('email@example.com', 'Analytics MP3 Error' . __FILE__, "$err\n\n$errmsg\n\n$header");
}
curl_close( $ch );
return $result;
}
function ga_send_pageview($file, $title) {
$data = array(
'v' => 1,
'tid' => GOOGLEACCOUNT,
'cid' => gaParseCookie(),
't' => 'pageview',
'dh' => GOOGLEDOMAIN,
'dp' => $file,
'dt' => $title
);
if(strlen($_SERVER['HTTP_REFERER'])) {
$data['utmr'] = $_SERVER['HTTP_REFERER'];
}
if (strlen($_SERVER['HTTP_USER_AGENT'])) {
$data['ua'] = $_SERVER['HTTP_USER_AGENT'];
}
gaSendData($data);
}
function ga_send_event($category=null, $action=null, $label=null) {
$data = array(
'v' => 1,
'tid' => GOOGLEACCOUNT,
'cid' => gaParseCookie(),
't' => 'event',
'ec' => $category, //Category (Required)
'ea' => $action, //Action (Required)
'el' => $label
);
gaSendData($data);
}
任何建议都非常感谢!
【问题讨论】:
-
你不能一次删除php中的标题行来尝试确定哪一个可能是造成问题的原因吗?我将特别关注 Content-Range 标头(如果从头开始流式传输,则不应设置)和
$begin变量。这似乎是一个奇怪的问题,但您应该能够逐步调试以确定原因。 -
这些是可变比特率 mp3 吗?还是恒定比特率? iOS 11 对处理 VBR 搜索进行了一些更改,这可能与您的问题有关:twitter.com/marcoarment/status/963604820778192897