1

我在 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];
4

2 回答 2

3

首先添加 AVFoundation.framework 并添加以下代码。

在 .h 文件中

#import <AVFoundation/AVFoundation.h>

和 .m 文件

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
于 2015-05-05T09:53:02.840 回答
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

本教程如何在 iPhone 中播放音频和视频

更新:

MPMoviePlayerViewController *Mpplayer =[[MPMoviePlayerViewController alloc] initWithContentURL: [url standardizedURL]];
Mpplayer.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;  
[self presentModalViewController:Mpplayer animated:YES];
[[Mpplayer moviePlayer] play];
于 2013-04-16T09:47:53.160 回答