Using AVQueuePlayer to get current track information - iphone

I am loading my iPod library into AVQueuePlayer and playing it using this:
[[AVQueuePlayer alloc]]initWithItems:[MPMediaCollectionInstance items] ]; //just one line.
But how do I read which MPMediaItem is currently playing? I want to know information like artist / song name etc.
Thanks.

Have the instance of the AVQueuePlayer that you have allocated.
AVQueuePlayer *_queuePlayer = [[AVQueuePlayer alloc] initWithItems:[MPMediaCollectionInstance items]];
With that instance, you can get the AVPlayerItem.
AVPlayerItem *currentItem = _queuePlayer.currentItem;
For the above line, please check the doc reference.
And now try the following code
NSArray *metadataList = [currentItem.asset commonMetadata];
for (AVMetadataItem *metaItem in metadataList) {
NSLog(#"%#",[metaItem commonKey]);
}
Which will give a list as follows:
title
creationDate
artwork
albumName
artist
Now you can get the value for corresponding keys. For this, you have to refer this doc too.

Related

Update current song name AVPLayer Xcode

I would like to update current song name into this picture. I don't know how to call that one and need to show image. I am using AVPlayer now. I would like to know how to do.
I have tried this but it show error.
Undefined symbols for architecture armv7s:"_MPMediaItemPropertyTitle", referenced from: -[PlayViewController playPressed] in PlayViewController.o
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
infoCenter.nowPlayingInfo =
[NSDictionary dictionaryWithObjectsAndKeys:#"my title", MPMediaItemPropertyTitle,
#"my artist", MPMediaItemPropertyArtist,
nil];
Did you #import <MediaPlayer/MediaPlayer.h> in this class?
Is the MediaPlayer framework linked to your project?

iOS get video frame rate

I want to get the frame rate for a specific video. I tried to look at APIs in the AVFoundation and AssetsLibrary like AVURLAsset or AVAssetReader. None of them are really helpful. Does anybody know a method in the Apple's frameworks/library to get the frame rate
The easier option is to obtain an AVAssetTrack and read its nominalFrameRate property.
Looks like this can help you.
Nominal frame rate is the frame rate of the track, in frames per second. (read-only)
AVAssetTrack Class
#property(nonatomic, readonly) float nominalFrameRate
For iOS 7+ you can use the currentVideoFrameRate property of AVPlayerItemTrack. Its the only consistent property that I've seen measure FPS. The nominalFrameRate property seems to be broken in HLS streams.
AVPlayerItem *item = AVPlayer.currentItem; // Your current item
float fps = 0.00;
for (AVPlayerItemTrack *track in item.tracks) {
if ([track.assetTrack.mediaType isEqualToString:AVMediaTypeVideo]) {
fps = track.currentVideoFrameRate;
}
}
For the property currentVideoFrameRate, from Apple's document:
If the item is not playing, or if the media type of the track is not
video, the value of this property is 0.0.
It requires the item is in "playing", so I used nominalFrameRate instead.
It still works in iOS 12, swift 4:
let tracks = asset.tracks(withMediaType: .video)
let fps = tracks?.first?.nominalFrameRate
Remember to handle nil checking.

MPMediaItemCollection Delete selected que/collection?

I am currently looking at Apple's AddMusic example and playing around with it before I start rewriting it into my application.
I noticed that it makes its own little playlist of the songs qued. I want to use the swipe action on the table view to remove songs that a use clicked by mistake.
I have implemented the swipe action but can't work out a way to delete that specific row?
Any idea would be great, below is the code to add it. I tried doing the reverse with no luck. If it's not possible how should I go about it?
Cheers
MainViewController *mainViewController = (MainViewController *) self.delegate;
MPMediaItemCollection *currentQueue = mainViewController.userMediaItemCollection;
MPMediaItem *anItem = (MPMediaItem *)[currentQueue.items objectAtIndex: row];
An MPMediaItemCollection is immutable, ie. you can't change the items. You need to create a new one with all items less the one you want to remove. See below:
NSArray* items = [currentQueue items];
NSMutableArray* array = [NSMutableArray arrayWithCapacity:[items count]];
[array addObjectsFromArray:items];
[array removeObjectAtIndex:row];
MPMediaItemCollection* newCollection = [MPMediaItemCollection collectionWithItems:array];
Be careful to not create an empty collection. It's not allowed and the MPMediaItemCollection will raise an exception.

Playing from iPod music library

I want to play particular selected song from my ipod music library.How can i do that?
My idea is to save the title name from MPMediaQuery and then play later on when app starts
so any one can have idea to do this?
Thank You.
The basic solution is to save the unique identifier each song in the library has, aka MPMediaItemPropertyPersistentID. You can use this ID to play the song, and you can save the ID to memory in order to remember the song the user selected between launches. If you don't know how the Media Player Framework works, look at the AddMusic sample code.
Your view controller must implement the MPMediaPickerControllerDelegate protocol. Assuming you're just allowing the user to select a single song, then the basic outline of your callback will look something like this.
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
NSArray* items = [mediaItemCollection items];
if ([items count] == 1)
{
MPMediaItem* song = (MPMediaItem *)[items objectAtIndex:0];
NSNumber* persistentId = [song valueForProperty:MPMediaItemPropertyPersistentID];
// ...Save/Play here...
}
}
At this point you can use the persistent ID to play the song, and/or save it to user defaults.

Cocoa - Add Video Watermark General Info

Just looking for how to programmatically add a watermark or some sort of overlay to video using cocoa. Not looking for a step by step ( although that would awesome ), but more or less looking for where I should start looking to learn how. Are there frameworks developed to work for this. Would like something native to cocoa or objective-c or c because I would like to eventually give this a go on the iPhone. Any help would be great.
I'm not sure if you mean just for playback, or if you'd like to export a video with a watermark that'll show up on other players.
If you mean just for playback, you can probably just add a view on top of the player view on Mac and iPhone that contains the watermark.
If you'd like a watermark on the video itself, this is hard on the Mac and probably impossible on the iPhone without essentially rewriting QuickTime.
On the Mac, the code might look like this (you need to import QTKit):
// Make a new movie so we don't destroy the existing one
QTMovie* movie = [[QTMovie alloc] initWithMovie:currentMovie
timeRange:QTMakeTimeRange(QTMakeTime(0,1000), [currentMovie duration])
error:nil];
// Make it editable
[movie setAttribute:[NSNumber numberWithBool:YES]
forKey:QTMovieEditableAttribute];
//Get the size
NSValue *value = [movie attributeForKey:QTMovieNaturalSizeAttribute];
NSSize size = [value sizeValue];
// Add a new track to the movie and make it the frontmost layer
QTTrack *track = [movie addVideoTrackWithSize:size];
[track setAttribute:[NSNumber numberWithShort:-1] forKey:QTTrackLayerAttribute];
// Create a codec dictionary for the image we're about to add
NSDictionary *imageDict = [NSDictionary dictionaryWithObjectsAndKeys:
#"tiff", QTAddImageCodecType,
[NSNumber numberWithLong:codecHighQuality], QTAddImageCodecQuality, nil];
// Get the video length in QT speak
QTTime qttime = [currentMovie duration];
NSTimeInterval reftime;
QTGetTimeInterval(qttime, &reftime);
//Add the image for the entire duration of the video
[track addImage:image forDuration:qttime withAttributes:imageDict];
// Finally, tell the track that it should use its alpha correctly
MediaHandler media = GetMediaHandler([[track media] quickTimeMedia]);
MediaSetGraphicsMode(media, graphicsModeStraightAlpha, NULL);
... And that's it! Your movie now has a watermark, and you can export it to file.