Browse Source

Improved audio manager

master
Igor Zhukov 10 years ago
parent
commit
0a5a2e25f0
  1. 52
      app/js/directives.js
  2. 75
      app/js/services.js

52
app/js/directives.js

@ -1981,33 +1981,6 @@ angular.module('myApp.directives', ['myApp.filters']) @@ -1981,33 +1981,6 @@ angular.module('myApp.directives', ['myApp.filters'])
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) {
if (newPlayer === currentPlayer) {
return false;
@ -2019,14 +1992,20 @@ angular.module('myApp.directives', ['myApp.filters']) @@ -2019,14 +1992,20 @@ angular.module('myApp.directives', ['myApp.filters'])
}
function link($scope, element, attrs) {
if ($scope.audio._ == 'audio') {
AppAudioManager.updateAudioDownloaded($scope.audio.id);
} else {
AppDocsManager.updateDocDownloaded($scope.audio.id);
}
$scope.mediaPlayer = {};
$scope.download = function () {
($scope.audio.rawUrl ? $q.when() : downloadAudio($scope.audio)).then(
function () {
FileManager.download($scope.audio.rawUrl, $scope.audio.mime_type || 'audio/ogg', $scope.audio.file_name || 'audio.ogg');
}
);
if ($scope.audio._ == 'audio') {
AppAudioManager.saveAudioFile($scope.audio.id);
} else {
AppDocsManager.saveDocFile($scope.audio.id);
}
};
$scope.togglePlay = function () {
@ -2038,7 +2017,14 @@ angular.module('myApp.directives', ['myApp.filters']) @@ -2038,7 +2017,14 @@ angular.module('myApp.directives', ['myApp.filters'])
$scope.audio.progress.cancel();
}
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 () {
checkPlayer($scope.mediaPlayer.player);
$scope.mediaPlayer.player.play();

75
app/js/services.js

@ -2706,7 +2706,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -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 audiosForHistory = {};
@ -2724,51 +2724,84 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2724,51 +2724,84 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
return audiosForHistory[audioID] = audio;
}
function openAudio (audioID, accessHash) {
function updateAudioDownloaded (audioID) {
var audio = audios[audioID],
historyAudio = audiosForHistory[audioID] || audio || {},
inputFileLocation = {
_: 'inputAudioFileLocation',
id: audioID,
access_hash: accessHash || audio.access_hash
access_hash: audio.access_hash
};
historyAudio.progress = {enabled: true, percent: 1, total: audio.size};
function updateDownloadProgress (progress) {
console.log('dl progress', progress);
historyAudio.progress.done = progress.done;
historyAudio.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
$rootScope.$broadcast('history_update');
if (historyAudio.downloaded === undefined) {
MtpApiFileManager.getDownloadedFile(inputFileLocation, audio.size).then(function () {
historyAudio.downloaded = true;
}, function () {
historyAudio.downloaded = false;
});
}
}
var downloadPromise = MtpApiFileManager.downloadFile(audio.dc_id, inputFileLocation, audio.size, {mime: 'audio/ogg'});
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};
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 = $sce.trustAsResourceUrl(url);
historyAudio.autoplay = true;
$timeout(function () {
console.log('disable autoplay');
delete historyAudio.autoplay;
$rootScope.$broadcast('history_update');
}, 1000);
historyAudio.url = url;
historyAudio.downloaded = true;
console.log('audio save done');
}, function (e) {
console.log('audio download failed', e);
historyAudio.progress.enabled = false;
}, updateDownloadProgress);
}, function (progress) {
console.log('dl progress', progress);
historyAudio.progress.done = progress.done;
historyAudio.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
$rootScope.$broadcast('history_update');
});
historyAudio.progress.cancel = downloadPromise.cancel;
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 {
saveAudio: saveAudio,
wrapForHistory: wrapForHistory,
openAudio: openAudio
updateAudioDownloaded: updateAudioDownloaded,
downloadAudio: downloadAudio,
saveAudioFile: saveAudioFile
}
})

Loading…
Cancel
Save