1

I am playing audio in iphone app but problem is that it works fine on simulator but it is not working on device here is my code it just closes the app when this code runs.

         NSString*thePath=[[NSBundle mainBundle] pathForResource:fileName ofType:@"MP3"];
    NSURL*url=[NSURL fileURLWithPath:thePath];



    //NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
    mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [[mp moviePlayer] prepareToPlay];
    [[mp moviePlayer] setUseApplicationAudioSession:NO];
    [[mp moviePlayer] setShouldAutoplay:YES];
    [[mp moviePlayer] setControlStyle:2];
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
    [self presentMoviePlayerViewControllerAnimated:mp];

Here is the video code this also works on simulator but not on iphone

         NSString*thePath=[[NSBundle mainBundle] pathForResource:fileName ofType:@"MP4"];
    NSURL*url=[NSURL fileURLWithPath:thePath];
    //NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp4"];
    mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [[mp moviePlayer] prepareToPlay];
    [[mp moviePlayer] setUseApplicationAudioSession:NO];
    [[mp moviePlayer] setShouldAutoplay:YES];
    [[mp moviePlayer] setControlStyle:2];
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
    [self presentMoviePlayerViewControllerAnimated:mp];

2 Answers2

3

First add AVFoundation.framework and add below code.

in .h file

#import <AVFoundation/AVFoundation.h>

and .m file

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
bLacK hoLE
  • 781
  • 1
  • 7
  • 20
0

try this code...

NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];

first check the file is stored in your project folder or not and also that variable is not nil.

Also Check Out this Link play-mp3-files-with-iphone-sdk

And this tutorial how-to-play-audio-and-video-in-iphone

UPDATE:

MPMoviePlayerViewController *Mpplayer =[[MPMoviePlayerViewController alloc] initWithContentURL: [url standardizedURL]];
Mpplayer.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;  
[self presentModalViewController:Mpplayer animated:YES];
[[Mpplayer moviePlayer] play];
Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • @JdeveloperIphone oh ok dude then see this link.. http://stackoverflow.com/questions/2058067/i-have-a-problem-with-mpmovieplayercontroller-iphone-sdk – Paras Joshi Apr 16 '13 at 09:54
  • @JdeveloperIphone also check this tutorial.. http://iphoneapp-dev.blogspot.in/2011/07/how-to-play-audio-and-video-in-iphone.html i hope this is helpful to you.. :) – Paras Joshi Apr 16 '13 at 09:56
  • same code if i am using any server url file then works fine on device this same problem also with video – Jdeveloper Iphone Apr 16 '13 at 09:56
  • don't know what is the reason for not playing on device it works fine on simulator i have tried all – Jdeveloper Iphone Apr 16 '13 at 10:09
  • hey check the internet connection of device and some setting about that.. check that... bcoz its working perfact in my project now.. – Paras Joshi Apr 16 '13 at 10:11