问题
I'm trying to play a .m4a sound file using AVQueuePlayer, on iPhone4, but there is no sound. If I try AudioServicesPlaySystemSound with the same file, all works okay.
Here's the code.
(AVFoundation framework is installed.)
#import <AVFoundation/AVFoundation.h>
-(void) play_sound_file: (NSString *) sound_file_m4a
{
printf("\n play_sound_file 1: '%s' ", [sound_file_m4a UTF8String] );
AVPlayerItem *sound_file = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@"m4a"]]];
AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:sound_file, nil]];
[player play];
return;
}
LATEST CODE AS OF 8/28/12........... (Note: both sound files work with AudioServicesPlaySystemSound)
AVPlayerItem *sound_file1 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"Hold still"] ofType:@"m4a"]]];
AVPlayerItem *sound_file2 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"Press go"] ofType:@"m4a"]]];
AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems: [NSArray arrayWithObjects:sound_file1, sound_file2, nil]];
[player play];
回答1:
I explained how to fix this in a comment on my answer in your last post. One of the reasons this may not be working is because you are passing one item to arrayWithObjects:
which requires more than one item. Another thing that may be causing this not to work is if you are attempting to run this in the simulator, there are known compatibility issues between AVQueuePlayer
and the simulator.
Also:
Replace
AVPlayerItem *sound_file = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@"m4a"]]];
With
AVPlayerItem *sound_file = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"sound_file_m4a"] ofType:@"m4a"]]];
EDIT: Here's an example of how to play audio files sequentially.
In your header file:
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *data;
NSArray *arrayOfTracks;
NSUInteger currentTrackNumber;
Then in your implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
arrayOfTracks = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];//Do NOT include file extension here
currentTrackNumber = 0;
}
- (void)startPlaying
{
if (data) {
[data stop];
//[data release]; if project isn't using Automatic Reference Counting
data = nil;
}
data = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[arrayOfTracks objectAtIndex:currentTrackNumber]] ofType:@"m4a"]] error:NULL];
data.delegate = self;
[data play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag) {
if (currentTrackNumber < [arrayOfTracks count] - 1) {
currentTrackNumber ++;
if (data) {
[data stop];
//[data release]; if project isn't using Automatic Reference Counting
data = nil;
}
data = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[arrayOfTracks objectAtIndex:currentTrackNumber]] ofType:@"m4a"]] error:NULL];
data.delegate = self;
[data play];
}
}
}
来源:https://stackoverflow.com/questions/12160774/ios-xcode4-avqueueplayer-not-playing-sound