1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【mBaaS】ファイルストアから動画を取得して再生する【Android】

Posted at

はじめに

ニフティクラウド mobile backend (mBaaS) の「ファイルストア」機能を使っていて、テキスト以外に動画なんかも使えるのか試しました。
mBaaSと記述していますが、ファイルの取得部分以外は普通のAndroidの機能です。

動作確認環境

Android 5系
NCMB Android SDK v2.2.4

事前準備

  • mBaaSはプロジェクトにSDKが入っている状態です。詳しくはこちら
  • ファイルストアにMP4ファイルをアップロード済み(xxxx.mp4)

実装

流れとしては、Activityを開いたらmBaaSから動画を取得、
成功したらローカルでvideoviewで再生するというシンプルなもの。

VideoActivity.java
public class VideoActivity extends AppCompatActivity {

    // VideoView
    private VideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        mVideoView = (VideoView) findViewById(R.id.video);
        // メディアコントローラーの設定
        mVideoView.setMediaController(new MediaController(this));
        // 動画の準備ができたら再生
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mVideoView.start();
            }
        });

        // mBaaSのファイルストアから動画を取得
        getVideo();
    }

    private void getVideo(){
        // 動画ファイル名
        final String videoName = "xxxx.mp4";

        // ファイルストアから動画を取得
        NCMBFile file = new NCMBFile(videoName);
        file.fetchInBackground(new FetchFileCallback() {
            @Override
            public void done(byte[] data, NCMBException e) {
                if (e != null) {
                    // 失敗
                    e.printStackTrace();
                } else {
                    // アプリ内のfilesに保存
                    FileOutputStream out = null;
                    try {
                        out = openFileOutput(videoName, Context.MODE_PRIVATE);
                        out.write(data);
                        out.close();
                    } catch (IOException e1) {
                        e.printStackTrace();
                    }

                    // 動画のパスを設定
                    mVideoView.setVideoPath("/data/data/" + getPackageName() + "/files/" + videoName);
                }
            }
        });
    }
}
activity_video.xml
<VideoView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/video"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VideoActivity"
    />

結論

特に問題ないです。
ただ動画となるとサイズが大きいので、取得するタイミングや取得中の表示はちゃんと考えないといけなそうです。
他に追加してできそうなこと…

  • アプリ側からmBaaSに動画を保存したり公開する
  • パーミッションを設定して特定のユーザーだけに限定公開する
  • プッシュ通知タップ時にペイロード情報から動画を取得・視聴させる など

普通にダウンロードするだけなら公開ファイル設定にしてURLからとってきた方が楽かもしれないですね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?