zoukankan      html  css  js  c++  java
  • android四大组件之Service 从网络上播放音乐

    package com.example.musicplay;
    
    import java.io.InputStream;
    import java.io.Reader;
    import java.math.BigDecimal;
    import java.net.URL;
    import java.sql.Array;
    import java.sql.Blob;
    import java.sql.Clob;
    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.NClob;
    import java.sql.ParameterMetaData;
    import java.sql.PreparedStatement;
    import java.sql.Ref;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.RowId;
    import java.sql.SQLException;
    import java.sql.SQLWarning;
    import java.sql.SQLXML;
    import java.sql.Time;
    import java.sql.Timestamp;
    import java.util.Calendar;
    
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class PlayService extends Service {
        private MediaPlayer mediaPlayer;
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            return new MusicBinder();
        }
        class MusicBinder extends Binder implements PlayInterface{
    
            @Override
            public void play() {
                // TODO Auto-generated method stub
                PlayService.this.play();
            }
    
            @Override
            public void playContiue() {
                // TODO Auto-generated method stub
                PlayService.this.playContiue();
            }
    
            @Override
            public void pause() {
                // TODO Auto-generated method stub
                PlayService.this.pause();
                
            }
            
        }
        @Override
        public void onCreate() {
            mediaPlayer = new MediaPlayer();
        }
        
        private void play(){
            //重置
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource("http://192.168.21.1:8080/ok/Charlotte Perrelli - Hero.mp3");
                //准备
                mediaPlayer.prepareAsync();
            } catch (Exception e) {
                // TODO Auto-generated catcssh block
                e.printStackTrace();
            }
            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                //准备完毕此方法调用
                @Override
                public void onPrepared(MediaPlayer arg0) {
                    // TODO Auto-generated method stub
                    mediaPlayer.start();
                }
            });
        }
        private void playContiue(){
            mediaPlayer.start();
        }
        private void pause(){
            mediaPlayer.pause();
        }
    }
  • 相关阅读:
    Centos 下Nginx 自启动脚本
    EUI ToggleButton ToggleSwitch 实现类似音乐开关按钮
    EUI RadioButton,RadioButtonGroup实现多选项按钮
    Theme皮肤文件(json解析、多文件管理)
    egret.Tween、egret.Ease
    Bitmap 的bitmapdata和texture区别
    在Egret实现二维码长按识别
    微信测试号实现微信分享等功能
    Egret Wing3 商城插件下载和使用
    Egret Wing3 FTP使用方法
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/4977635.html
Copyright © 2011-2022 走看看