Using MPMusicPlayerController and MPMediaPickerController from Monotouch to play music from your library

Using the handy MPMusicPlayerController and MPMediaPickerController, you can interact with your iPod library on your iPhone. You can select and play music (podcasts, etc) using these classes and the associated MPMediaPickerControllerDelegate without leaving your application (actually the music plays in the background just like the iPod app). Here’s a short demo app that will open the MPMediaPickerController modally to allow selection of a song. The song is queued in the MPMusicPlayerController when it is selected. For simplicity I added 2 buttons, 1 to open the controller and another to play whatever you selected. This only works on the device. Here’s the code for this small “app.” (Note: this example requires a patch that will be in monotouch 1.4).

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.MediaPlayer;

namespace iPodDemo
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}

public partial class AppDelegate : UIApplicationDelegate
{
MPMusicPlayerController _musicPlayer;

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
_musicPlayer = new MPMusicPlayerController ();

window.AddSubview (viewController.View);

iPodButton.TouchUpInside += (o, e) =>
{
MPMediaPickerController mediaController = new MPMediaPickerController (MPMediaType.MPMediaTypeMusic);
mediaController.AllowsPickingMultipleItems = false;
mediaController.Delegate = new MediaPickerDelegate (_musicPlayer, viewController);
viewController.PresentModalViewController (mediaController, true);
};

playButton.TouchUpInside += (o, e) =>
{
_musicPlayer.Stop ();
_musicPlayer.Play ();
};

window.MakeKeyAndVisible ();

return true;
}

public override void OnActivated (UIApplication application)
{
}
}

public class MediaPickerDelegate : MPMediaPickerControllerDelegate
{
MPMusicPlayerController _musicPlayer;
UIViewController _viewController;

public MediaPickerDelegate (MPMusicPlayerController musicPlayer, UIViewController viewController) : base()
{
_musicPlayer = musicPlayer;
_viewController = viewController;
}

public override void MediaItemsPicked (MPMediaPickerController sender, MPMediaItemCollection mediaItemCollection)
{
_musicPlayer.SetQueue (mediaItemCollection);
_viewController.DismissModalViewControllerAnimated (true);
}

public override void MediaPickerDidCancel (MPMediaPickerController sender)
{
_viewController.DismissModalViewControllerAnimated (true);
}
}

2 thoughts on “Using MPMusicPlayerController and MPMediaPickerController from Monotouch to play music from your library

  1. Pingback: MonoTouch.Info
  2. Hi mike,
    That’s a great tutorial .
    I am using camera capture in my project and store it in the device path,
    I got the camera invoked. How can i save the captured image to the device path in the “FinishedPickingmedia” class,or any idea or suggestion may be useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s