简体   繁体   中英

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

I have mp3 files in a local dictionary. I tried to play these files using AVPlayer but it doesn't work.

.h file :

#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 file :

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];
}

In order to create NSURL for the locally stored files you have to use fileURLWithPath: method, currently you are trying to initialize url as a remote file. Replace NSURL *url = [NSURL URLWithString:myfilepath]; with NSURL *url = [NSURL fileURLWithPath:myfilepath]; and it should work. Good luck!

PS: Also, you have better to do some file existence check before passing the url to the player.

Am using the following code to stream the song. Instead of the audioURL please use the local file path. Please try this. It is working for me.

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

Hope it will help you. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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