【问题标题】:AVAudioPlayer doesn't work on iOS5AVAudioPlayer 在 iOS5 上不起作用
【发布时间】:2013-06-24 19:55:49
【问题描述】:

以下代码播放用户音乐库中的歌曲。在运行 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)

【问题讨论】:

  • 感谢您的反对。很有帮助。

标签: ios avaudioplayer avaudiosession


【解决方案1】:

这就是我在兼容 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];

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 2011-12-22
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多