我正在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];
答案 0 :(得分:3)
首先添加AVFoundation.framework并添加以下代码。
<。>文件中的
#import <AVFoundation/AVFoundation.h>
和.m文件
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
答案 1 :(得分:0)
试试这段代码......
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];