This is how I've code to open media picker list
- (void)viewDidLoad
{
[super viewDidLoad];
player=[MPMusicPlayerController iPodMusicPlayer];
picker=[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
[picker setDelegate:self];
picker.prompt=#"Add an audio to application";
}
I've also implemented its delegate method
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
/*I know only here I can get path but don't know how?
mediaItemCollection don't have such kind of properties or any
method to get selected file path.
So is there any other way to do so?
Note: I required this path, so that I can store it somewhere and can play it in future.
*/
}
Initialize a MPMediaItem object from some index of the mediaItemCollection.items array:
MPMediaItem *anItem = (MPMediaItem *)[mediaItemCollection.items objectAtIndex: row];
Then call the -valueForProperty: method:
NSURL *assetURL = [anItem valueForProperty: MPMediaItemPropertyAssetURL];
There are other properties that you can get from the MPMediaItem described here:http://bit.ly/GGs3XI
Look under "General Media Item Property Keys"
Hope this helps!
Tams
Related
I was looking at Apple's documentation, and I cannot seem to find a way to get whether or not an MPMediaItem is a 'favorite' track or not. See screenshot below, with the pink heart.
How can one get this property? I know since it's a new feature, it's availability would be limited to iOS 8.4 or later.
Here's some code I'm using to get other properties from MPMediaItems, via the music picker:
- (void) processMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
//iterate through selected songs
if (mediaItemCollection) {
NSArray *allSelectedSongs = [mediaItemCollection items];
for(MPMediaItem *song in allSelectedSongs)
{
NSURL *songURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
NSNumber *ident = [song valueForProperty:MPMediaEntityPropertyPersistentID];
NSString *identString = [BukketHelper convertULLToNSString:ident];
NSNumber *isCloud = [song valueForProperty:MPMediaItemPropertyIsCloudItem];
}
//do other stuff here
}
Anyone have ideas?
You have to use Apple Music API to get or set users's Like/Dislike to a song like this:
GET https://api.music.apple.com/v1/me/ratings/songs/{id}
From: Apple Docs link
I am trying to get the title of the song currently being played by the default Music app. Here is the method:
- (NSString*)getSongTitle {
MPMediaItem *currentSong = [[MPMusicPlayerController systemMusicPlayer] nowPlayingItem];
_title = [currentSong valueForProperty:#"MPMediaGroupingTitle"];
NSLog(_title);
return _title;
}
Everything I have read online says that this should be correct, but _title gets assigned nil every time. Any ideas?
I have #imported MediaPlayer by the way.
I think you are just using the wrong key. Try MPMediaItemPropertyTitle. It should look something like this:
- (NSString*)getSongTitle {
MPMediaItem *currentSong = [[MPMusicPlayerController systemMusicPlayer] nowPlayingItem];
_title = [currentSong valueForProperty:MPMediaItemPropertyTitle];
NSLog(_title);
return _title;
}
Try this:
- (NSString*)getSongTitle {
MPMediaItem *currentSong = [[MPMusicPlayerController systemMusicPlayer] nowPlayingItem];
_title = currentsong.title;
NSLog(_title);
return _title;
}
I am working on adding video to a chat application. The users can either share a video chosen from their files or record a new one to upload.
The issue I am having is differentiating between a video which has just been recorded and a video which has been selected as I want to save a just recorded video to their album when they upload it.
Currently my code looks like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// This checks whether we are adding image or video (public.movie for video)
if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:#"public.image"]) {
UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
[self sendImage:image];
}
else {
// SS-V
// If we are dealing with a video then we want to return to the chat view and post the video
NSURL * videoURL = (NSURL *)[info objectForKey:UIImagePickerControllerMediaURL];
// Send video to the chat view
[self sendVideo:[videoURL absoluteString]];
}
[tableView reloadData];
[picker dismissViewControllerAnimated:YES completion:Nil];
}
So I am getting the local url of the video and then uploading it to an external database.
I have also got this code:
// Save the recorded video to the iPhone
NSString * videoCompatibleString = [videoURL.absoluteString stringByReplacingOccurrencesOfString:#"file://" withString:#""];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoCompatibleString)) {
UISaveVideoAtPathToSavedPhotosAlbum (videoCompatibleString, self, nil, nil);
}
Which will save it to my phone library.
So now my only issue is how to differentiate between a video just taken and a video selected from my library already.
I was hoping there would be an elegant way of achieving this. Seemingly there isn't and so I set up an enum to record what I loaded the image picker with, if I loaded it with the camera then I knew that I would be taking the video or picture so should add it to the album:
In the header file:
typedef enum {
bPictureTypeCamera,
bPictureTypeAlbumImage,
bPictureTypeAlbumVideo,
} bPictureType;
// This allows us to see what kind of media is being sent to know if we need to save it to album
bPictureType _pictureType;
In the main file:
// If we have just taken the media then save it to the users camera
if (_pictureType == bPictureTypeCamera) {
[self saveMediaWithInfo:info];
}
With my save function checking if it is video or image and saving it:
- (void)saveMediaWithInfo: (NSDictionary *)info {
// Save image
if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:#"public.image"]) {
UIImageWriteToSavedPhotosAlbum([info objectForKey:UIImagePickerControllerEditedImage], self, nil, nil);
}
// Save Video
else {
// Make sure the video is in a compatible format to save
NSURL * videoURL = (NSURL *)[info objectForKey:UIImagePickerControllerMediaURL];
NSString * videoCompatibleString = [[videoURL absoluteString] stringByReplacingOccurrencesOfString:#"file://" withString:#""];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (videoCompatibleString)) {
UISaveVideoAtPathToSavedPhotosAlbum (videoCompatibleString, self, nil, nil);
}
}
}
Now the neatest answer but it does what it needs to
i'm developing a music player app.I have a tableView that contains the songs names of the ipod library.My problem is that when i select a cell it plays a specific song but when that song is completed it doesn't skip to the next item automatically.
This is my code when i select a cell:
MPMediaQuery *mq = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *songNamePredicate = [MPMediaPropertyPredicate
predicateWithValue:[songsTitle objectAtIndex:indexPath.row]
forProperty:MPMediaItemPropertyTitle];
[mq addFilterPredicate:songNamePredicate];
[mp setQueueWithQuery:mq];
[mp play];
i really need your help.Thanks.
It doesn't skip to the next song because the playback queue only has one item. At launch, set the playback queue to the entire songs query. Also create an array of MPMediaItems using the items property of MPMediaQuery:
MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
self.songs = songsQuery.items;
[self.musicPlayer setQueueWithQuery:songsQuery];
In tableView:didSelectRowAtIndexPath:, have the music player play the selected item using MPMusicPlayerController's setNowPlayingItem: method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MPMediaItem *song = self.songs[indexPath.row];
[self.musicPlayer setNowPlayingItem:song];
[self.musicPlayer play];
}
Here is a sample project that demonstrates this:
https://dl.dropboxusercontent.com/u/1990969/SongsPlayerTableView.zip
I am trying to play MPMedaiItem in MPMusicPlayerController in iOS.
In my case , i have a UITableView that show songs from playlist.
When i tap cell on UITableView , i want to play that song with MPMusicPlayerController.
And i also want to skip next songs from playlist when i tap Next Button.
How can i play it?
Here is some of my codes that write in didSelected Method of UITableView.
That doesn't play anything.
MPMediaItemCollection *songs = [self.arrayOfSongs objectAtIndex:indexPath.row ];
MPMediaItem *item = [songs representativeItem ];
NSLog(#"%#",[item valueForProperty:MPMediaItemPropertyTitle ]);
[self.player setNowPlayingItem:[item valueForProperty:MPMediaItemPropertyAssetURL]];
[self.player play ];
I know this is a little late, but your problem is that MPMusicPlayerController's nowPlayingItem property expects a MPMediaItem object, and you're passing it an NSString containing the URL of the asset. Here's an example of how this could be accomplished.
MPMusicPlayerController *controller = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:arrayOfMediaItems];
MPMediaItem *item = [collection representativeItem];
[controller setQueueWithItemCollection:collection];
[controller setNowPlayingItem:item];
[controller prepareToPlay];
[controller play];
I find it easier to use AVPlayer in an instance like this.
in your header file declare the AVPlayer object;
AVPlayer *audioPlayer;
Then in your method file use something like:
if(!audioPlayer){
audioPlayer = [[AVPlayer alloc] initWithURL:[item valueForProperty:MPMediaItemPropertyAssetURL]];
} else {
[audioPlayer replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:itemURL]];
}
[audioPlayer play];