AVAudioPlayer在iOS5上不起作用

时间:2013-06-24 19:55:50

标签: ios avaudioplayer avaudiosession

以下代码播放用户音乐库中的歌曲。在运行iOS6的设备上运行正常,但在运行iOS5的设备上根本没有声音。我究竟做错了什么?在iOS5上搜索AVAudioPlayer问题的情况并不多。

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];


self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.songUrl error:nil];
[self.audioPlayer setNumberOfLoops:-1];
[self.audioPlayer play];

self.songUrl有效。 (iPod的库://item/item.m4a ID = 557492601628322780)

2 个答案:

答案 0 :(得分:0)

这是我在与iOS 5.0和iOS 6.0兼容的应用中实现的方式

In your *.h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>


@interface v1MusicPicController : UIViewController <MPMediaPickerControllerDelegate, AVAudioPlayerDelegate> 
{

    MPMusicPlayerController *musicPlayer;
    MPMediaItemCollection   *userMediaItemCollection;

}

@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
@property (nonatomic, retain) MPMediaItemCollection   *userMediaItemCollection;

--------- In your *.m file


@implementation v1MusicPicController

@synthesize musicPlayer;
@synthesize userMediaItemCollection;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self setMusicPlayer: [MPMusicPlayerController applicationMusicPlayer]];

    // By default, an application music player takes on the shuffle and repeat modes
    //      of the built-in iPod app. Here they are both turned off.
    [musicPlayer setShuffleMode: MPMusicShuffleModeOff];
    [musicPlayer setRepeatMode: MPMusicRepeatModeNone];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (IBAction) DoneButtonMusic:(id)sender
{
        MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
        mediaPicker.delegate = self;
        mediaPicker.allowsPickingMultipleItems = YES; // this is the default   
        [self presentModalViewController:mediaPicker animated:YES];


}

// To learn about notifications, see "Notifications" in Cocoa Fundamentals Guide.
- (void) registerForMediaPlayerNotifications {

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver: self
                           selector: @selector (handle_NowPlayingItemChanged:)
                               name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                             object: musicPlayer];

    [notificationCenter addObserver: self
                           selector: @selector (handle_PlaybackStateChanged:)
                               name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
                             object: musicPlayer];


    [musicPlayer beginGeneratingPlaybackNotifications];
}

- (void) updatePlayerQueueWithMediaCollection: (MPMediaItemCollection *) mediaItemCollection 
{                                   


    if (userMediaItemCollection == nil) 
    {

         //NSLog(@"Went here 4");

        // apply the new media item collection as a playback queue for the music player
        [self setUserMediaItemCollection: mediaItemCollection];
        [musicPlayer setQueueWithItemCollection: userMediaItemCollection];
        [musicPlayer play];

    }

}


// Media picker delegate methods
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 
{

    //NSLog(@"Went here 2");

    // We need to dismiss the picker
    [self dismissModalViewControllerAnimated:YES];

    // Apply the chosen songs to the music player's queue.
    [self updatePlayerQueueWithMediaCollection: mediaItemCollection];

}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker 
{
    // User did not select anything
    // We need to dismiss the picker
    [self dismissModalViewControllerAnimated:YES];
}

您可能已经知道这一点,但我使用AVAudioPlayer播放我的应用包中的声音。

@property (nonatomic, retain) AVAudioPlayer *myAudioPlayer1;
@synthesize myAudioPlayer1;

// ************************
        // PLAY AUDIO
        // ************************
        [myAudioPlayer1 stop];

        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"funny_sound" ofType: @"mp3"];
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];    
        myAudioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
        [myAudioPlayer1 play];

答案 1 :(得分:0)

我看到ipod-library URL的行为相同。更具体地说,initWithContentsOfURL设置错误代码-43,类似于未找到文件错误。显然,在iOS 6中,这已被修复,或者已添加支持,但我没有找到任何官方解释。

Loading the audio into an NSData and using AVAudioPlayer initWithData也因类似错误(NSData代码256)而失败。

到目前为止,我发现的最佳解决方案是use AVPlayer instead of AVAudioPlayer,但由于方法和属性略有不同,因此需要进行一些修改。