简体   繁体   中英

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.

我相信您需要创建播放wav文件的队列,以便可以按顺序进行管理,否则它们会相互覆盖,委托集也放在哪里?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM