如何将 MPMusicPlayerController 与 AVAudioPlayer 一起使用

发布于 2024-09-17 19:22:31 字数 1540 浏览 1 评论 0原文

我正在尝试使用 ipod 播放应用内音频,我用来播放音频文件的代码是:

代码:

       [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
       UInt32 doSetProperty = 1;
       AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers,  sizeof(doSetProperty), &doSetProperty);
       [[AVAudioSession sharedInstance] setActive: YES error: nil];

       if(error)
       {
           NSLog(@"Some error happened");
       }

    NSString *path = [[NSBundle mainBundle] pathForResource:effect ofType:type];
    myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    myPlayer.delegate = self;
    myPlayer.numberOfLoops = -2;
    myPlayer.volume = 1.0f;
    [myPlayer play];

并播放我正在使用的 ipod 音乐

代码:

player = [MPMusicPlayerController iPodMusicPlayer];

我的 ipod 播放正常,直到音频开始播放,但一旦音频停止我无法回到 ipod 上玩。我正在使用

Code:

NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"sound"];

if([value compare:@"ON"] == NSOrderedSame && myPlayer != nil)
{
    [myPlayer stop];
    [myPlayer release];
    myPlayer = nil;
}

NSError *error = nil;

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];

if(error)
{
    NSLog(@"Some error happened");
}

停止音频,然后调用

Code:

 [player play];

播放 iPod 音乐,但它不播放音乐,它说标题为空。

如果有人能帮助我解决这个问题,我将非常感激。

问候, 安库尔

I am trying to play an in app audio with ipod the code I am using to play the audio file is:

Code:

       [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
       UInt32 doSetProperty = 1;
       AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers,  sizeof(doSetProperty), &doSetProperty);
       [[AVAudioSession sharedInstance] setActive: YES error: nil];

       if(error)
       {
           NSLog(@"Some error happened");
       }

    NSString *path = [[NSBundle mainBundle] pathForResource:effect ofType:type];
    myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    myPlayer.delegate = self;
    myPlayer.numberOfLoops = -2;
    myPlayer.volume = 1.0f;
    [myPlayer play];

and to play the ipod music I am using

Code:

player = [MPMusicPlayerController iPodMusicPlayer];

My ipod plays fine until the audio starts playing but once the audio stops I am not able to get back to the ipod to play. I am using

Code:

NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"sound"];

if([value compare:@"ON"] == NSOrderedSame && myPlayer != nil)
{
    [myPlayer stop];
    [myPlayer release];
    myPlayer = nil;
}

NSError *error = nil;

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];

if(error)
{
    NSLog(@"Some error happened");
}

to stop the audio and then just call

Code:

 [player play];

to play the ipod music but it does not play the music, it says the title to be null.

I would really appreciate if some one could help me with this.

Regards,
Ankur

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-09-24 19:22:31

我建议将音频会话设置(包括 Ambient 类别)移至应用程序委托的 didFinishLaunchingWithOptions,并且根本不使用 Playback 类别。您仍然可以通过环境设置很好地播放 AVAudio 声音。

如果您想在播放 AV 声音时停止 iPod 音乐,那么您应该只使用“播放”类别。 (我不太确定您要做什么。)

当您尝试恢复 iPod 音乐时出现的错误表明您的 MPMusicPlayerController 对象在被 AVAudio 声音停止时重置了其歌曲队列,并且您必须重新设置 MP 播放器。 “标题为空”听起来绝对像 MP 播放器中的悬空指针或类似内容。

I suggest moving the audio session setup (including the Ambient category) to your application delegate's didFinishLaunchingWithOptions and not using the Playback category at all. You can still play AVAudio sounds just fine with the Ambient setting.

If you want to kill iPod music whenever you play AV sounds, then you should only be using the Playback category. (I'm not exactly sure what you're trying to do.)

The error that you're getting when you try to resume the iPod music suggests that your MPMusicPlayerController object resets its song queue when it is stopped by the AVAudio sounds, and that you'll have to set up the MP player again. "Title is null" definitely sounds like a dangling pointer or the like in the MP player.

倾城泪 2024-09-24 19:22:31

无法在 AVAudioPlayer 中播放 iPod 音频。您需要使用 *AVURLAsset 将 iPod 中的库资源链接转换为 mp3 或 wav 并将其保存到文档库中。 IOS.5 目前在从 ipod 到 mp3 文件的转换 (AudioExportSession) 中存在错误,因此这是不可能的。对不起。

It's not possible to play iPod audio in AVAudioPlayer. You need to convert the library Asset link from ipod, using *AVURLAsset to an mp3 or wav and save it to the documents library. IOS.5 currently has a bug in the conversion (AudioExportSession) from ipod to mp3 file, so it's NOT POSSIBLE. Sorry.

倦话 2024-09-24 19:22:31

myPlayer 完成播放后,您需要恢复 iPod 播放器。

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player play];
}

You need to resume iPod player after myPlayer finish playing.

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player play];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文