2012-03-08 30 views
1

SETUP從一個NSBundle

播放MP3我把MP3變成我的包只是爲了測試,我將它們下載到的文件目錄。但現在我要解決這個問題。

我找到了一對夫婦的教程,但沒有任何我需要的東西。下面絕對是一對夫婦的解決方案的組合,可能不是最好的方法。

問題

如何我拉一首我的包和應用程序內玩?

//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]; 

} 

回答

4

嘗試播放文件與AVAudioPlayer

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

audioPlayer.delegate = self; 
[audioPlayer play]; 
+0

我得到這樣的警告:通過視圖 - 控制* const_strong到incompatiable ID類型的參數。它還在[audioPlayer播放]上提供了BAD_EXEC; – zach 2012-03-08 22:11:45

+0

刪除audioPlayer.delegate = self;或者將AVAudioPlayerDelegate作爲協議添加到ViewController中。 – tim 2012-03-08 22:24:16

+0

感謝您的幫助。我只是google了AVAudioPlayer,發現這個教程和它的奇蹟。 http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/再次感謝 – zach 2012-03-10 06:08:37