playing mp3 from nsbundle

烈酒焚心 提交于 2019-12-23 04:07:33

问题


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

}

回答1:


Try playing the file with AVAudioPlayer

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

audioPlayer.delegate = self;
[audioPlayer play];


来源:https://stackoverflow.com/questions/9625155/playing-mp3-from-nsbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!