2013-06-24 2 views
0

다음 코드는 사용자의 음악 라이브러리에서 노래를 재생합니다. iOS6을 실행하는 기기에서 정상적으로 작동하지만 iOS5를 실행하는 기기에서는 전혀 소리가 들리지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? iOS5에서 AVAudioPlayer 문제를 검색해도 많은 도움이되지 않습니다.AVAudioPlayer가 iOS5에서 작동하지 않습니다.

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이 유효합니다. (아이팟 라이브러리 : //item/item.m4a ID는 = 557492601628322780)

+0

downvote에 감사드립니다. 매우 도움이됩니다. – soleil

답변

0

이 내가 아이폰 OS 5.0과 아이폰 OS 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]; 
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이지만, 방법과 속성이 약간 다르기 때문에 약간의 재 작업이 필요합니다.