Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract from ipod-library #22

Closed
LondonX opened this issue Jul 14, 2022 · 3 comments
Closed

extract from ipod-library #22

LondonX opened this issue Jul 14, 2022 · 3 comments

Comments

@LondonX
Copy link

LondonX commented Jul 14, 2022

I'm gettings songs with flutter plugin on_audio_query which returns song.uri likes ipod-library://item/item.flac?id=4724756324520404040, how can I extract wavefrom from this.

Thanks.

@ryanheise
Copy link
Owner

This plugin only extracts data from a file. It's up to you to get the data from other sources and save them to a file. Once you do so, this plugin is applicable after that point. I can't help you with how to read data from various sources, you will need to search for other plugins to do that or write a plugin that does that.

@LondonX
Copy link
Author

LondonX commented Jul 18, 2022

Thanks for reply. Yes, I write a plugin to do that, post here hope to help other guys.

Dart call

final exported = await AvAssetExportIos.instance.export(
      ipodLibraryUri: song.data,
      targetUri: iosExportFile.uri.toString(),
    );

SwiftAvAssetExportIosPlugin.swift

import Flutter
import UIKit
import AVFoundation
import AVKit
import AssetsLibrary

public class SwiftAvAssetExportIosPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "av_asset_export_ios", binaryMessenger: registrar.messenger())
        let instance = SwiftAvAssetExportIosPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        let args = call.arguments as? [String : Any?]
        switch(call.method) {
        case "export":
            let ipodLibraryUri = args!["ipodLibraryUri"] as! String
            let targetUri = args!["targetUri"] as! String
            let assetURL = URL.init(string: ipodLibraryUri)!
            let targetURL = URL.init(string: targetUri)!
            export(assetURL, targetURL) { error in
                print("[SwiftAvAssetExportIosPlugin] has error", error != nil)
                result(error == nil)
            }
            break
        default:
            result(FlutterMethodNotImplemented)
        }
    }
    
    func export(
        _ assetURL: URL,
        _ targetURL: URL,
        completionHandler: @escaping (_ error: Error?) -> ()
    ) {
        let asset = AVURLAsset(url: assetURL)
        guard let exporter = AVAssetExportSession(
            asset: asset,
            presetName: AVAssetExportPresetAppleM4A
        ) else {
            completionHandler(ExportError.unableToCreateExporter)
            return
        }
        try? FileManager.default.removeItem(at: targetURL)
        exporter.outputURL = targetURL
        exporter.outputFileType = AVFileType.m4a
        
        print("[SwiftAvAssetExportIosPlugin] exportAsynchronously assetURL: \(assetURL), outputURL: \(targetURL)");
        exporter.exportAsynchronously {
            switch(exporter.status) {
            case .completed:
                completionHandler(nil)
                break
            case .failed:
                print(exporter.error!)
                completionHandler(exporter.error)
                break
            default:
                break
            }
        }
    }
}

enum ExportError: Error {
    case unableToCreateExporter
    case unsupportedFormat
}

@biner88
Copy link

biner88 commented Mar 13, 2024

Supports ipod-library:// of IOS and content:// of Android. Post it here to help more people. Thank you for your code.@LondonX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants