Exemple #1
0
        /// <summary>
        /// Exports the audio of a media item to storage.
        /// </summary>
        /// <returns><c>true</c> if audio started exporting, <c>false</c> if it is exporting another audio or the media item has DRM.</returns>
        /// <param name="mediaItem">Media item.</param>
        /// <param name="outputFolder">Absolute output folder or specify null to use Documents folder.</param>
        /// <param name="outputFile">Output file name or specify null to use <c>[artist] - [title].[extension]</c>.</param>
        /// <param name="overwrite">Whether to overwrite the output file.</param>
        public static bool ExportAudio(MPMediaItem mediaItem, string outputFolder = null, string outputFile = null, bool overwrite = false)
        {
            if (_exporter != null)
            {
                return(false);
            }
            if (mediaItem == null)
            {
                return(false);
            }

            if (outputFolder == null)
            {
                outputFolder = Application.persistentDataPath;
            }

            NSURL assetURL = mediaItem.Value(MPMediaItem.PropertyAssetURL) as NSURL;

            if (assetURL == null)
            {
                return(false);
            }

            if (outputFile == null)
            {
                string artist    = mediaItem.Value(MPMediaItem.PropertyArtist) as string;
                string title     = mediaItem.Value(MPMediaItem.PropertyTitle) as string;
                string extension = Path.GetExtension(assetURL.AbsoluteString().Split('?')[0]);
                outputFile = artist + " - " + title + extension;
            }

            _outputPath = outputFolder + "/" + outputFile;
            if (!overwrite && File.Exists(_outputPath))
            {
                return(false);
            }

            _avAsset  = AVURLAsset.Asset(assetURL);
            _exporter = new AVAssetExportSession(_avAsset, AVAssetExportSession.AVAssetExportPresetPassthrough);
            _exporter.outputFileType = "com.apple.quicktime-movie";
            _exporter.shouldOptimizeForNetworkUse = true;

            string tmpExt = UTType.CopyPreferredTag(_exporter.outputFileType, UTType.kUTTagClassFilenameExtension);

            _tmpPath            = Application.persistentDataPath + "/" + UUID.Generate() + "." + tmpExt;
            _exporter.outputURL = new NSURL(_tmpPath, false);

            _exporter.ExportAsynchronously(_OnExportDone);

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Exports the audio of a media item to storage.
        /// </summary>
        /// <returns><c>true</c> if audio started exporting, <c>false</c> if it is exporting another audio or the media item has DRM.</returns>
        /// <param name="mediaItem">Media item.</param>
        /// <param name="outputFolder">Absolute output folder or specify null to use Documents folder.</param>
        /// <param name="outputFile">Output file name or specify null to use <c>[artist] - [title].[extension]</c>.</param>
        /// <param name="overwrite">Whether to overwrite the output file.</param>
        public static bool ExportAudio(MPMediaItem mediaItem, string outputFolder = null, string outputFile = null, bool overwrite = false)
        {
            if (_exporter != null)
                return false;
            if (mediaItem == null)
                return false;

            if (outputFolder == null)
                outputFolder = Application.persistentDataPath;

            NSURL assetURL = mediaItem.Value(MPMediaItem.PropertyAssetURL) as NSURL;
            if (assetURL == null)
                return false;

            if (outputFile == null) {
                string artist = mediaItem.Value(MPMediaItem.PropertyArtist) as string;
                string title = mediaItem.Value(MPMediaItem.PropertyTitle) as string;
                string extension = Path.GetExtension(assetURL.AbsoluteString().Split('?')[0]);
                outputFile = artist + " - " + title + extension;
            }

            _outputPath = outputFolder + "/" + outputFile;
            if (!overwrite && File.Exists(_outputPath))
                return false;

            _avAsset = AVURLAsset.Asset(assetURL);
            _exporter = new AVAssetExportSession(_avAsset, AVAssetExportSession.AVAssetExportPresetPassthrough);
            _exporter.outputFileType = "com.apple.quicktime-movie";
            _exporter.shouldOptimizeForNetworkUse = true;

            string tmpExt = UTType.CopyPreferredTag(_exporter.outputFileType, UTType.kUTTagClassFilenameExtension);
            _tmpPath = Application.persistentDataPath + "/" + UUID.Generate() + "." + tmpExt;
            _exporter.outputURL = new NSURL(_tmpPath, false);

            _exporter.ExportAsynchronously(_OnExportDone);

            return true;
        }
    void ExportFirstSong()
    {
        MPMediaQuery query = MPMediaQuery.SongsQuery();

        if (query.items.Length == 0)
        {
            Log("No songs in Music Library.");
            return;
        }

        // get first song
        MPMediaItem mediaItem = query.items[0] as MPMediaItem;

        Log("Exporting song: " + mediaItem.Value(MPMediaItem.PropertyTitle));

        // use null for outputFolder and outputFile to name it [artist] - [title]
        if (!MediaExporter.ExportAudio(mediaItem, null, null, true))
        {
            Log("Export error or song has DRM.");
        }
    }