2012-03-08 4 views
1

SETUPmp3 de jouer NSBundle

Je mis un mp3 dans mon paquet juste pour le test, je vais devoir les télécharger dans le répertoire des documents. Mais pour l'instant, je vais travailler sur le bundle.

J'ai trouvé quelques tutoriels, mais rien pour ce dont j'ai besoin. Ce qui suit est certainement un mélange de quelques solutions et probablement pas la meilleure approche.

Problème

Comment puis-je tirer une chanson de mon paquet et jouer dans l'application?

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

} 

Répondre

4

Essayez de lire le fichier avec AVAudioPlayer

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

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

je reçois cet avertissement: passer viewcontroller * const_strong au paramètre de type id incompatiable . Il donne également un BAD_EXEC sur [lecture audioPlayer]; – zach

+0

supprime le fichier audioPlayer.delegate = self; ligne ou ajouter AVAudioPlayerDelegate en tant que protocole à votre ViewController. – tim

+0

Merci pour l'aide. J'ai juste googlé AVAudioPlayer et trouvé ce tutoriel et cela a fait des merveilles. http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/ Merci encore – zach