菜鸟教程小白 发表于 2022-12-12 22:10:16

ios - MPNowPlayingInfoCenter 抛出 EXC_BAD_ACCESS


                                            <p><p>我正在制作一个播放音频的应用程序,我已经对其进行了设置,以便通过 <code>MPNowPlayingInfoCenter</code> 更新锁定屏幕,但我遇到了问题。</p>

<p>在尝试更新正在播放的信息时,有时会出现 <code>EXC_BAD_ACCESS</code> 错误。</p>

<p>这是执行此操作的代码:</p>

<pre><code>- (void)updatePlayback
{
    if(!active)
      return;

    NowPlayingController* npc = ;
    CMTime elapsed = player.currentTime;
    Float64 elInterval = CMTimeGetSeconds(elapsed);
    ;

    CMTime duration = player.currentItem.duration;
    Float64 durInterval = CMTimeGetSeconds(duration);
    ;

    ;
    if(durInterval &gt; 0)
    {
      ;
      ;
    }

    _activeMetadata = @(durInterval);
    _activeMetadata = @(isPlaying);
    _activeMetadata = @(elInterval);

    MPNowPlayingInfoCenter* npInfoCenter = ;
    if(npInfoCenter &amp;&amp; _activeMetadata)
    {
      if()
      {

//////////THE FOLLOWING LINE TRIGGERS EXC_BAD_ACCESS SOMETIMES////////////
            ;
      }

    }
}
</code></pre>

<p>99.9% 的情况下,这是可行的,但有时在将应用程序退出后台或更改音频文件时,或者只是随机地,</p>

<pre><code>;
</code></pre>

<p>抛出 <code>EXC_BAD_ACCESS</code>。</p>

<p>另外,<code>_activeMetadata</code> 被声明为:</p>

<pre><code>@property (atomic, strong, retain) NSMutableDictionary* activeMetadata;
</code></pre>

<p>在创建 AVPlayer 时实例化:</p>

<pre><code>    AVAsset* asset = ];
    AVPlayerItem* playerItem = ;
    player = ;

    CMTime duration = player.currentItem.duration;
    NSTimeInterval durInterval = CMTimeGetSeconds(duration);
    NSLog(@&#34;%f&#34;, durInterval);

    MPMediaItemArtwork* albumArtwork = [ initWithImage:]];
    NSDictionary* nowPlayingInfo = @{MPMediaItemPropertyTitle:ptString,
                                     MPMediaItemPropertyArtist:spString,
                                     MPMediaItemPropertyArtwork:albumArtwork,
                                     MPMediaItemPropertyAlbumTitle:info[@&#34;title&#34;],
                                     MPMediaItemPropertyPlaybackDuration:@(durInterval),
                                     MPNowPlayingInfoPropertyPlaybackRate:@(1),
                                     MPNowPlayingInfoPropertyElapsedPlaybackTime:@(0)};
    [ setNowPlayingInfo:nowPlayingInfo];

    _activeMetadata = ;
</code></pre>

<p><code>updatePlayback</code> 在每一帧通过 CADisplayLink 调用。</p>

<p>任何想法可能导致异常?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您过于频繁地调用 <code>setNowPlayingInfo</code>。当然,它确实不应该崩溃,但没有必要使用 <code>CADisplayLink</code> 每秒调用 60 次。</p>

<p>那你为什么这么频繁地调用它?如果是因为你想让进度条跟踪流畅,还是没有必要的。从 <code>MPNowPlayingInfoPropertyElapsedPlaybackTime</code> 声明:</p>

<pre><code>// The elapsed time of the now playing item, in seconds.
// Note the elapsed time will be automatically extrapolated from the previously
// provided elapsed time and playback rate, so updating this property frequently
// is not required (or recommended.)
</code></pre>

<p>附言我用 m4a 文件尝试了代码,发现 <code>durInterval</code> 是 NotANumber。使用正确的持续时间并仅调用一次 <code>setNowPlayingInfo</code>,进度条跟踪良好且没有崩溃。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - MPNowPlayingInfoCenter 抛出 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39555416/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39555416/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - MPNowPlayingInfoCenter 抛出 EXC_BAD_ACCESS