0

以下代码播放用户音乐库中的歌曲。在运行 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-library://item/item.m4a?id=557492601628322780)

4

2 回答 2

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];
于 2013-06-24T20:43:11.120 回答
0

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

将音频加载到 NSData 并使用 AVAudioPlayer initWithData对我来说也失败了,出现了类似的错误(NSData 代码 256)。

到目前为止,我发现的最佳解决方案是使用 AVPlayer 而不是 AVAudioPlayer,但是由于方法和属性略有不同,因此需要进行一些返工。

于 2013-12-16T01:33:50.290 回答