Воспроизведение mp3 с nsbundle - PullRequest
1 голос
/ 09 марта 2012

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 Ответ

4 голосов
/ 09 марта 2012

Попробуйте воспроизвести файл с помощью AVAudioPlayer

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

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