簡體   English   中英

使用帶有Objective-C的libmms

[英]Using libmms with Objective-C

我一直在互聯網上尋找有關如何使用libmms的教程或示例的幾天。 似乎沒有,這對於似乎被廣泛使用的lib來說很奇怪。

LibMMS是一個用於解析mms://和mmsh://類型網絡流的公共庫。 http://sourceforge.net/projects/libmms/files/libmms/0.6.2/libmms-0.6.2.tar.gz/download

我找到的唯一代碼示例來自stackoverflow上的另一篇文章。

這將在下面顯示。

mms_connect(NULL, NULL, g_tcUrl.av_val, g_hostname.av_val, g_playpath.av_val, "", g_port, 128*1024)

注意:

NSString* strTemp;      
strTemp = @"mms://123.30.49.85/htv2";
// strTemp = @"mms://212.58.251.92/wms/bbc_ami/radio1/radio1_bb_live_int_eq1_sl0";
g_tcUrl.av_val = new char[[strTemp length] + 1];
[strTemp getCString:g_tcUrl.av_val
          maxLength:([strTemp length]+1)
           encoding:NSUTF8StringEncoding];
g_tcUrl.av_len = strlen(g_tcUrl.av_val);
//strTemp = @"212.58.251.92";
strTemp = @"123.30.49.85";
g_hostname.av_val = new char[[strTemp length]+1];       
[strTemp getCString:g_hostname.av_val
          maxLength:([strTemp length]+1)
           encoding:NSUTF8StringEncoding];
g_hostname.av_len = strlen(g_hostname.av_val);
//strTemp = @"/wms/bbc_ami/radio1/radio1_bb_live_int_eq1_sl0";
strTemp = @"/htv2";
g_playpath.av_val = new char[[strTemp length] + 1];         
[strTemp getCString:g_playpath.av_val
          maxLength:([strTemp length]+1)
           encoding:NSUTF8StringEncoding];
g_playpath.av_len = strlen(g_playpath.av_val);
g_port = 1755; 

這不是客觀的C,但確實顯示了需要傳遞給mms_connect方法的內容。

所以我創建了一個新項目,包括所有需要的libmms文件並構建它。 編譯好了。

下一步是包括

#import "mms.h"
#import "mms_config.h"

並宣布

mms_t *mms = 0;

到目前為止沒問題。

我想嘗試的下一件事是調用mms_connect方法,這就是我遇到的問題。

我不是C程序員,所以這看起來可能是FUBAR,但這是我最好的嘗試。 我不能用

char *g_tcUrl = new char[[strTemp length] + 1];

因為在這里使用的方式不能在目標c中識別新的。 在Objective-C中我應該用什么來達到同樣的效果?

mms_t *mms = 0;

NSString* strTemp;      
strTemp = @"mms://123.30.49.85/htv2";

char *g_tcUrl = new char[[strTemp length] + 1];
[strTemp getCString:g_tcUrl maxLength:([strTemp length]+1) 
encoding:NSUTF8StringEncoding];

strTemp = @"123.30.49.85";
char *g_hostname = new char[[strTemp length]+1];       
[strTemp getCString:g_hostname maxLength:([strTemp length]+1) 
encoding:NSUTF8StringEncoding];

strTemp = @"/htv2";
char * g_playpath = new char[[strTemp length] + 1];         
[strTemp getCString:g_playpath maxLength:([strTemp length]+1) 
encoding:NSUTF8StringEncoding];
int g_port = 1755;

//mms = mms_connect(mms_io_t *io, void *data, const char *url, const char *host, 
const char *uri, const char *query, int port, int bandwidth);
mms = mms_connect(NULL, NULL, g_tcUrl, g_hostname, g_playpath, "", g_port, 
128*1024);

現在我想在Objective-c文件中混合使用C代碼。 當我嘗試測試並弄清楚如何使用libmms時,該代碼全部在我的viewDidLoad中。

伙計們,我非常感謝所有建議和幫助,我可以在我的應用程序中使用libmms。

-碼

您可以在同一個文件中混合使用C ++和Objective-C。 要么為文件提供.mm擴展名,要么將文件類型更改為sourcecode.cpp.objcpp。

我能想到的最簡單的版本:

// wherever these NSStrings come from in the app, we can use them
NSString *mmsURL = @"mms://123.30.49.85/htv2",
         *mmsHostname = @"123.30.49.85",
         *mmsPath = @"/htv2";
NSInteger port = 1755;

mms_t *mms = mms_connect(NULL, NULL,
                         [mmsURL cStringUsingEncoding:NSASCIIStringEncoding],
                         [mmsHostname cStringUsingEncoding:NSASCIIStringEncoding],
                         [mmsPath cStringUsingEncoding:NSASCIIStringEncoding],
                         "", port, 128*1024);

要以相反的方式移動數據(如果要將流數據傳遞到基於AVPlayer的自定義電影播放器​​或類似內容):

char *myCString = "some data coming from libmms";
NSString *myStringFromACString = 
[NSString stringWithCString:myCString
              usingEncoding:NSASCIIStringEncoding];

你讓事情變得過於復雜。 你不必在這里使用新的。

mms_t *mms = 0;
const char* g_tcUrl = "mms://123.30.49.85/htv2"; // Easy C String,
                                                 // No need for heap allocation.
const char* g_hostname = "123.30.49.85";       
const char* g_playpath = "/htv2";         

int g_port = 1755;

//mms = mms_connect(mms_io_t *io, void *data, const char *url, const char *host, const char *uri, const char *query, int port, int bandwidth);
mms = mms_connect(NULL, NULL, g_tcUrl, g_hostname, g_playpath, "", g_port, 
128*1024);

新的不是C的一部分。它是C ++的一部分,在C中你必須使用malloc在堆上分配內存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM