4

わかりました、それで、まったく音がないエクササイズのビデオがたくさんあるアプリがあります。

また、演習の開始時に、ユーザーがオーディオ ファイルを選択できるように、デバイスのミュージック プレーヤーからすべての mp3 ファイルを表示します。

ただし、ビデオが開始されるたびに、音楽プレーヤーが一時停止します。Music Player が再生し続け、Video も同時に再生されるようにするにはどうすればよいですか。

ビデオの場合、クラスを使用 -- MPMoviePlayerViewController

次のように追加されます。

self.movieplayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:urlStr];
self.movieplayerController.moviePlayer.controlStyle =  MPMovieControlStyleFullscreen;
self.movieplayerController.moviePlayer.fullscreen = YES;
self.movieplayerController.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentModalViewController:movieplayerController animated:YES];

Music Player の場合、クラスは --- MPMusicPlayerController です。

オーディオは次のように選択され、再生されます。

 - (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
      [self dismissModalViewControllerAnimated: YES];

      [self.musicPlayer stop];

      [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];

      [self.musicPlayer play];
}

編集

また、ビデオを再生するために AVPlayer を試してみましたが、成功しませんでした!

コードは次のとおりです。

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:urlStr];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avPlayerLayer.frame = self.view.frame;
[self.view.layer addSublayer:avPlayerLayer];
[player play];
4

3 に答える 3

5

やっと答えが出た……。

AVPlayer で動作します。

上記の質問のように初期化します。

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:urlStr];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avPlayerLayer.frame = self.view.frame;
[self.view.layer addSublayer:avPlayerLayer];
[player play];

ただし、この前に、AppDelegate で、ミキシングを許可する AudioSession を作成します。

AudioSessionInitialize(NULL, NULL, NULL, NULL);  
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 allowMixWithOthers = true;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixWithOthers), &allowMixWithOthers);
AudioSessionSetActive(true);    

ここから答えを得ました。

AVPlayer を使用したアプリは、起動後に mp4 割り込み iPod の音楽を再生します

于 2012-09-17T12:22:07.900 に答える
1

AudioToolboxこの目的で使用できます。私は(あなたの質問を読んだ後)小さなデモとその動作を開発しました。

ここからデモをダウンロードしてMPMoviePlayerViewControllerください: - http://mobile.tutsplus.com/tutorials/iphone/mediaplayer-framework_mpmovieplayercontroller_ios4/

.wav次に、任意のファイルをバンドルに追加します。

このメソッドを変更します。

-(IBAction)playMovie:(id)sender
{
    UIButton *playButton = (UIButton *) sender; 

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"big-buck-bunny-clip" ofType:@"m4v"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];

    [moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x, 
                                                    playButton.frame.origin.y, 
                                                    playButton.frame.size.width, 
                                                    playButton.frame.size.height)];

    [self.view addSubview:moviePlayerController.view];
    //moviePlayerController.fullscreen = YES;

    //moviePlayerController.scalingMode = MPMovieScalingModeFill;

    [moviePlayerController.moviePlayer play];

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];

    SystemSoundID soundID;

    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);

    AudioServicesPlaySystemSound (soundID);

    [soundPath release];
}

それはただのデモです。それがさらに役立つことを願っています...

于 2012-09-17T09:34:00.067 に答える