2012-03-08 61 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