{"id":13908382,"url":"https://github.com/eroscai/SZAVPlayer","last_synced_at":"2025-07-18T07:31:02.522Z","repository":{"id":40658237,"uuid":"229447154","full_name":"eroscai/SZAVPlayer","owner":"eroscai","description":"SZAVPlayer is a lightweight audio/video player library, based on AVPlayer and AVAssetResourceLoaderDelegate, pure-Swift. Support cache and video image output.","archived":false,"fork":false,"pushed_at":"2024-06-14T01:28:26.000Z","size":372,"stargazers_count":250,"open_issues_count":7,"forks_count":32,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-11T07:25:00.623Z","etag":null,"topics":["asset","audio","avassetresourceloaderdelegate","avplayer","cache","carthage","player","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eroscai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-21T15:33:00.000Z","updated_at":"2025-07-11T06:49:54.000Z","dependencies_parsed_at":"2024-06-20T18:58:53.249Z","dependency_job_id":"e6b22e9e-0c72-4f21-98f9-e8fb62359073","html_url":"https://github.com/eroscai/SZAVPlayer","commit_stats":{"total_commits":64,"total_committers":4,"mean_commits":16.0,"dds":0.140625,"last_synced_commit":"a269ad4286ea676459f0a3da7b113d7a30c78f73"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/eroscai/SZAVPlayer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZAVPlayer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZAVPlayer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZAVPlayer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZAVPlayer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eroscai","download_url":"https://codeload.github.com/eroscai/SZAVPlayer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZAVPlayer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265720467,"owners_count":23817242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asset","audio","avassetresourceloaderdelegate","avplayer","cache","carthage","player","swift"],"created_at":"2024-08-06T23:02:41.358Z","updated_at":"2025-07-18T07:31:01.783Z","avatar_url":"https://github.com/eroscai.png","language":"Swift","funding_links":[],"categories":["HarmonyOS"],"sub_categories":["Windows Manager"],"readme":"# SZAVPlayer\n\n[![Version](https://img.shields.io/cocoapods/v/SZAVPlayer.svg?style=flat)](https://cocoapods.org/pods/SZAVPlayer)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![SPM supported](https://img.shields.io/badge/SPM-supported-DE5C43.svg?style=flat)](https://swift.org/package-manager/)\n[![License](https://img.shields.io/cocoapods/l/SZAVPlayer.svg?style=flat)](https://cocoapods.org/pods/SZAVPlayer)\n[![Platform](https://img.shields.io/cocoapods/p/SZAVPlayer.svg?style=flat)](https://cocoapods.org/pods/SZAVPlayer)\n\n[中文说明](./README_cn.md)\n\nSZAVPlayer is a lightweight audio/video player library, based on `AVPlayer`, pure-Swift. Support cache and video image output.\n\n## Features\n\n- [x] Encapsulate the state changes of `AVPlayer` and `AVPlayerItem` and output them uniformly, greatly reducing the implementation cost of audio/video play.\n- [x] Achieved full control of `AVPlayer` data loading, based on `AVAssetResourceLoaderDelegate`. Through the Range request and corresponding cache, it can respond to player's requests ASAP. It also can play the cached audio/video normally in the weak network and no network enviroment.\n- [x] Support video image output, can be drawn to multiple views at the same time.\n- [x] Load AVAsset asynchronously to not blocking the main thread.\n- [x] Support setting cache size manually and also support cleaning.\n\n## Main Flow\n\n![Main Flow](./MainFlow.jpg)\n\n## Hint\n\n\u003e If you find that always play failed in the simulator, try exit simulator completely and restart again. This is kind of simulator's bug.\n\n## Usage\n\n1. Create player and set delegate.\n\n    ```swift\n    let player = SZAVPlayer()\n    player.delegate = self\n    \n    audioPlayer = player\n    ```\n\n2. Setup player with url.\n\n    ```swift\n    // uniqueID is to identify wether they are the same audio. If set to nil will use urlStr to create one.\n    let config = SZAVPlayerConfig(urlStr: audio.url, uniqueID: nil)\n    audioPlayer.setupPlayer(config: config)\n    ```\n    \n    or\n    \n    ```swift\n    // If you want play video, pass an additional parameter `isVideo`.\n    let config = SZAVPlayerConfig(urlStr: video.url, uniqueID: nil, isVideo: true, isVideoOutputEnabled: true/false)\n    videoPlayer.setupPlayer(config: config)\n    ```\n\n3. Implement `SZAVPlayerDelegate`.\n\n    ```swift\n    extension AudioViewController: SZAVPlayerDelegate {\n    \n        func avplayer(_ avplayer: SZAVPlayer, refreshed currentTime: Float64, loadedTime: Float64, totalTime: Float64) {\n            progressView.update(currentTime: currentTime, totalTime: totalTime)\n        }\n    \n        func avplayer(_ avplayer: SZAVPlayer, didChanged status: SZAVPlayerStatus) {\n            switch status {\n            case .readyToPlay:\n                SZLogInfo(\"ready to play\")\n                if playerStatus == .playing {\n                    audioPlayer.play()\n                }\n            case .playEnd:\n                SZLogInfo(\"play end\")\n                handlePlayEnd()\n            case .loading:\n                SZLogInfo(\"loading\")\n            case .loadingFailed:\n                SZLogInfo(\"loading failed\")\n            case .bufferBegin:\n                SZLogInfo(\"buffer begin\")\n            case .bufferEnd:\n                SZLogInfo(\"buffer end\")\n                if playerStatus == .stalled {\n                    audioPlayer.play()\n                }\n            case .playbackStalled:\n                SZLogInfo(\"playback stalled\")\n                playerStatus = .stalled\n            }\n        }\n    \n        func avplayer(_ avplayer: SZAVPlayer, didReceived remoteCommand: SZAVPlayerRemoteCommand) -\u003e Bool {\n            return false\n        }\n    \n    }\n    ```\n    \n4. Replace new audio.\n\n    ```swift\n    // The setupPlayer function will automatically determine if it has been setup before. \n    // If it is, it will directly call the replacePalyerItem function to replace the new audio.\n    audioPlayer.setupPlayer(config: config)\n    ```\n    \n    or\n    \n    ```swift\n    // or just use this function.\n    audioPlayer.replace(urlStr: audio.url, uniqueID: nil)\n    ```\n    \n    these two functions have the same effect.\n    \n5. Enable video image output.\n\n    - Set `isVideoOutputEnabled ` to `true`.\n    \n    ```swift\n    let config = SZAVPlayerConfig(urlStr: video.url, uniqueID: nil, isVideo: true, isVideoOutputEnabled: true)\n    videoPlayer.setupPlayer(config: config)\n    ```\n    \n    - Implement avplayer delegate function.\n    \n    ```swift\n    func avplayer(_ avplayer: SZAVPlayer, didOutput videoImage: CGImage) {\n        videoOutputView1.layer.contents = videoImage\n    }\n    ```\n    \n    - Call `removeVideoOutput` function when ready to release the player.\n    \n    ```swift\n    videoPlayer.removeVideoOutput()\n    ```\n    \n6. Seek player to time.\n\n    ```swift\n    audioPlayer.seekPlayerToTime(time: currentTime, completion: nil)\n    ```\n    \n7. Set max cache size.\n\n    ```swift\n    // Unit: MB, if reached the max size, it will automatically trim the cache.\n    SZAVPlayerCache.shared.setup(maxCacheSize: 100)\n    ```\n    \n8. Clean all cache.\n\n    ```swift\n    SZAVPlayerCache.shared.cleanCache()\n    ```\n    \n9. Play local files. Because there is no need to go through the custom loading process for local files, directly set `disableCustomLoading` to `true`.\n\n\t```swift\n\tlet config = SZAVPlayerConfig(urlStr: audio.url, uniqueID: nil)\n\tconfig.disableCustomLoading = true\n\taudioPlayer.setupPlayer(config: config)\n\t```\n\n## Example\n\nThe Example project has implemented a complete play example, including play/pause/previous/next/seekToTime/cleanCache, etc. \n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Requirements\n\n- iOS 10.0+\n- Swift 5.0+\n\n## Installation\n\n### CocoaPods\n\nSZAVPlayer is available through [CocoaPods](https://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'SZAVPlayer'\n```\n\n### Carthage\n\n[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate SZAVPlayer into your Xcode project using Carthage, specify it in your `Cartfile`:\n\n```ogdl\ngithub \"eroscai/SZAVPlayer\" ~\u003e 1.1.1\n```\n\n### Swift Package Manager\n\nFrom Xcode 11, you can use [Swift Package Manager](https://swift.org/package-manager/) to add SZAVPlayer to your project.\n\n- Select File \u003e Swift Packages \u003e Add Package Dependency. Enter https://github.com/eroscai/SZAVPlayer.git in the \"Choose Package Repository\" dialog.\n- Add `CoreServices.framework` and `AVFoundation.framework` to your project if not added before. (If anyone knows how to do this automatically, please tell me).\n\n## Author\n\neroscai, csz0102@gmail.com\n\n## License\n\nSZAVPlayer is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feroscai%2FSZAVPlayer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feroscai%2FSZAVPlayer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feroscai%2FSZAVPlayer/lists"}