I need to start playing a song (which is in the iPod library) in another iOS App using the name of the song.
I've studied some of the MediaPlayer framework, but didn't find anything useful.
I know this can be done, as a couple of apps in the App Store do it, such as SoundHound, which lets you play a certain song you've discovered if you have it in your iPod library.
try this :
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-music-library-access/
EDIT:
you can try this to reach songs:
#pragma mark - Media Picker
- (IBAction)showMediaPicker:(id)sender
{
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES;
mediaPicker.prompt = #"Select songs to play";
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
if (mediaItemCollection) {
[musicPlayer setQueueWithItemCollection: mediaItemCollection];
[musicPlayer play];
}
[self dismissModalViewControllerAnimated: YES];
}
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
[self dismissModalViewControllerAnimated: YES];
}
If you would like to play a song with a specific title, without requiring the user to select the song manually with the Media Picker, you might want to try a Media Player Query using the song title.
MPMediaPropertyPredicate *titlePredicate =
[MPMediaPropertyPredicate predicateWithValue:searchText
forProperty:MPMediaItemPropertyTitle
comparisonType:MPMediaPredicateComparisonContains];
NSSet *predicates = [NSSet setWithObjects: titlePredicate, nil];
MPMediaQuery *songsQuery = [[MPMediaQuery alloc] initWithFilterPredicates: predicates];
NSLog(#"%#", [songsQuery items]);
There is are good code examples and more discussion on queries at MPMediaQuery search for Artists, Albums, and Songs
Related
I want to show the list of all songs from the user's ipod in a list inside my application. When the user clicks a song I want to store the name of the this song.
I also later want to grab the name of that song and play it (but not right away).
Any ideas where to start for this? I know it is probably somewhere in the Media.Player framework but I can't seem to figure out how to actual view the list of songs from inside the application
You can call up the MPMediaPickerController using this:
- (IBAction) selectSong: (id) sender
{
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = NO;
picker.prompt = NSLocalizedString (#"Select any song from the list", #"Prompt to user to choose some songs to play");
[self presentModalViewController: picker animated: YES];
}
Then you can add the songs that were selected to your own array using something like this.
Note: You will access meta-data information from each track using valueForProperty.
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
[self dismissModalViewControllerAnimated: YES];
someMutableArray = [mediaItemCollection mutableCopy];
}
Then this is kind of self explanatory but necessary:
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
[self dismissModalViewControllerAnimated: YES];
}
For more information visit Apple's iPod Library Access Programming Guide
I am using a
MPMediaPickerController
to pick songs to add to a playlist in my app. using the following code,
for selecting a song from a Genius playlist the song title is overlapping the picker title as shown in the image1. for any other case the text is not overlapping.
can someone please help me how to fix that.
- (void)addToPlaylist: (id)sender {
MPMediaPickerController* picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyVideo];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.prompt = NSLocalizedString (#"AddSongsToPlaylist", nil);
[self presentModalViewController: picker animated: YES];
[picker release];
}
-----------------image 1------------------------------ normal song selection
I am testing the following simple code on my iphone 4 with iOS 5.0 on it. I implemented the following sample code to play a user selected song. The user clicks on the a button on the page, its shows his playlist/songs .... but once the song is selected and picker is dismissed, the song doesnt play. What gives?
From my *.h file
#interface MusicPlayerDemoViewController : UIViewController <MPMediaPickerControllerDelegate> {
MPMusicPlayerController *musicPlayer;
}
#property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
- (IBAction)selectMusic:(id)sender;
from my *.m file
- (IBAction)selectMusic:(id)sender
{
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = NO; // this is the default
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];
}
// Media picker delegate methods
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
[musicPlayer setQueueWithItemCollection:mediaItemCollection];
[musicPlayer play];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
[self dismissModalViewControllerAnimated:YES];
}
SOLUTION:
Add the following code in ViewDidLoad. I wasn't instantiating the player. All works
[self setMusicPlayer: [MPMusicPlayerController applicationMusicPlayer]];
// By default, an application music player takes on the shuffle and repeat modes
// of the built-in iPod app. Here they are both turned off.
[musicPlayer setShuffleMode: MPMusicShuffleModeOff];
[musicPlayer setRepeatMode: MPMusicRepeatModeNone];
I'm diving into iOS development and have been slowly building my own alarm clock app to learn how to develop on the platform. I want my alarm clock to allow me to display a list of songs on my iOS device, choose only one, and have it play when the alarm fires. I've figured out how to use the MPMediaPicker to display the list of songs and allow the user to select songs that are ultimately added to a MPMediaItemCollection that is used to tell the MPMediaPlayer object which songs to play. Here's the code for all that...
- (IBAction) selectSong: (id) sender {
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = NO;
picker.prompt = NSLocalizedString (#"Select any song from the list", #"Prompt to user to choose some songs to play");
[self presentModalViewController: picker animated: YES];
[picker release]; }
Store the song...
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {
[self dismissModalViewControllerAnimated: YES];
selectedSongCollection=mediaItemCollection; }
Dismiss the picker...
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker {
[self dismissModalViewControllerAnimated: YES]; }
Now this code allows you to choose a song and play it at any point while the app is running. My questions are...
How can I store that song information in the userInfo dictionary that's included as part of the local notification that represents my alarm being triggered?
my other question is, once I'm able to retrieve that song info from the local notification, how do I play it?
I'm so new to all this that I'm really having a hard time understanding how this would work. Thanks so much in advance for your help!
save the representativeItem from the collection returned to user info dictionary
when you want to play the song back, use MPMediaQuery to get the specific song to play.
http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMediaQuery_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008220
details on how to query for the stored song
http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMediaPropertyPredicate_ClassReference/Reference/Reference.html#//apple_ref/occ/clm/MPMediaPropertyPredicate/predicateWithValue:forProperty:
Apple Documentation for Querying data, plus examples
I'm having trouble implementing MPMoviePlayerController. I've downloaded and run the MoviePlayer app, and it works fine, but I'm unable to reproduce this functionality in my own app. I have added the MediaPlayer Framework and imported to my app.
Here are the relevant parts of my code:
UITableViewController class:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==1) {
MyAppDelegate *appDelegate =
(MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate initAndPlayMovie:[self localMovieURL]];
}
}
MyAppDelegate:
-(void)initAndPlayMovie:(NSURL *)movieURL
{
// Initialize a movie player object with the specified URL
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
self.moviePlayer = mp;
[mp release];
[self.moviePlayer play];
}
}
I did some debugging in
- (void) moviePreloadDidFinish:(NSNotification*)notification {
NSLog(#"moviePreloadDidFinish");
}
and in
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSLog(#"moviePlayBackDidFinish");
}
I noticed that the movie loads correctly and plays, I guess somewhere in the background, for the appropriate amount of time, but I just can't see the Movie Player. The view never changes from my TableViewController!
Any Ideas? Please help!!!