آموزش جاوااسکریپت › انجمن ها › react native › استفاده از فایل صوتی در react native
- این موضوع 3 پاسخ، 2 کاربر را دارد و آخرین بار در 5 سال پیش بدست مهدی حسن زاده بهروزرسانی شده است.
در حال نمایش 4 نوشته (از کل 4)
-
نویسندهنوشتهها
-
علیمیهمان
سلام چطور میتونم از فایل های صوتی داخل اپلیکشنم استفاده کنم اعمالی مثل play و… لطفا راهنمایی کنید
مهدیمیهمانسلام.شما میتونید از کتابخانه react-native-video استفاده کنید که در درس 75 از دوره جامع کار با این کتابخانه و پخش فایل صوتی و ویدئویی رو آموزش دادم.
لینک دوره: https://goo.gl/pMZrXR
لینک کتابخانه:https://github.com/react-native-community/react-native-videoعلیمیهمانسلام مهندس
این کد منهJavaScript123456789101112131415class RemoteSound extends Component {playTrack = () => {const track = new Sound('file -name.mp3', null, (e) => {if (e) {console.log('error loading track:', e)} else {track.play()}})}render() {return <Button title="play me" onPress={this.playTrack} />}}میشه کمک کنی میخوام فایل هارو با یک state پاس بدم به متغیر track ولی نمیدونم چجوری این کارو بکنم خیلیم گشتم ولی نتونستم چیزی پیدا کنم
مهدی حسن زادهمدیرکلسلام
JavaScript123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293import Video from 'react-native-video';class MusicPlay extends Component{constructor(props){super(props);this.state = {rate: 1,volume: 1,muted: false,resizeMode: 'contain',duration: 0.0,currentTime: 0.0,paused: true,};video: Video;}onLoad = (data) => {this.setState({ duration: data.duration });};onProgress = (data) => {this.setState({ currentTime: data.currentTime });};onEnd = () => {this.setState({ paused: true })this.video.seek(0)};onAudioBecomingNoisy = () => {this.setState({ paused: true })};onAudioFocusChanged = (event: { hasAudioFocus: boolean }) => {this.setState({ paused: !event.hasAudioFocus })};getCurrentTimePercentage() {if (this.state.currentTime > 0) {return parseFloat(this.state.currentTime) / parseFloat(this.state.duration);}return 0;};renderRateControl(rate) {const isSelected = (this.state.rate === rate);return (<TouchableOpacity onPress={() => { this.setState({ rate }) }}><Text style={[styles.controlOption, { fontWeight: isSelected ? 'bold' : 'normal' }]}>{rate}x</Text></TouchableOpacity>);}renderResizeModeControl(resizeMode) {const isSelected = (this.state.resizeMode === resizeMode);return (<TouchableOpacity onPress={() => { this.setState({ resizeMode }) }}><Text style={[styles.controlOption, { fontWeight: isSelected ? 'bold' : 'normal' }]}>{resizeMode}</Text></TouchableOpacity>)}renderVolumeControl(volume) {const isSelected = (this.state.volume === volume);return (<TouchableOpacity onPress={() => { this.setState({ volume }) }}><Text style={[styles.controlOption, { fontWeight: isSelected ? 'bold' : 'normal' }]}>{volume * 100}%</Text></TouchableOpacity>)}renderSoundIcon(){if(this.state.volume===1){return(<Icon name="volume-up" size={22} onPress={()=>this.setState({volume:0})} />);}return(<Icon name="volume-off" size={22} onPress={()=>this.setState({volume:1})} />)}render() {const { params } = this.props.navigation.state;const flexCompleted = this.getCurrentTimePercentage() * 100;const flexRemaining = (1 - this.getCurrentTimePercentage()) * 100;const {navigation}=this.props;return (<View style={styles.container}><HeaderElevators headerText={params.Item.title} navigation={navigation} /><Videoref={(ref: Video) => { this.video = ref }}/* For ExoPlayer */source={{ uri: params.Item.url, type: 'mpd' }}// source={require('./broadchurch.mp4')}// style={styles.fullScreen}rate={this.state.rate}paused={this.state.paused}volume={this.state.volume}muted={this.state.muted}resizeMode={this.state.resizeMode}onLoad={this.onLoad}onProgress={this.onProgress}onEnd={this.onEnd}onAudioBecomingNoisy={this.onAudioBecomingNoisy}onAudioFocusChanged={this.onAudioFocusChanged}repeat={false}/><View style={styles.imgBox}><Image source={{uri: 'http://upmusic.ir/wp-content/uploads/2017/12/Macan-Band-2-Daghighe-Boodi.jpg'}} style={styles.picStyle} /></View><View style={styles.boxName}><Text style={styles.soundNameStyle}>{params.Item.title}</Text></View><View style={styles.boxName}><Text style={styles.artistNameStyle}>{params.Item.artistName}</Text></View><View style={styles.boxName}><View style={[styles.favioretisBoxIcon,{alignItems:'flex-start'}]}><Icon name="heart-o" size={22} /></View><View style={[styles.favioretisBoxIcon,{alignItems:'flex-end'}]}>{this.renderSoundIcon()}</View></View><View style={styles.controls}><View style={styles.trackingControls}><View style={styles.progress}>{( Platform.OS === 'android' )?( <ProgressBarAndroid styleAttr = "Horizontal" progress = { this.getCurrentTimePercentage()} indeterminate = { false } style={{width:'100%'}} /> ):( <ProgressViewIOS progress = { this.getCurrentTimePercentage() } /> )}</View></View></View><View style={styles.IconsBox}><View style={styles.playStyle}><Icon name="backward" size={22} onPress={()=>this.setState({currentTime:this.state.currentTime-5})}/></View><View style={styles.playStyle}>{this.state.paused?<Icon name="play" size={22}onPress={() => this.setState({ paused: !this.state.paused })}/>:<Icon name="pause" size={22}onPress={() => this.setState({ paused: !this.state.paused })}/>}</View><View style={styles.playStyle}><Icon name="forward" size={22} onPress={()=>this.renderRateControl(0.25)}/></View></View></View>);}}const styles = StyleSheet.create({container: {flex: 1,backgroundColor: '#fff',},IconsBox:{width:'100%',height:40,flexDirection:'row',backgroundColor:'transparent',justifyContent:'center',alignItems:'center',marginTop:30},imgBox:{width:'100%',height:300,},picStyle:{width:'100%',height:'100%',},boxName:{width:'95%',height:30,marginLeft:'2.5%',marginRight:'2.5%',backgroundColor:'transparent',borderRadius:5,justifyContent:'center',alignItems:'center',marginTop:10,flexDirection:'row'},favioretisBoxIcon:{width:'45%',height:'100%',backgroundColor:'transparent',justifyContent:'center',},soundNameStyle:{fontSize:18,fontFamily:'IRANSans',color:'#000',fontWeight:'bold',},artistNameStyle:{fontSize:16,fontFamily:'IRANSans',color:'#000',},fullScreen: {width:'100%',height:350,justifyContent: 'center',alignItems: 'center',},playStyle:{width:'33%',height:'100%',justifyContent: 'center',alignItems: 'center',alignSelf:'center'},controls: {backgroundColor: 'transparent',borderRadius: 5,position: 'absolute',bottom: 70,left: 20,right: 20,},progress: {flex: 1,flexDirection: 'row',borderRadius: 3,overflow: 'hidden',},innerProgressCompleted: {height: 20,backgroundColor: '#cccccc',},innerProgressRemaining: {height: 20,backgroundColor: '#2C2C2C',},generalControls: {flex: 1,flexDirection: 'row',borderRadius: 4,overflow: 'hidden',paddingBottom: 10,},rateControl: {flex: 1,flexDirection: 'row',justifyContent: 'center',},volumeControl: {flex: 1,flexDirection: 'row',justifyContent: 'center',},resizeModeControl: {flex: 1,flexDirection: 'row',alignItems: 'center',justifyContent: 'center',},controlOption: {alignSelf: 'center',fontSize: 11,color: 'white',paddingLeft: 2,paddingRight: 2,lineHeight: 12,},}); -
نویسندهنوشتهها
در حال نمایش 4 نوشته (از کل 4)