3

Xcode 4.5.2 を使用して iOS/iPhone ラジオ アプリを作成しようとしています。

@"http://xx.xxxxxxx.com/8111/radio.m3u" を再生、一時停止、ボリューム コントロール、バックグラウンド機能/マルチタスクで再生できるようにしたいと思っていました。

AVFoundationこれまでに、MediaplayerおよびAudioToolBoxフレームワークを追加しました。play、pause、slider オブジェクトを xib に追加しました。

ViewController.h

@interface ViewController : UIViewController

@property (strong,nonatomic) MPMoviePlayerController *myPlayer;

@property (weak, nonatomic) IBOutlet UISlider *myslider;

- (IBAction)playButtonPressed;

- (IBAction)myslider:(id)sender;

@end

ViewController.m
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()
{
    UISlider *volumeSlider;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}

- (IBAction)playButtonPressed;
{
    NSString *urlAddress = @"http://xxxxxxx.com/8111/listen.m3u";
    NSURL *url = [NSURL URLWithString:urlAddress];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
    player.movieSourceType = MPMovieSourceTypeStreaming;
    [player prepareToPlay];
    self.myPlayer = player;
    [self.view addSubview:self.myPlayer.view];
    [self.myPlayer play];
}

- (IBAction)stopButtonPressed;
{
    [self.myPlayer stop];
}
- (IBAction)myslider:(id)sender
{
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectMake(10, 10, 200, 40)];
    [volumeSlider addSubview:volumeView];
    [volumeView sizeToFit];
    }
4

2 に答える 2

1

これを達成するには2つの方法があります。

  1. URL を UIWebView に直接読み込むと、適切に読み込まれます。
  2. MPMoviePlayerController も使用できます。
于 2012-12-12T12:40:47.223 に答える
1

ViewController で強力なオブジェクトとして「MPMoviePlayerController *player」を作成します。

したがって、コードは次のようになります。

@interface ViewController ()
{
    UISlider *volumeSlider;
    MPMoviePlayerController *player;
}

@end


- (IBAction)playButtonPressed;
{
    NSString *urlAddress = @"http://xxxxxxx.com/8111/listen.m3u";
    NSURL *url = [NSURL URLWithString:urlAddress];

    if(nil != player)
    {
      player = nil; // Alternatively you can stop and restart with the different stream.
    }

    player = [[MPMoviePlayerController alloc]initWithContentURL:url];
    player.movieSourceType = MPMovieSourceTypeStreaming;
    [player prepareToPlay];
    self.myPlayer = player;
    [self.view addSubview:self.myPlayer.view];
    [self.myPlayer play];
}
于 2014-07-17T22:28:41.190 に答える