从nsbundle播放mp3

时间:2012-03-08 21:24:46

标签: ios ios4 mp3 nsbundle mpmusicplayercontroller

SETUP

我将一个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];

}

1 个答案:

答案 0 :(得分:4)

尝试使用AVAudioPlayer播放文件

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

audioPlayer.delegate = self;
[audioPlayer play];