如何在iOS应用中发挥的m3u音频流 [英] How to play m3u audio stream in iOS app
问题描述
我试图用X code 4.5.2创建的iOS / iPhone应用程序的无线电
我想流@http://xx.xxxxxxx.com/8111/radio.m3u有播放,暂停,音量控制,并能够在后台功能/多任务播放。
我已经添加了 AVFoundation
,媒体播放器
和 AudioToolBox
框架迄今。我已经添加了播放,暂停和滑块对象厦门国际银行。
ViewController.h@interface的ViewController:UIViewController中@property(强,非原子)的MPMoviePlayerController * myPlayer;@property(弱,非原子)IBOutlet中UISlider * myslider; - (IBAction为)为playButton pressed; - (IBAction为)myslider:(ID)发送;@结束ViewController.m
#进口ViewController.h
#进口<的MediaPlayer / MediaPlayer.h>@interface的ViewController()
{
UISlider * volumeSlider;
}@结束@implementation的ViewController - (无效)viewDidLoad中
{
[超级viewDidLoad中];
[UIApplication的sharedApplication] beginReceivingRemoteControlEvents]
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication的sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
} - (IBAction为)为playButton pressed;
{
* NSString的urlAddress = @http://xxxxxxx.com/8111/listen.m3u
NSURL * URL = [NSURL URLWithString:urlAddress];
*的MPMoviePlayerController播放器= [[的MPMoviePlayerController页头] initWithContentURL:URL];
player.movieSourceType = MPMovieSourceTypeStreaming;
[播放器prepareToPlay]。
self.myPlayer =玩家;
[self.view addSubview:self.myPlayer.view];
[self.myPlayer播放];
} - (IBAction为)STOPBUTTON pressed;
{
[self.myPlayer站]
}
- (IBAction为)myslider:(ID)发送者
{
MPVolumeView * volumeView = [[MPVolumeView的alloc] initWithFrame:方法CGRectMake(10,10,200,40)];
[volumeSlider addSubview:volumeView];
[volumeView sizeToFit]
}
创建的MPMoviePlayerController *玩家作为您的ViewController一个强大的对象。
所以你code将类似于如下:
@interface ViewController中()
{
UISlider * volumeSlider;
*的MPMoviePlayerController播放器;
}@结束
- (IBAction为)为playButton pressed;
{
* NSString的urlAddress = @http://xxxxxxx.com/8111/listen.m3u
NSURL * URL = [NSURL URLWithString:urlAddress]; 如果(零!=玩家)
{
玩家=零; //或者你可以停止,并与不同的流重新启动。
} 玩家= [[的MPMoviePlayerController页头] initWithContentURL:URL];
player.movieSourceType = MPMovieSourceTypeStreaming;
[播放器prepareToPlay]。
self.myPlayer =玩家;
[self.view addSubview:self.myPlayer.view];
[self.myPlayer播放];
}
I'm trying to create an iOS/iPhone radio app using Xcode 4.5.2.
I wanted to stream @"http://xx.xxxxxxx.com/8111/radio.m3u" with play, pause, volume control and able to play on background feature/multitasking.
I've added AVFoundation
, Mediaplayer
and AudioToolBox
frameworks thus far. I've added play, pause and slider objects to 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];
}
Create a "MPMoviePlayerController *player" as a strong object in your ViewController.
So you code would look something like below:
@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];
}
这篇关于如何在iOS应用中发挥的m3u音频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!