Improved audio manager
This commit is contained in:
parent
4c58d00f74
commit
0a5a2e25f0
@ -1981,33 +1981,6 @@ angular.module('myApp.directives', ['myApp.filters'])
|
|||||||
templateUrl: templateUrl('audio_player')
|
templateUrl: templateUrl('audio_player')
|
||||||
};
|
};
|
||||||
|
|
||||||
function downloadAudio (audio) {
|
|
||||||
var inputFileLocation = {
|
|
||||||
_: audio._ == 'document' ? 'inputDocumentFileLocation' : 'inputAudioFileLocation',
|
|
||||||
id: audio.id,
|
|
||||||
access_hash: audio.access_hash
|
|
||||||
};
|
|
||||||
|
|
||||||
audio.progress = {enabled: true, percent: 1, total: audio.size};
|
|
||||||
|
|
||||||
var downloadPromise = MtpApiFileManager.downloadFile(audio.dc_id, inputFileLocation, audio.size, {mime: 'audio/ogg'});
|
|
||||||
|
|
||||||
audio.progress.cancel = downloadPromise.cancel;
|
|
||||||
|
|
||||||
return downloadPromise.then(function (url) {
|
|
||||||
delete audio.progress;
|
|
||||||
audio.rawUrl = url;
|
|
||||||
audio.url = $sce.trustAsResourceUrl(url);
|
|
||||||
}, function (e) {
|
|
||||||
console.log('audio download failed', e);
|
|
||||||
audio.progress.enabled = false;
|
|
||||||
}, function (progress) {
|
|
||||||
console.log('audio dl progress', progress);
|
|
||||||
audio.progress.done = progress.done;
|
|
||||||
audio.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkPlayer (newPlayer) {
|
function checkPlayer (newPlayer) {
|
||||||
if (newPlayer === currentPlayer) {
|
if (newPlayer === currentPlayer) {
|
||||||
return false;
|
return false;
|
||||||
@ -2019,14 +1992,20 @@ angular.module('myApp.directives', ['myApp.filters'])
|
|||||||
}
|
}
|
||||||
|
|
||||||
function link($scope, element, attrs) {
|
function link($scope, element, attrs) {
|
||||||
|
if ($scope.audio._ == 'audio') {
|
||||||
|
AppAudioManager.updateAudioDownloaded($scope.audio.id);
|
||||||
|
} else {
|
||||||
|
AppDocsManager.updateDocDownloaded($scope.audio.id);
|
||||||
|
}
|
||||||
|
|
||||||
$scope.mediaPlayer = {};
|
$scope.mediaPlayer = {};
|
||||||
|
|
||||||
$scope.download = function () {
|
$scope.download = function () {
|
||||||
($scope.audio.rawUrl ? $q.when() : downloadAudio($scope.audio)).then(
|
if ($scope.audio._ == 'audio') {
|
||||||
function () {
|
AppAudioManager.saveAudioFile($scope.audio.id);
|
||||||
FileManager.download($scope.audio.rawUrl, $scope.audio.mime_type || 'audio/ogg', $scope.audio.file_name || 'audio.ogg');
|
} else {
|
||||||
}
|
AppDocsManager.saveDocFile($scope.audio.id);
|
||||||
);
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.togglePlay = function () {
|
$scope.togglePlay = function () {
|
||||||
@ -2038,7 +2017,14 @@ angular.module('myApp.directives', ['myApp.filters'])
|
|||||||
$scope.audio.progress.cancel();
|
$scope.audio.progress.cancel();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
downloadAudio($scope.audio).then(function () {
|
var downloadPromise;
|
||||||
|
if ($scope.audio._ == 'audio') {
|
||||||
|
downloadPromise = AppAudioManager.downloadDoc($scope.audio.id);
|
||||||
|
} else {
|
||||||
|
downloadPromise = AppDocsManager.downloadDoc($scope.audio.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
downloadPromise.then(function () {
|
||||||
onContentLoaded(function () {
|
onContentLoaded(function () {
|
||||||
checkPlayer($scope.mediaPlayer.player);
|
checkPlayer($scope.mediaPlayer.player);
|
||||||
$scope.mediaPlayer.player.play();
|
$scope.mediaPlayer.player.play();
|
||||||
|
@ -2706,7 +2706,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
.service('AppAudioManager', function ($rootScope, $modal, $window, $timeout, $sce, MtpApiFileManager) {
|
.service('AppAudioManager', function ($rootScope, $modal, $window, $timeout, $sce, MtpApiFileManager, FileManager) {
|
||||||
var audios = {};
|
var audios = {};
|
||||||
var audiosForHistory = {};
|
var audiosForHistory = {};
|
||||||
|
|
||||||
@ -2724,51 +2724,84 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
|
|||||||
return audiosForHistory[audioID] = audio;
|
return audiosForHistory[audioID] = audio;
|
||||||
}
|
}
|
||||||
|
|
||||||
function openAudio (audioID, accessHash) {
|
function updateAudioDownloaded (audioID) {
|
||||||
var audio = audios[audioID],
|
var audio = audios[audioID],
|
||||||
historyAudio = audiosForHistory[audioID] || audio || {},
|
historyAudio = audiosForHistory[audioID] || audio || {},
|
||||||
inputFileLocation = {
|
inputFileLocation = {
|
||||||
_: 'inputAudioFileLocation',
|
_: 'inputAudioFileLocation',
|
||||||
id: audioID,
|
id: audioID,
|
||||||
access_hash: accessHash || audio.access_hash
|
access_hash: audio.access_hash
|
||||||
|
};
|
||||||
|
|
||||||
|
if (historyAudio.downloaded === undefined) {
|
||||||
|
MtpApiFileManager.getDownloadedFile(inputFileLocation, audio.size).then(function () {
|
||||||
|
historyAudio.downloaded = true;
|
||||||
|
}, function () {
|
||||||
|
historyAudio.downloaded = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function downloadAudio (audioID, toFileEntry) {
|
||||||
|
var audio = audios[audioID],
|
||||||
|
historyAudio = audiosForHistory[audioID] || audio || {},
|
||||||
|
inputFileLocation = {
|
||||||
|
_: 'inputAudioFileLocation',
|
||||||
|
id: audioID,
|
||||||
|
access_hash: audio.access_hash
|
||||||
};
|
};
|
||||||
|
|
||||||
historyAudio.progress = {enabled: true, percent: 1, total: audio.size};
|
historyAudio.progress = {enabled: true, percent: 1, total: audio.size};
|
||||||
|
|
||||||
function updateDownloadProgress (progress) {
|
var downloadPromise = MtpApiFileManager.downloadFile(audio.dc_id, inputFileLocation, audio.size, {
|
||||||
|
mime: audio.mime_type || 'audio/ogg',
|
||||||
|
toFileEntry: toFileEntry
|
||||||
|
});
|
||||||
|
|
||||||
|
downloadPromise.then(function (url) {
|
||||||
|
delete historyAudio.progress;
|
||||||
|
historyAudio.url = url;
|
||||||
|
historyAudio.downloaded = true;
|
||||||
|
console.log('audio save done');
|
||||||
|
}, function (e) {
|
||||||
|
console.log('audio download failed', e);
|
||||||
|
historyAudio.progress.enabled = false;
|
||||||
|
}, function (progress) {
|
||||||
console.log('dl progress', progress);
|
console.log('dl progress', progress);
|
||||||
historyAudio.progress.done = progress.done;
|
historyAudio.progress.done = progress.done;
|
||||||
historyAudio.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
|
historyAudio.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
|
||||||
$rootScope.$broadcast('history_update');
|
$rootScope.$broadcast('history_update');
|
||||||
}
|
});
|
||||||
|
|
||||||
var downloadPromise = MtpApiFileManager.downloadFile(audio.dc_id, inputFileLocation, audio.size, {mime: 'audio/ogg'});
|
|
||||||
|
|
||||||
downloadPromise.then(function (url) {
|
|
||||||
delete historyAudio.progress;
|
|
||||||
historyAudio.url = $sce.trustAsResourceUrl(url);
|
|
||||||
historyAudio.autoplay = true;
|
|
||||||
$timeout(function () {
|
|
||||||
console.log('disable autoplay');
|
|
||||||
delete historyAudio.autoplay;
|
|
||||||
$rootScope.$broadcast('history_update');
|
|
||||||
}, 1000);
|
|
||||||
}, function (e) {
|
|
||||||
console.log('audio download failed', e);
|
|
||||||
historyAudio.progress.enabled = false;
|
|
||||||
}, updateDownloadProgress);
|
|
||||||
|
|
||||||
historyAudio.progress.cancel = downloadPromise.cancel;
|
historyAudio.progress.cancel = downloadPromise.cancel;
|
||||||
|
|
||||||
return downloadPromise;
|
return downloadPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rootScope.openAudio = openAudio;
|
function saveAudioFile (audioID) {
|
||||||
|
var audio = audios[audioID],
|
||||||
|
historyAudio = audiosForHistory[audioID] || audio || {};
|
||||||
|
|
||||||
|
FileManager.chooseSave(audio.file_name, 'ogg', audio.mime_type || 'audio/ogg').then(function (writableFileEntry) {
|
||||||
|
if (!writableFileEntry) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
downloadAudio(audioID, writableFileEntry).then(function () {
|
||||||
|
console.log('file save done');
|
||||||
|
});
|
||||||
|
}, function () {
|
||||||
|
downloadAudio(audioID).then(function (url) {
|
||||||
|
FileManager.download(url, audio.mime_type, audio.file_name);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
saveAudio: saveAudio,
|
saveAudio: saveAudio,
|
||||||
wrapForHistory: wrapForHistory,
|
wrapForHistory: wrapForHistory,
|
||||||
openAudio: openAudio
|
updateAudioDownloaded: updateAudioDownloaded,
|
||||||
|
downloadAudio: downloadAudio,
|
||||||
|
saveAudioFile: saveAudioFile
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user