【问题标题】:Obj-C iPhone Music App HelpObj-C iPhone 音乐应用帮助
【发布时间】:2011-03-06 15:41:13
【问题描述】:

我是一个 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)点击按钮时我的乐器声音不会播放...

任何帮助都是有用的,谢谢。

【问题讨论】:

    标签: iphone objective-c avaudioplayer mpmusicplayercontroller


    【解决方案1】:

    我相信你需要创建一个播放wav文件的队列,这样你就可以按顺序管理,否则它们会相互覆盖,你的委托设置在哪里?

    【讨论】:

      猜你喜欢
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多