WebViewでYoutubeページの動画再生

AndroidManifest.xmlandroid:hardwareAcceleratedを足して、

    <application
        android:hardwareAccelerated="true"

WebViewのSettingでsetJavaScriptEnabledをtrue、setPluginStateをONに。 何もすることがなくても、setWebChromeClientをしないと再生されません。 ここハマりポイントでした。

WebView webView = (WebView) view.findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.setWebViewClient(new WebViewClient() {

    public void onPageFinished(WebView view, String url) {
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("http://m.youtube.com/watch?v=hogehoge");

以上!

自作したViewのClassをxmlで使う

AndroidでScrollViewなんかの子クラスをつくって、それをlayoutのxmlから呼ぼうとしたところ少しはまったのでメモ。

ポイントはコンストラクタの引数の取り方で、ContextとAttributeSetを指定して親を呼ばないと

Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class com.example.MyHorizontalScrollView

などとエラーを出して落ちます。

例としてはこんな感じ

public class MyHorizontalScrollView extends HorizontalScrollView {

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }
}

でした。

さくらクラウドのネットワーク設定

AWSのようにサーバを立ちあげたら自動的にネットワークにつながらないので、ネットワークに手動でつなげます。

最初はping投げても返ってこない

$ ping google.com
ping: unknown host google.com

なので2ファイルを修正してネットワークにつなぎます。 該当修正箇所だけ。

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

# 固定IP
BOOTPROTO=static
# システム起動時にこのデバイスを起動
ONBOOT=yes
# ネットマスクの指定
NETMASK=255.255.255.0
# コンソール画面(NIC)からIPとゲートウェイの情報を指定
IPADDR=133.242.xx.xx
GATEWAY=133.242.xx.1
# vi /etc/resolv.conf

# NICの画面にある推奨ネームサーバを指定
nameserver 133.242.0.3
nameserver 133.242.0.4
search localdomain

修正終わったら再起動

# /etc/init.d/network restart
$ ping google.com
PING google.com (173.194.38.78) 56(84) bytes of data.

できましたおけ

podspecからlocalにレポジトリを作成して使う

cocoapodsにあるTapkuLibraryが最新に比べてちょっと古かったので、githubのpodspecからlocalにレポジトリをつくってインストールしてみる。

githubからclone
$ git clone git://github.com/devinross/tapkulibrary.git
localにディレクトリをつくって、podspecファイルをコピーする

0.2.3がカレントだったので、仮にバージョンを0.2.4としています。

$ mkdir -p  ~/.cocoapods/local/TapkuLibrary/0.2.4/
$ cp tapkulibrary/TapkuLibrary.podspec ~/.cocoapods/local/TapkuLibrary/0.2.4/
podspecのsourceを修正

githubのmasterブランチを使いたいので、sourceの箇所を変更する

$ vim ~/.cocoapods/local/TapkuLibrary/0.2.4/TapkuLibrary.podspec
s.source        = { :git => 'https://github.com/devinross/tapkulibrary.git'}
確認する
$ pod search TapkuLibrary
-> TapkuLibrary (0.2.4)
   tap + haiku = tapku, a well crafted open source iOS framework
   - Homepage: https://github.com/devinross/tapkulibrary
   - Source:   https://github.com/devinross/tapkulibrary.git
   - Versions: 0.2.4 [local repo]
Podfileを修正

ここまでできたらPodfileを書き換えてupdateすればOK

$ vim Podfile
pod 'TapkuLibrary', '~> 0.2.4'
updateかける
$ pod update TapkuLibrary

iOS、Mac用の短縮URLライブラリ

https://github.com/theaudience/URL-Shortener

goo.gl、bit.lyなど選べてblockで短縮URL得られるので便利

UrlShortener *shortener = [[UrlShortener alloc] init];
[shortener shortenUrl:landingPage
          withService:UrlShortenerServiceGoogle
           completion:^(NSString *shortUrl) {
               
               // complete
               
           } error:^(NSError *error) {
               // Handle the error.
               NSLog(@"Error: %@", [error localizedDescription]);
           }];