1

SETUP

I put a mp3 into my bundle just for testing, i will have them download into the documents directory. But for now i am going to work off the bundle.

I have found a couple tutorials, but not anything for what i need. The below is definitely a mix of a couple solutions and probably not the best approach.

Problem

How do i pull a song out of my bundle and play within the app?

//Create an instance of MPMusicPlayerController
MPMusicPlayerController* myPlayer = [MPMusicPlayerController applicationMusicPlayer];

//Read the song off the bundle.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"LittleMoments" ofType:@"mp3"];  
 NSData *myData = [NSData dataWithContentsOfFile:filePath];

if (myData) { 

    //This wont work for me because i don't want to query songs from the iPod, 
    i want to play songs out of the Bundle.

    //Create a query that will return all songs by The Beatles grouped by album
    MPMediaQuery* query = [MPMediaQuery songsQuery];
    [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"The Beatles" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]];
    [query setGroupingType:MPMediaGroupingAlbum];

    //Pass the query to the player
    [myPlayer setQueueWithQuery:query];
 /////**** how can i get nsdata into MyPlayer?*****////// 

    //Start playing and set a label text to the name and image to the cover art of the song that is playing
    [myPlayer play];

}
zach
  • 1,281
  • 7
  • 27
  • 41

1 Answers1

4

Try playing the file with AVAudioPlayer

AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:myData error:nil];

audioPlayer.delegate = self;
[audioPlayer play];
tim
  • 1,682
  • 10
  • 18
  • i get this warning: passing viewcontroller *const_strong to parameter of incompatiable type id. It also gives a BAD_EXEC on [audioPlayer play]; – zach Mar 08 '12 at 22:11
  • remove the audioPlayer.delegate = self; line or add AVAudioPlayerDelegate as a protocol to your ViewController. – tim Mar 08 '12 at 22:24
  • Thanks for the help. i just googled AVAudioPlayer and found this tutorial and it worked wonders. http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/ Thanks again – zach Mar 10 '12 at 06:08