Obj-C iPhone Music App Help
I'm an Obj-c beginner, trying to make a simple iPhone music app. Basically, you press some different buttons and some different sounds are played. You may play a song from your iPod library, using an applicationMusicPlayer. Here is my code:
MusicViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "MusicAppDelegate.h"
@interface MusicViewController : UIViewController
<AVAudioPlayerDelegate, MPMediaPickerControllerDelegate>
{
IBOutlet UIButton *play;
MPMusicPlayerController *myPlayer;
NSMutableDictionary *sounds;
}
@property (nonatomic, retain) MPMusicPlayerController *myPlayer;
- (AVAudioPlayer *)playerForSoundFile:(NSString *)fileName;
- (IBAction)playClick:(id)sender //to play the instrument sound
- (IBAction)playSongClick:(id)sender //to pick a song from iPod library
- (IBAction)playPauseClick:(id)sender //to pause or continue playing song from library
@end
MusicViewController.m
- (id)init
{
if (! (self == [super init]))
return nil;
sounds = [[NSMutableDictionary alloc] init];
return self;
}
- (void)viewDidLoad {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(handle_PlaybackStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:self.myPlayer];
[self.myPlayer beginGeneratingPlaybackNotifications];
}
- (IBAction)playClick:(id)sender
{
AVAudioPlayer *theAudio = [self playerForSoundFile:@"Sound1"];
[theAudio play];
}
- (IBAction)pickSongClick:(id)sender
{
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
[picker setDelegate:self];
[picker setAllowsPickingMultipleItems:NO];
picker.prompt =
NSLocalizedString (@"Pick a song to play", "Prompt in media item picker");
picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical开发者_运维技巧;
[self presentModalViewController: picker animated: YES];
[picker release];
}
- (void)mediaPicker: (MPMediaPickerController *) picker
didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {
myPlayer = [MPMusicPlayerController applicationMusicPlayer];
[myPlayer setShuffleMode:MPMusicShuffleModeOff];
[myPlayer setRepeatMode:MPMusicRepeatModeNone];
[self dismissModalViewControllerAnimated: YES];
[myPlayer stop];
[myPlayer setQueueWithItemCollection: (MPMediaItemCollection *) mediaItemCollection];
[myPlayer play];
[playPause setImage:[UIImage imageNamed:@"pause.gif"] forState:UIControlStateNormal];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryAmbient
error: &setCategoryError];
- (void) mediaPickerDidCancel: (MPMediaPickerController *) picker {
[self dismissModalViewControllerAnimated: YES];
}
- (IBAction)playPauseClick:(id)sender
{
if (myPlayer.playbackState == MPMusicPlaybackStatePlaying) {
[myPlayer pause];
[playPause setImage:[UIImage imageNamed:@"play.gif"] forState:UIControlStateNormal];
} else if (myPlayer.playbackState == MPMusicPlaybackStatePaused) {
[myPlayer play];
[playPause setImage:[UIImage imageNamed:@"pause.gif"] forState:UIControlStateNormal];
} else {
[playPause setImage:[UIImage imageNamed:@"play.gif"] forState:UIControlStateNormal];
}
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryAmbient
error: &setCategoryError];
}
- (void)handle_PlaybackStateChanged:(id)notification
{
if (myPlayer.playbackState == MPMusicPlaybackStatePaused)
{
[playPause setImage:[UIImage imageNamed:@"pause.gif"] forState:UIControlStateNormal];
}
else
{
[playPause setImage:[UIImage imageNamed:@"play.gif"] forState:UIControlStateNormal];
}
}
- (AVAudioPlayer *)playerForSoundFile:(NSString *)fileName
{
NSString *path;
AVAudioPlayer *player = [sounds objectForKey:fileName];
if (! player)
{
path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
AVAudioPlayer *theAudio=[[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL] autorelease];
theAudio.delegate = self;
[sounds setObject:player forKey:fileName];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryAmbient
error: &setCategoryError];
}
return player;
[path release];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter]
removeObserver:self];
[sounds release];
[super dealloc];
}
I'm using an NSMutableDictionary to manage memory, because I am playing multiple sounds from different buttons, I posted just one of the button sound methods to show you how I'm doing it.
The problems are 1) handle_PlaybackStateChanged:(id)notification is not being called when the player is paused or the song finishes... 2) My instrument sound won't play when I click the button...
Any help is useful, thank you.
I believe you need to create a Queue of the wav files to play, so you can manage in order, otherwise they override each other, also where is you delegate set?
精彩评论