Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MPMediaItems with AVAudioPlayer

Is it possible to pick media items using MPMediaPickerController and then load them into an AVAudioPlayer object?

like image 996
some_id Avatar asked Mar 07 '11 16:03

some_id


People also ask

How to play and pause audio in avaudioplayer?

Then, declare the following state linking to AVAudioPlayer before the body variable and after the struct: As you can see, we have set up a text with 2 buttons below, one for playing the audio, the other one for pausing it. the syntax is pretty simple: audioPlayer.pause () and audioPlayer.play ().

What is the avaudioplayer class for?

The AVAudioPlayer class has other properties to control playback (such as the number of times to loop) as well as properties that give you information about the sound file (such as the duration). Check out the Apple class reference document for more information! Have you teamed up with a partner to develop an app?

How should my avaudioplayer code look like?

At the end, your code should look like the following: The AVAudioPlayer class has other properties to control playback (such as the number of times to loop) as well as properties that give you information about the sound file (such as the duration). Check out the Apple class reference document for more information!

How do I integrate AVKit with avaudioplayer?

First, import the AVKit framework at the top of your ContentView.Swift file: Then, declare the following state linking to AVAudioPlayer before the body variable and after the struct:


3 Answers

If MPMusicPlayerController doesn't meet your needs, you can copy the audio to your local bundle so you can use AVAudioPlayer.

EDIT

You basically have three options for playing audio from the user's iPod library: MPMediaPlayer, AVPlayer and AVAudioPlayer.

Here are examples for MPMediaPlayer and AVPlayer:

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker    didPickMediaItems: (MPMediaItemCollection *) collection {       MPMediaItem *item = [[collection items] objectAtIndex:0];     NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];      [self dismissModalViewControllerAnimated: YES];      // Play the item using MPMusicPlayer      MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];      [appMusicPlayer setQueueWithItemCollection:collection];     [appMusicPlayer play];       // Play the item using AVPlayer      AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];     AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];     [player play];    } 

If you need to use AVAudioPlayer for some reason, or you need access to the audio file's actual audio data, you have to first copy the audio file to your app's directory and then work with it there. The AVAsset + AVPlayer stuff is the closest analogy to ALAsset if you're used to working with photos and videos.

like image 162
Art Gillespie Avatar answered Sep 28 '22 12:09

Art Gillespie


I wanted to add (and I can't comment on SO yet) that it seems like in iOS 8, and perhaps before, songs that are stored on iCloud do not have a value for the property assetURL and return null for [npItem valueForProperty:MPMediaItemPropertyAssetURL].

Here is a sample output:

MPMediaItem *npItem = self.musicPlayerController.nowPlayingItem;
NSLog(@"Song Title: %@\n assetURL: %@\n Cloud Item: %d", npItem.title, [npItem valueForProperty:MPMediaItemPropertyAssetURL], npItem.cloudItem)


// Log Output
Song Title: Time We Had
assetURL: (null)
Cloud Item: 1

Song Title: The Quiet
assetURL: ipod-library://item/item.m4a?id=1529654720874100371
Cloud Item: 0
like image 22
dillondrenzek Avatar answered Sep 28 '22 11:09

dillondrenzek


Just wanted to say that it appears that in iOS 6 and 7, AVAudioPlayer can play file URLs directly from the iPod without having to copy the audio data into your app directory as Art suggested.

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) collection {


    MPMediaItem *item = [[collection items] objectAtIndex:0];
    NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];

    // Play the item using AVPlayer
    self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [self.avAudioPlayer play];
}
like image 20
nvrtd frst Avatar answered Sep 28 '22 11:09

nvrtd frst