【问题标题】:MPMoviePlayerController want video only, no audioMPMoviePlayerController 只想要视频,没有音频
【发布时间】:2013-11-19 09:32:19
【问题描述】:

我在我的应用中使用视频来制作一些教程屏幕。视频没有音轨。我可以正常工作,除非用户已经在收听音频,例如来自音乐应用程序,当其中一个教程视频开始时音频会停止。

有没有办法只播放视频而不影响音频,以便用户可以继续欣赏他们播放的音频?

我尝试在[self.player play] 之前设置[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];,但这不起作用。 useApplicationAudioSession 看起来很有希望,但它在 iOS 6 中已被弃用。我也尝试过 AVPlayer 并在 play 之前再次设置 AVAudioSessionCategoryAmbient 但没有运气。

【问题讨论】:

  • 尝试在application:didFinishLaunchingWithOptions:中设置[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];并尝试在AVPlayerAVPreviewLayer中播放视频。希望这会有所帮助。
  • [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; 在 App Delegate 中有效,即使使用 MPMoviePlayerController!奇怪的是,在play 之前调用它时不起作用。如果您想将此作为答案,我会将其标记为正确。
  • 我不确定,但我认为您应该在初始化电影播放器​​之前更改音频会话类别。

标签: ios cocoa-touch mpmovieplayercontroller avplayer


【解决方案1】:

尝试在application:didFinishLaunchingWithOptions:中设置[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];并尝试将AVPlayerAVPreviewLayer一起播放视频。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我不确定这正是您要问的,但我在重新阅读问题之前已经写下了答案,它可能对看这里的人有用……


    您无法将MPMoviePlayerController 上的音频静音。但是,您可以切换到使用AVPlayer,并按如下方式删除音频:

    // Load the video asset
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL: <your-video-URL> options:nil];
    NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
    
    // Mute all the audio tracks
    NSMutableArray *allAudioParams = [NSMutableArray array];
    for (AVAssetTrack *track in audioTracks) {
        AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
        [audioInputParams setVolume:0.0 atTime:kCMTimeZero];
        [audioInputParams setTrackID:[track trackID]];
        [allAudioParams addObject:audioInputParams];
    }
    AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
    [audioZeroMix setInputParameters:allAudioParams];
    
    // Create a player item
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
    [playerItem setAudioMix:audioZeroMix]; // Mute the player item
    
     // Create an AVPlayer
     AVPlayer * moviePlayer = [AVPlayer playerWithPlayerItem: playerItem]
    

    然后您需要将AVPlayerplayerLayer 添加到您的视图控制器的视图层次结构中。

    AVPlayer 还可以让您更好地控制播放(例如,更精细的搜索)。

    【讨论】:

    • 投反对票有什么理由吗?如何改进答案?
    • 嗨,反对票是我的。我错误地单击了向下箭头,现在除非您编辑答案,否则我无法将其删除:You last voted on this answer Jun 6 at 18:32. Your vote is now locked in unless this answer is edited.
    【解决方案3】:
    [videoPlayer moviePlayer].useApplicationAudioSession = YES;
    [[MPMusicPlayerController applicationMusicPlayer] setVolume:0.0];
    

    【讨论】:

    • useApplicationAudioSession 已弃用
    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    相关资源
    最近更新 更多