-->

如何在iOS应用中发挥的m3u音频流(How to play m3u audio stream in

2019-07-05 00:04发布

我试图创建一个使用的Xcode 4.5.2在iOS / iPhone应用程序电台。

我想流@“http://xx.xxxxxxx.com/8111/radio.m3u”有播放,暂停,音量控制,并且能够在后台功能/多任务播放。

我已经添加AVFoundationMediaplayerAudioToolBox迄今框架。 我已经添加了播放,暂停和滑块对象厦门国际银行。

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];
    }

Answer 1:

有实现这个双向的。

  1. 您可以直接加载你的URL中的UIWebView,它会正常。
  2. 您还可以使用的MPMoviePlayerController。


Answer 2:

创建“的MPMoviePlayerController *玩家”在您的ViewController一个强大的对象。

所以,你的代码看起来会象下面这样:

@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];
}


文章来源: How to play m3u audio stream in iOS app