Search code examples
iosobjective-cmpmovieplayercontroller

Playing mp3 and mp4 file using MPMoviePlayerController


I have mp3 and mp4 dat in url. I tried to play that in my ViewController so, i have created two players

audioPlayer = [[MPMoviePlayerController alloc]init];
videoPlayer  = [[MPMoviePlayerController alloc]init];

and assigned those links to the players -

Video

videoPlayer.contentURL = [NSURL URLWithString:video];
[videoPlayer prepareToPlay];

Audio

audioPlayer.contentURL = [NSURL URLWithString:audio];
[audioPlayer prepareToPlay];

Here i'm facing one issue that i can able to play one of them from above. It won't play either one is stopped after playing or initially its not allow to play both of them.

How do i fix this.


Solution

  • hi Praveen you have to use AVAudioPlayer for this type if task. please check bellow reference of :

    http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

    and you can play your mp3 file using bellow code and import AVFoundation.framework:-

        NSString *strURlString =@"yoururlwith.mp3"
    
        NSLog(@"%@",strURlString);
    
        strURlString =[strURlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    
        NSLog(@"img url ==%@",strURlString);
    
        NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURlString]];
        NSError *error;
    
        audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
        audioPlayer.numberOfLoops = 0;
        audioPlayer.volume = 1.0f;
        [audioPlayer prepareToPlay];
    
        if (audioPlayer == nil)
            NSLog(@"%@", [error description]);
        else
            [audioPlayer play];