我正在iphone应用程序中播放音频,但问题是它在模拟器上可以正常运行,但在设备上不起作用,这是我的代码,当该代码运行时它只是关闭了应用程序。
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];
这是视频代码,它也可以在模拟器上使用,但不能在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];
最佳答案
试试这个代码...
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
首先检查文件是否存储在项目文件夹中,并且该变量不是
nil
。同时查看此链接play-mp3-files-with-iphone-sdk
而本教程how-to-play-audio-and-video-in-iphone
更新:
MPMoviePlayerViewController *Mpplayer =[[MPMoviePlayerViewController alloc] initWithContentURL: [url standardizedURL]];
Mpplayer.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
[self presentModalViewController:Mpplayer animated:YES];
[[Mpplayer moviePlayer] play];
关于iphone - 音频文件无法在设备上运行,但可以在模拟器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16033723/