0

私はObj-cの初心者で、簡単なiPhoneミュージックアプリを作ろうとしています。基本的に、いくつかの異なるボタンを押すと、いくつかの異なるサウンドが再生されます。 applicationMusicPlayerを使用して、iPodライブラリから曲を再生することができます。ここに私のコードです:Obj-C iPhone Music Appヘルプ

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、メモリを管理するためにNSMutableDictionaryを使用していますどのように私はそれをやっているかを示すためにボタンの音のメソッドの1つだけを投稿しました。

問題が 1)handle_PlaybackStateChangedある:(ID)通知は、私はボタンをクリックしたときに 2)私の楽器の音が再生されません...プレーヤーが一時停止されたときに呼び出されるか、曲が終了していません...

ご協力いただきありがとうございます。

答えて

0

私は再生するwavファイルのキューを作成する必要があると思うので、順番に管理することができます。そうでなければ、オーバーライドします。