what is avurlasset work and simplyfie this mehtod? - ios

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:assetURL options:#{AVURLAssetPreferPreciseDurationAndTimingKey:#YES}];
// audio asset url create object and specifies its method....
this audio asset can get value in only second?
I want to know how above code works and role of AVURLAsset

AVURLAsset is a subclass of AVAsset. AVAsset is used to get information about the asset such as metadata (like track titles, author, composer, and so on). The "asset" is usually a sound or video file.
In the example you have given, you are creating an AVAsset from a URL, and you are specifying that you want precise duration and timing. Because of that option, when you ask for the duration, there may be a significant amount of work required for the AVAsset method to compute a precise time. This is clearly stated in the Apple documentation. The documentation also suggests that you usually do not need the precise duration and timing.

The (no-)difference between AVAsset and AVURLAsset as of 2022-12-08:
https://developer.apple.com/documentation/avfoundation/avurlasset
This class is a concrete subclass of AVAsset. When you create an asset as shown below, the system creates and returns an instance of AVURLAsset.
let url: URL = // A local or remote asset URL.
let asset = AVAsset(url: url)

Related

How can you obtain a current manifest from the playhead in AV player?

I am currently trying to capture the currently playing HLS/DASH manifest in the AV player.
Would anyone have any code snippets/documentation that could help with it?
To access the current url that the avplayer is streaming from:
if let currAsset = player.currentItem.asset as? AVURLAsset {
var url = currAsset.url
... use the url ...
}
I didn't find any official documentation about this either. I suppose then at the time of writing that AVPlayer does not provide the manifest.
Failing that, consider downloading the playlist and parse the manifest by your own.
I found M3U8Kit useful to parse m3u8 manifest.

How to load native album video using swift

I'm an undergraduate student and I'm witring an iPhone HumanSeg app. But now I have a problem, that I have a native video in album, and I need to load that video into my code and do some processing. My codes are below:
let filePath = Bundle.main.path(forResource: "1", ofType: "mp4")
let videoURL = NSURL(fileURLWithPath: filePath!)
let avAsset = AVAsset(url: videoURL as URL)
But when I run this code, Xcode just tells me that filePath is nil. I assert that 1.mp4 is in both Assets.xcaassets and iPhone album. Is there anyone who'd like to offer some help?
By the way, How can I get the images(in UIImage format) in the video at the fastest speed? For each image at given time, I really have to read it in no more than 5ms so I may output the preserved video at a good fps.
Check target's build phases, whether the file is being copied to the bundle.Also the check the box to include the file. This code is correct for fetching that file.

Streaming .mp3 with AVURLAsset + AVPlayerItem + AVPLayer

I have an URL. It looks like this:
https://content.stage.someCompany.net/deliveries/artistNameHere/songNameHere-128.mp3?Expires=someNumberHere&Signature=someReallyReallyReallyLongStringHere&Key-Pair-Id=someIdHere
Let me break it down in pieces:
https://content.stage.someCompany.net/deliveries/artistNameHere/songNameHere-128.mp3
?Expires=someNumberHere
&Signature=someReallyReallyReallyLongStringHere
&Key-Pair-Id=someIdHere
As you can see, it's just a glorified .mp3 restricted to 128 kbps, with some security stuff at the end.
If I load it in Safari on my Mac, it will play. If I pass it to an AVPlayer constructor in my iOS app, it will play as well.
If, however, I use it to create an AVURLAsset, it reports that .isPlayable is false. If I stubbornly persist in further creating an AVPlayerItem based on that asset, it will report AVPlayerItemStatusFailed.
Needless to say, in these conditions my AVURLAsset + AVPlayerItem + AVPLayer infrastructure, which culminates in player.play(), actually plays no music.
However, it does successfully play if I substitute other URLs, like Apple's own https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8
or
(some random .mp3 from another stackoverflow topic) http://podcast.cbc.ca/mp3/podcasts/asithappens_20160907_50906.mp3
The differences that I see: Apple's url is in fact a "playlist" of some sort, while the second one in a plain, "civilised" .mp3. No more security mambo-jumbo at the end of the link.
Why won't my url play? Do I need to do something specific with the security stuff? Right now, I'm just naively "hey, AVURLAsset...here's my (entire) URL...do your stuff with it..."
Found it.
The links that I receive are valid only once. Apparently that's why "the security" is in place at the end of them.
Testing one in Safari and "seeing that it works" invalidates it. Subsequently, trying the same one in the app results is .isPlayable = false.
Simply requesting & using directly in the app results in .isPlayable = true.
So AVURLAsset + AVPlayerItem + AVPLayer are working just fine. I was just a bloody fool.

Get UID URL for ALAsset with ALAssetPropertyAssetURL

I get asset URL by calling [asset valueForProperty:ALAssetPropertyAssetURL] sometimes it returns nil or may be asset is nil (I've seen it in the crash logs).
I've read in the documentation:
ALAssetPropertyAssetURL
The key to retrieve a URL identifier for the asset.
The corresponding value is an NSURL object.
This URL is used by the library-change notifications to identify assets and asset groups. Only the ALAssetRepresentation and ALAssetsGroup classes support this property.
I think it is mean, that I can't get ALAssetPropertyAssetURL property from ALAsset, but it works any time when I run it.
Questions:
How I can get Asset url to use it in [PHAsset fetchAssetsWithALAssetURLs: ....]?
UPD
I don't understand this: "How to get URL if only the ALAssetRepresentation and ALAssetsGroup classes support this property?"
We could use something like this with ALAsset object
[[asset defaultRepresentation] url]

AVAsset from app sandbox

I'm working with AVAsset in my app and met next problem.
When I'm writing something like this:
AVAsset *audioAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Hello.mp3", [[NSBundle mainBundle] resourcePath]]]];
its work fine. But when I trying get fresh recorded file from my tmp directory from in app's sandbox like that:
AVAsset *audioAsset2 = [AVAsset assetWithURL:[NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingPathComponent:[#"speechRecord" stringByAppendingPathExtension:#"m4a"]]]];
it doesn't work too. Even if I trying add video file to asset from sandbox - result is the same.
I even tried work with AVURLAsset, but asset always empty too. I need it to mix two audio files between themselves and then merge it with recorded video. If I can do that without AVAssets and there is another way, I will appreciate if You tell me. Or may there is some another function for that?
You create the second URL for audioAsset2 with the URLWithString selector. Your first asset loaded correctly, because you used the fileURLWithPath selector.
If you want to load a file at a given URL always create the NSURL object with the fileURLWithPath selector.
So simply try:
AVAsset *audioAsset2 = [AVAsset assetWithURL:[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[#"speechRecord" stringByAppendingPathExtension:#"m4a"]]]];

Resources