在Android应用中实现直播购物功能,需要结合视频直播、实时互动和电商功能。以下是简化版的核心代码示例:
typescript
// 直播播放界面 LivePlayerActivity.java
public class LivePlayerActivity extends AppCompatActivity {
private AliLivePlayerView livePlayerView; // 阿里云直播SDK的播放器视图
private RecyclerView productRecyclerView;
private ProductAdapter productAdapter;
private WebSocketClient webSocketClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_player);
// 初始化直播播放器
livePlayerView = findViewById(R.id.live_player_view);
String playUrl = "rtmp://example.com/live/stream123";
livePlayerView.setLiveSource(playUrl);
livePlayerView.startPlay();
// 初始化商品列表
productRecyclerView = findViewById(R.id.product_list);
productRecyclerView.setLayoutManager(new LinearLayoutManager(this));
productAdapter = new ProductAdapter(getSampleProducts());
productRecyclerView.setAdapter(productAdapter);
// 初始化WebSocket实时通信
initWebSocket();
}
private void initWebSocket() {
webSocketClient = new WebSocketClient("ws://example.com/chat") {
@Override
public void onMessage(String message) {
// 处理实时消息(弹幕、商品推送等)
handleRealTimeMessage(message);
}
};
webSocketClient.connect();
}
private void handleRealTimeMessage(String message) {
// 解析JSON消息
if (message.startsWith("PRODUCT_HIGHLIGHT")) {
String productId = message.split(":")[1];
highlightProduct(productId);
} else {
// 显示弹幕消息
showDanmaku(message);
}
}
// 商品点击事件处理
private void onProductSelected(Product product) {
Intent intent = new Intent(this, ProductDetailActivity.class);
intent.putExtra("product_id", product.getId());
startActivity(intent);
}
// 加入购物车
private void addToCart(Product product) {
CartManager.getInstance().addProduct(product);
Toast.makeText(this, "已加入购物车", Toast.LENGTH_SHORT).show();
}
}
// 商品适配器 ProductAdapter.java
class ProductAdapter extends RecyclerView.Adapter<ProductViewHolder> {
private List<Product> products;
public ProductAdapter(List<Product> products) {
this.products = products;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_product, parent, false);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = products.get(position);
holder.name.setText(product.getName());
holder.price.setText("¥" + product.getPrice());
Glide.with(holder.itemView).load(product.getImageUrl()).into(holder.image);
holder.itemView.setOnClickListener(v -> {
((LivePlayerActivity)context).onProductSelected(product);
});
holder.addCartBtn.setOnClickListener(v -> {
((LivePlayerActivity)context).addToCart(product);
});
}
}
// 购物车管理单例 CartManager.java
public class CartManager {
private static CartManager instance;
private List<Product> cartItems = new ArrayList<>();
public static synchronized CartManager getInstance() {
if (instance == null) {
instance = new CartManager();
}
return instance;
}
public void addProduct(Product product) {
cartItems.add(product);
// 这里可以发送网络请求更新服务器购物车
}
public List<Product> getCartItems() {
return new ArrayList<>(cartItems);
}
}
// 商品详情页 ProductDetailActivity.java
public class ProductDetailActivity extends AppCompatActivity {
private Product currentProduct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
String productId = getIntent().getStringExtra("product_id");
fetchProductDetails(productId);
}
private void fetchProductDetails(String productId) {
// 调用API获取商品详情
ProductRepository.getProduct(productId, new Callback<Product>() {
@Override
public void onSuccess(Product product) {
currentProduct = product;
updateUI();
}
});
}
private void updateUI() {
TextView priceView = findViewById(R.id.product_price);
priceView.setText("¥" + currentProduct.getPrice());
// 更新其他UI元素...
}
public void onPurchaseClick(View view) {
// 处理购买逻辑
Order order = new Order(currentProduct);
PaymentService.startPayment(this, order);
}
}
关键实现要点:
- 直播功能集成:
- 使用阿里云/腾讯云直播SDK
- 支持RTMP/FLV/HLS协议
- 处理播放器生命周期
- 实时互动功能:
- WebSocket实现实时消息
- 弹幕消息渲染
- 实时商品状态同步
- 电商功能:
- 商品列表展示(RecyclerView)
- 购物车管理(单例模式)
- 订单支付流程
- 界面布局要点:
xml
<!-- activity_live_player.xml -->
<RelativeLayout>
<!-- 直播播放区域 -->
<com.aliyun.player.AliyunPlayerView
android:id="@+id/live_player_view"
android:layout_width="match_parent"
android:layout_height="300dp"/>
<!-- 商品列表 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/product_list"
android:layout_below="@id/live_player_view"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<!-- 弹幕输入框 -->
<EditText
android:id="@+id/danmaku_input"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
- 需要添加的权限:
ini
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
完整实现还需要:
- 后端接口对接:
- 直播地址获取
- 商品数据API
- 订单支付API
- 实时消息服务
- 性能优化:
- 直播卡顿处理
- 消息队列防刷
- 图片缓存(Glide)
- WebSocket重连机制
- 扩展功能:
- 优惠券领取
- 主播互动游戏
- 商品秒杀倒计时
- 多直播间切换
建议使用成熟的第三方服务:
- 直播:阿里云直播、腾讯云直播
- IM:融云、环信
- 支付:支付宝/微信SDK
以上代码展示了核心流程,实际开发需要根据具体业务需求补充异常处理、状态管理、安全校验等逻辑。