Quantcast
Channel: Envato Tuts+ Code - Mobile Development
Viewing all articles
Browse latest Browse all 1836

Build an iOS Music Player – Tuts+ Premium

$
0
0

This three-part tutorial series will teach you how to build a custom music player with the iOS SDK. Read on!


Tutorial Teaser

Step 1: Adding the Images

Download the source code attached to this project and drag all the images from the folder titled Images Part 3 into the project. Make sure “Copy items into destination group’s folder (if needed)” is checked and click “Finish”.

    Add Images

Step 2: Design the Now Playing Screen

Open the Storyboard and select the View Controller (not the view). Open the Attributes Inspector and select the “Hides Bottom Bar on Push” checkbox. Also change the Bottom bar in the Simulated Metrics section from “Inferred” to “None”. Now select the view and give it a black background. If you’re ready with that, drag an image view from the Object Library on the view and set the Mode from “Scale to Fill” to “Aspect Fit”. With these options set, the album artwork won’t be stretched out to fit the image view. Now open the Size Inspector and change its size to 320 by 320 pixels. Also modify the autoresizing as follows:

    Artwork Image View Autoresizing

With this autoresizing setup, the image will keep on top of the view and won’t be stretched out on a iPhone with a 3.5″ Retina screen. Now drag another image view onto the current view and modify the size options to look as follows:

    Control Background Size

With this size options the image view will stay at the bottom of the screen. We adjust this autoresizing option so our music player will look good on the newest iPhone 5, but also on an older iPhone or iPod touch. Now open the Attributes Inspector and set the Image to “Music-controls-background.png”.

Now that we have created the background for the controls, let’s next create the controls themselves. First drag a slider from the Object Library on top of the image view we just created. Change the autoresizing settings, so they look the same as the controls image view. After that give the slider a size of 228 by 23 pixels, set the X to 46 and set the Y to 470. Now open the Attributes inspector and set the “Min Track Tint” to a really dark color, so it’s almost black. You can leave the “Max Track Tint” to default.

Drag three buttons from the Object Library on top of the controls image view, change their type to “Custom”, delete the text and select the “Show Touch On Highlight” checkbox. Set the image of the first button to Previous-icon.png, the image of the second button to Play-icon.png and the image of the third button to Next-icon.png. Now open the Size Inspector and change the autoresizing settings so they are the same as the slider’s. Now change the size and position of the buttons as follows:

  • Button 1: width: 106 height: 47 X: 0 Y: 408
  • Button 2: width: 108 height: 47 X: 106 Y: 408
  • Button 3: width: 106 height: 47 X: 214 Y: 408

Now the interface of the now playing screen should look like the following:

    Now Playing Interface

The last thing we need to do for the design of the now playing screen is to add three labels for the song information. First, drag a view from the Object Library on top of the navigation bar. Change the background to “Clear Color”, set the width to 196 pixels and the height to 36 pixels. The X an Y should automatically adjust themselves. This view will contain the 3 labels for the song artist, title, and album names. Now drag three labels into the view we just added. Delete the text, set the Alignment to center and change the font to “System 12.0″ of all the labels. Now change the size and position of the labels as follows:

  • Label 1: width: 196 height: 12 X: 0 Y: 0
  • Label 2: width: 196 height: 12 X: 106 Y: 12
  • Label 3: width: 196 height: 12 X: 214 Y: 24

At last, select the second label and change the font to “System Bold 12.0″. This label will be for the songs title, so with a bold font it will be more prominent.

Now that we have finished the standard design of our now playing screen, I think it’s a good time to test our application. Click Build and Run to test the app. Play a song to go to the now playing screen. You should be able to see the controls, but of course they won’t work.

Step 3: Making the Outlets and Actions

To make our now playing screen work, we first need to create some new files. Go to “File” >“New” >“File…” to create a new file. Select “Objective-C class” and then click “Next”. Enter “NowPlayingViewController” for the class and make sure that it’s a subclass of UIViewController and that both the checkboxes are not selected. Click “Next” again and then click “Create”.

Open NowPlayingViewController.h and modify the code to read as follows:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface NowPlayingViewController : UIViewController
{
    MPMusicPlayerController *musiPlayer;
    IBOutlet UIImageView *artworkImageView;
    IBOutlet UIButton *playPauseButton;
    IBOutlet UISlider *volumeSlider;
    IBOutlet UILabel *artistLabel;
    IBOutlet UILabel *titleLabel;
    IBOutlet UILabel *albumLabel;
}
@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
- (IBAction)playPause:(id)sender;
- (IBAction)nextSong:(id)sender;
- (IBAction)previousSong:(id)sender;
- (IBAction)volumeSliderChanged:(id)sender;
- (void) registerMediaPlayerNotifications;
@end

Here we first import the MediaPlayer framework, then we created an MPMusicPlayerController object which we will use to control the music. After that, we create some outlets for the interface elements and at last we create the actions.

Now open the Storyboard and select the View Controler. Open the Identity Inspector and change the Class to the NowPlayingViewController we just created. Then open the Connections Inspector and connect the outlets as follows:

  • Connect the albumLabel outlet to the first label in the navigation bar.
  • Connect the artistLabel outlet to the third label in the navigation bar.
  • Connect the artworkImageView outlet to large the image view.
  • Connect the playPauseButton outlet to the button with the play icon.
  • Connect the titleLabel outlet to the second label in the navigation bar.
  • Connect the volumeSlider outlet to the slider.

Now connect the actions as follows:

  • Drag from the nextSong: action to the button with the next icon and select “Touch Up Inside” from the pop-up menu.
  • Drag from the playPause: action to the button with the play icon and select “Touch Up Inside” from the pop-up menu.
  • Drag from the previousSong: action to the button with the previous icon and select “Touch Up Inside” from the pop-up menu.
  • Drag from the volumeSliderChanged: action to the slider and select “Value Changed” from the pop-up menu.

Get the Full Series!

This tutorial series is available to Tuts+ Premium members only. Read a preview of this tutorial on the Tuts+ Premium web site or login to Tuts+ Premium to access the full content.


Joining Tuts+ Premium. . .

For those unfamiliar, the family of Tuts+ sites runs a premium membership service called Tuts+ Premium. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Mobiletuts+, Nettuts+, Aetuts+, Audiotuts+, Vectortuts+, and CgTuts+. You’ll learn from some of the best minds in the business. Become a premium member to access this tutorial, as well as hundreds of other advanced tutorials and screencasts.



Viewing all articles
Browse latest Browse all 1836

Trending Articles