tag:crieit.net,2005:https://crieit.net/boards/syncam-project/feed 「React Nativeでカメラ同期アプリを作る」の投稿 - Crieit Crieitで「React Nativeでカメラ同期アプリを作る」ボードに投稿された最近の投稿 2019-03-09T22:30:42+09:00 https://crieit.net/boards/syncam-project/feed tag:crieit.net,2005:PublicArticle/Day1 2019-03-09T22:30:42+09:00 2019-03-09T22:30:42+09:00 https://crieit.net/boards/syncam-project/Day1 Day1 カメラアプリを作る <p>ボードのゴールには色々書いてますが、まずはカメラアプリを作っていきます。(Day1と書いてますが2日間かかりました)</p> <h1>パーミッションの取得</h1> <p>今回は以下3つの権限が必要。<br /> - カメラ<br /> - 音声録音<br /> - 外部ストレージへの書き込み</p> <pre><code class="javascript">componentWillMount(){ (async()=>{ await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.CAMERA) await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.RECORD_AUDIO) await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE) })(); } </code></pre> <p>とりあえず全部投げておけばええやろ!的な実装なのであまり真似しないでください(戻り値をちゃんと評価しましょう)</p> <h1>動画の撮影処理</h1> <p>参考リンク:<a target="_blank" rel="nofollow noopener" href="https://medium.com/react-native-training/uploading-videos-from-react-native-c79f520b9ae1">Record and Upload Videos with React Native</a></p> <h1>ストレージへの保存処理</h1> <h2>カメラロールに保存する処理</h2> <p>ファイル名を特に決めずにデフォルトのまま保存するならカメラロールAPIを使うのが楽。<br /> <a target="_blank" rel="nofollow noopener" href="https://facebook.github.io/react-native/docs/cameraroll">React Native CameraRoll API</a><br /> <a target="_blank" rel="nofollow noopener" href="https://medium.com/react-native-training/mastering-the-camera-roll-in-react-native-13b3b1963a2d">Mastering the Camera Roll in React Native</a></p> <h2>なぜカメラロールを使わないか</h2> <ul> <li>ファイル名が自由に決められない→<a target="_blank" rel="nofollow noopener" href="https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#readfilepath-encodingpromise">rn-fetch-blob</a>のファイルシステムAPIを使う</li> </ul> <pre><code class="javascript">startRecording() { (async()=>{ this.setState({ recording: true }); // default to mp4 for android as codec is not set const { uri, codec = "mp4" } = await this.camera.recordAsync(); let file = await RNFetchBlob.fs.readFile(uri,'base64') let dir = RNFetchBlob.fs.dirs.PictureDir await RNFetchBlob.fs.writeFile( dir +'/'+ moment().format('YYYYMMDDHHmmSS') + '.mp4', file, 'base64' ) //await CameraRoll.saveToCameraRoll(uri) })() .catch((err)=>{ console.log(err) }) } </code></pre> <h1>ハマりどころ</h1> <h2>react-native-cameraが真っ白になる</h2> <p><a target="_blank" rel="nofollow noopener" href="https://github.com/moaazsidat/react-native-qrcode-scanner/issues/73">先駆者は言っています、「スタイルを確認せよ」と。</a></p> <h2>ストレージの保存処理はstartRecording()の中で</h2> <p>stopRecording()メソッドの中で書くべきだと思ってreduxとか導入したのですが、再描画されないとstoreもstateも変化しないんですよね。</p> ckoshien