使用 AVPlayer 无法在 iOS 9 中播放 mp3 文件

playing a mp3 file in iOS 9 is not working by using AVPlayer

我在本地字典中有 mp3 文件。我尝试使用 AVPlayer 播放这些文件,但它不起作用。

.h 文件:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreMedia/CoreMedia.h>
#import <AudioToolbox/AudioToolbox.h>

@interface IndusHomePageViewController : UIViewController 
{
     AVPlayer *player;
     NSTimer *playbackTimer;
}
-(void) setupAVPlayerForURL: (NSURL*) url;
@end

.m 文件:

NSString *docDir1 =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataPathDestination = [docDir1 stringByAppendingPathComponent:@"mysore"];
NSString *myfilepath = [dataPathDestination stringByAppendingPathComponent:@"en_01_Mysore.mp3"];
NSURL *url = [NSURL URLWithString:myfilepath];
[self setupAVPlayerForURL:url];

-(void) setupAVPlayerForURL: (NSURL*) url  
{
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
    player = [AVPlayer playerWithPlayerItem:anItem];
    [player addObserver:self forKeyPath:@"status" options:0 context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{    
    if (object == player && [keyPath isEqualToString:@"status"]) {
        if (player.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayer Failed");
        } else if (player.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayer Ready to Play");
        } else if (player.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");
        }
    }
}

- (IBAction)play:(id)sender {
    [player play]; 
}

- (IBAction)pause:(id)sender {
    [player pause];
}

为了为本地存储的文件创建 NSURL,您必须使用 fileURLWithPath: 方法,目前您正在尝试将 url 初始化为远程文件。将 NSURL *url = [NSURL URLWithString:myfilepath]; 替换为 NSURL *url = [NSURL fileURLWithPath:myfilepath];,它应该可以工作。祝你好运!

PS:此外,在将 url 传递给播放器之前,您最好做一些文件存在性检查。

我正在使用以下代码播放歌曲。请使用本地文件路径代替 audioURL。请试试这个。它对我有用。

NSURL *url = [NSURL URLWithString:[audioURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
AVURLAsset *asset = [AVURLAsset assetWithURL: url]; 
// self.aduioItem is an obj of AVPlayerItem 
self.audioItem  = [AVPlayerItem playerItemWithAsset: asset]; 
// self.audioPlayer is an obj of AVPlayer 
self.audioPlayer = [[AVPlayer alloc] init]; 
[self.audioPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; 
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem: self.audioItem]; self.audioPlayer = player; 
//Calling a methods to handle the current song completion.. 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.audioPlayer currentItem]];
//Calling a method to update the slider timer.. 
self.songTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatePlayerTime:) userInfo:nil repeats:YES]; [self.audioPlayer play];

希望对您有所帮助。谢谢