2012-12-12 2 views
3

Xcode 4.5.2를 사용하여 iOS/iPhone 라디오 앱을 만들려고합니다. iOS 앱에서 m3u 오디오 스트림을 재생하는 방법

나는 스트리밍 싶었 @ "http://xx.xxxxxxx.com/8111/radio.m3u"재생, 일시 정지, 볼륨 조절 및 배경 기능/멀티 태스킹에 재생할 수와 함께.

여기까지 AVFoundation, MediaplayerAudioToolBox 프레임 워크를 추가했습니다. 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]; 
    } 
+0

어떤 일이 잘못되고있는 것이 무엇인지에 대한 위의 코드와 저를 도와 주시겠습니까? 감사합니다. – user1896794

답변

1

두 가지 방법이 있습니다.

  1. URL을 UIWebView에서 직접로드 할 수 있으며 제대로 작동합니다.
  2. MPMoviePlayerController를 사용할 수도 있습니다.
+0

고맙습니다. 위의 코드에서 잘못된 점을 알려주십시오. – user1896794

+0

같은 질문이지만 Watch App에서 제발, 제발? 어떤 예가 있습니까? – Markus

1

"MPMoviePlayerController * player"를 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]; 
} 
관련 문제