从nsbundle播放mp3
build立
我把一个mp3放到我的软件包中进行testing,我将它们下载到文档目录中。 但是现在我要解决这个问题了。
我find了几个教程,但没有什么我需要的。 下面绝对是一对夫妇的解决scheme的组合,可能不是最好的方法。
问题
我如何从我的包中拉出一首歌曲,并在应用程序内播放?
//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]; }
尝试使用AVAudioPlayer播放文件
AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:myData error:nil]; audioPlayer.delegate = self; [audioPlayer play];