Certainly! I can help you build a React Native Expo app that mimics the iPhone's Dynamic Island feature for music playback. Below is a complete code with proper file names and project structure.
Project Structure
YourProject
│ app.json
│ App.js
│
└───assets
│
└───components
│ DynamicIsland.js
│ MusicPlayer.js
│
└───screens
HomeScreen.js
Step-by-Step Guide
1. Initialize the Expo Project
First, initialize a new Expo project:
expo init YourProject
cd YourProject
2. Install Necessary Dependencies
We'll need some additional packages:
npm install react-native-gesture-handler react-native-reanimated
Note: Make sure to install the latest versions compatible with Expo.
Code Files
1. App.js
// App.js
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import HomeScreen from './screens/HomeScreen';
export default function App() {
return (
<NavigationContainer>
<HomeScreen />
</NavigationContainer>
);
}
2. screens/HomeScreen.js
// screens/HomeScreen.js
import React from 'react';
import { View, StyleSheet } from 'react-native';
import MusicPlayer from '../components/MusicPlayer';
import DynamicIsland from '../components/DynamicIsland';
const HomeScreen = () => {
return (
<View style={styles.container}>
<MusicPlayer />
<DynamicIsland />
</View>
);
};
export default HomeScreen;
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
3. components/MusicPlayer.js
This component will handle the music playback logic.
// components/MusicPlayer.js
import React, { useState } from 'react';
import { Audio } from 'expo-av';
const MusicPlayer = () => {
const [sound, setSound] = useState();
const playSound = async () => {
const { sound } = await Audio.Sound.createAsync(
require('../assets/sample.mp3')
);
setSound(sound);
await sound.playAsync();
};
const pauseSound = async () => {
if (sound) {
await sound.pauseAsync();
}
};
return null; // Since this component doesn't render anything
};
export default MusicPlayer;
Note: Place a sample.mp3 file in the assets folder or use any local/remote audio file.
4. components/DynamicIsland.js
This component will mimic the Dynamic Island.
// components/DynamicIsland.js
import React, { useState } from 'react';
import {
View,
Text,
StyleSheet,
TouchableWithoutFeedback,
Animated,
Image,
} from 'react-native';
const DynamicIsland = () => {
const [expanded, setExpanded] = useState(false);
const animation = new Animated.Value(0);
const toggleExpand = () => {
setExpanded(!expanded);
Animated.timing(animation, {
toValue: expanded ? 0 : 1,
duration: 300,
useNativeDriver: false,
}).start();
};
const widthInterpolate = animation.interpolate({
inputRange: [0, 1],
outputRange: [200, 350],
});
const heightInterpolate = animation.interpolate({
inputRange: [0, 1],
outputRange: [50, 200],
});
const islandStyle = {
width: widthInterpolate,
height: heightInterpolate,
};
return (
<TouchableWithoutFeedback onPress={toggleExpand}>
<Animated.View style={[styles.islandContainer, islandStyle]}>
{expanded ? (
<View style={styles.expandedContent}>
<Image
source={require('../assets/album_art.png')}
style={styles.albumArt}
/>
<View style={styles.songInfo}>
<Text style={styles.songTitle}>Song Title</Text>
<Text style={styles.artistName}>Artist Name</Text>
</View>
<View style={styles.controls}>
<Text style={styles.controlButton}>⏮️</Text>
<Text style={styles.controlButton}>⏯️</Text>
<Text style={styles.controlButton}>⏭️</Text>
</View>
</View>
) : (
<View style={styles.collapsedContent}>
<Text style={styles.timer}>1:23</Text>
<Image
source={require('../assets/album_thumb.png')}
style={styles.thumbnail}
/>
</View>
)}
</Animated.View>
</TouchableWithoutFeedback>
);
};
export default DynamicIsland;
const styles = StyleSheet.create({
islandContainer: {
position: 'absolute',
top: 50,
alignSelf: 'center',
backgroundColor: '#000',
borderRadius: 25,
overflow: 'hidden',
},
collapsedContent: {
flexDirection: 'row',
alignItems: 'center',
padding: 10,
},
expandedContent: {
flex: 1,
padding: 20,
},
timer: {
color: '#fff',
marginRight: 10,
},
thumbnail: {
width: 30,
height: 30,
},
albumArt: {
width: '100%',
height: 100,
marginBottom: 20,
},
songInfo: {
alignItems: 'center',
marginBottom: 20,
},
songTitle: {
color: '#fff',
fontSize: 18,
fontWeight: 'bold',
},
artistName: {
color: '#fff',
fontSize: 14,
},
controls: {
flexDirection: 'row',
justifyContent: 'space-around',
},
controlButton: {
color: '#fff',
fontSize: 24,
},
});
Note: You'll need to place album_art.png and album_thumb.png images in your assets folder.
Explanation
- App.js: The root component initializing the navigation container.
- HomeScreen.js: The main screen that includes the
MusicPlayer and DynamicIsland components.
- MusicPlayer.js: Handles music playback using Expo's Audio API.
- DynamicIsland.js:
- Uses
Animated for transitions.
- Toggles between expanded and collapsed states.
- In the expanded state, displays song information and controls.
- In the collapsed state, shows minimal info like timer and thumbnail.
- Styles: Adjust the styles as needed to match the desired appearance.
Running the App
Start the Expo development server:
expo start
Use a simulator or your own device with the Expo Go app to run the project.
Additional Notes
- Permissions: Make sure to handle permissions if you're accessing external storage or media.
- State Management: For a real app, consider using a state management library like Redux or Context API to manage the music player's state across components.
- Animations: Enhance the animations with
react-native-reanimated for better performance.
- Testing: Test on both iOS and Android devices to ensure compatibility.
Conclusion
You now have a basic implementation of a Dynamic Island feature in a React Native Expo app. This setup mimics the music playback UI similar to the iPhone's Dynamic Island. Feel free to customize and expand upon this foundation to suit your specific needs.