我是一个 Obj-c 初学者,正在尝试制作一个简单的 iPhone 音乐应用程序。基本上,您按下一些不同的按钮,就会播放一些不同的声音。您可以使用应用程序MusicPlayer 从 iPod 库中播放歌曲。这是我的代码:
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];
}
我正在使用 NSMutableDictionary 来管理内存,因为我正在从不同的按钮播放多种声音,所以我只发布了一种按钮声音方法来向您展示我是如何做到的。
问题是 1)handle_PlaybackStateChanged:(id)notification 当播放器暂停或歌曲结束时没有被调用...... 2)当我单击按钮时我的乐器声音不会播放......
任何帮助都是有用的,谢谢。