繁体   English   中英

使用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];

希望对您有帮助。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM